Faking Requests
Hyper has the ability to fake requests and return fake responses as a result. This is perfect for testing scenarios where you don't actually want to make an HTTP request.
Non-ColdBox users will need to add a mapping to the Globber
dependency included with Hyper.
Faking All Requests
Faking is enabled on a HyperBuilder
instance, either the built-in one provided by Hyper or a custom Hyper client you have created.
To enable faking requests, call the fake
method:
After calling the fake
method, all requests created by this HyperBuilder
will be faked. By default, they will return 200 OK
responses with empty bodies.
Fake Configuration
When calling the fake
method, you can provide a struct mapping URL patterns to response generator functions.
The key is a URL pattern. The pattern can be any valid glob. If the pattern is matched by the fullUrl
of the HyperRequest
, then the response generator function will be called and the results returned.
A helper function, newFakeResponse
, is provided as the first argument to the response generator function. The current request is provided as the second argument.
FakeHyperResponse
The resposne generator function must return a FakeHyperResponse
instance. A FakeHyperResponse
instance acts similar to a normal HyperResponse
except the properties are not read only. Once you have an instance of your FakeHyperResponse
, you can continue to set any properties you need.
You can also pass the values to the newFakeResponse
function, if you'd like.
newFakeResponse
newFakeResponse
Creates a new FakeHyperResponse
instance.
statusCode
numeric
false
200
The status code for the fake response.
statusText
string
false
"OK"
The status text for the fake response.
data
string
false
""
The data for the fake response.
headers
{ string: string }
false
{}
The headers for the fake response.
executionTime
numeric
false
0
The execution time for the fake response.
charset
string
false
"UTF-8"
The charset for the fake response.
timestamp
datetime
false
now()
The timestamp of the fake response.
Return: FakeHyperResponse
Sequencing Fake Responses
In addition to returning a single FakeHyperResponse
per pattern, you can return an array of FakeHyperResponse
instances. These will be returned in a sequence.
If you continue to make requests to the same pattern after the sequence has been exhausted, an exception will be thrown.
Preventing Stray Requests
By default, Hyper returns a default FakeHyperResponse
for every request that doesn't match one of your configured patterns. You can instead cause Hyper to throw an exception if it encounters one of these stray requests by calling the preventStrayRequests
method.
Making Assertions
In addition to assertions made against the FakeHyperResponse
instances returned, you can also make assertions using the HyperBuilder
instance you faked. These come in the form of methods on the HyperBuilder
instance as well as custom TestBox Assertions.
HyperBuilder Method Assertions
The following methods are available to make assertions in your tests about the requests that were sent.
getFakeRequestCount
Returns the number of fake requests that have been made.
No arguments
Return: numeric
wasRequestSent
Returns whether a request has been made that matches the predicate
. Each request that has been made is passed to the predicate
in order. If the predicate
returns true
, the request is considered a match and true
is returned. If no request passes the predicate
, false
is returned.
predicate
function
true
A callback function that returns true if the request matches the criteria and returns false otherwise.
Return: boolean
Custom TestBox Assertions
You can register custom TestBox assertions provided by Hyper for more readable tests and test failure messages.
You must register these assertions before using them.
The following custom assertions are provided:
toHaveSentRequest
This takes in the HyperBuilder
instance as the actual and a predicate function as the expected. The predicate function is passed to the HyperBuilder
instance's wasRequestSent
method.
toHaveSentCount
This takes in the HyperBuilder
instance as the actual and an integer number of requests that should have been sent.
toHaveSentNothing
This takes in the HyperBuilder
instance as the actual and no other parameters.
Resetting the Builder
To reset the sent request counts and sequences but keep the fake configuration, use the resetFakes
method:
To reset the HyperBuilder
instance to normal operation call the clearFakes
method:
These are useful methods to call in an afterEach
block in TestBox to make sure you are only faking requests when you want to.