Mock rule builders should be constructed through the Mockttp instance you're using, not directly. You shouldn't ever need to call this constructor.
Mock rule builders should be constructed through the Mockttp instance you're using, not directly. You shouldn't ever need to call this constructor.
Run this rule forever, for all matching requests
Run this rule only once, for the first matching request
Call the given callback for any matched requests that are recieved, and build a response from the result.
The callback should return an object, potentially including various fields to define the response. All fields are optional, and default to being empty/blank, except for the status, which defaults to 200.
Valid fields are:
status (number)body (string)headers (object with string keys & values)json (object, which will be sent as a JSON response)If the callback throws an exception, the server will return a 500 with the exception message.
Reply to matched with the given status and a JSON object, with optionally more headers.
This method is shorthand for: server.get(...).thenReply(status, JSON.stringify(object), { 'Content-Type': 'application/json' })
This method returns a promise that resolves with a mocked endpoint. Wait for the promise to confirm that the rule has taken effect before sending requests to be matched. The mocked endpoint can be used to assert on the requests matched by this rule.
Pass matched requests through to their real destination. This works for proxied requests only, direct requests will be rejected with an error.
Calling this method registers the rule with the server, so it starts to handle requests.
This method returns a promise that resolves with a mocked endpoint. Wait for the promise to confirm that the rule has taken effect before sending requests to be matched. The mocked endpoint can be used to assert on the requests matched by this rule.
Reply to matched with with given status and (optionally) body and headers.
Calling this method registers the rule with the server, so it starts to handle requests.
This method returns a promise that resolves with a mocked endpoint. Wait for the promise to confirm that the rule has taken effect before sending requests to be matched. The mocked endpoint can be used to assert on the requests matched by this rule.
Run this rule three times, for the first three matching requests
Run this rule the given number of times, for the first matching requests
Run this rule twice, for the first two matching requests
Match only requests whose bodies include the given form data
Match only requests that include the given headers
Generated using TypeDoc
MockRuleBuilder
A builder for defining mock rules. Create one using a method like
.get(path)or.post(path)on a Mockttp instance, then call whatever methods you'd like here to define more precise request matching behaviour, control how the request is handled, and how many times this rule should be applied.When you're done, call a
.thenX()method to register the configured rule with the server. These return a promise for a MockedEndpoint, which can be used to verify the details of the requests matched by the rule.This returns a promise because rule registration can be asynchronous, either when using a remote server or testing in the browser. Wait for the promise returned by
.thenX()methods to guarantee that the rule has taken effect before sending requests to it.