Run this rule forever, for all matching requests
Run this rule only once, for the first matching request
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 sent to the given host, i.e. the full hostname plus port included in the request.
This can behave somewhat confusingly when matching against the default ports for a protocol (i.e. 80/443), or when specifying a hostname here without specifying the port. In those cases it's usually better to use forHostname and/or forPort instead to explicit match the content you're interested in.
Match only requests sent to the given hostname, ignoring the port.
Match only requests sent to the given port.
Match only requests when the callback returns true
Match only requests whose bodies either exactly match the given string (if a string is passed) or whose bodies match a regular expression (if a regex is passed).
Match only requests whose bodies include the given string.
Match only requests that include the given cookies
Match only requests that include the exact query string provided. The query string must start with a ? or be entirely empty.
Match only requests whose bodies include the given URL-encoded form data.
Match only requests that include the given headers.
Match only requests whose bodies exactly match the given object, when parsed as JSON.
Note that this only tests that the body can be parsed as JSON - it doesn't require a content-type header.
Match only requests whose bodies match (contain equivalent values, ignoring extra values) the given object, when parsed as JSON. Matching behaviour is the same as Lodash's _.isMatch method.
Note that this only tests that the body can be parsed as JSON - it doesn't require a content-type header.
Match only requests whose bodies include the given multipart form data.
This can take any number of form parts to look for. Each part is specified
with MultipartFieldMatchCondition object containing one or more of
name
(string), filename
(string) and content
(string or buffer) as
fields to match against in the form data.
Requests are matched if all conditions match at least one part in the request's form data.
Match only requests sent with the given protocol.
Match only requests that include the given query parameters.
Match only requests whose absolute url matches the given RegExp.
Set the rule priority. Any matching rule with a higher priority will always
take precedence over a matching lower-priority rule, unless the higher rule
has an explicit completion check (like .once()
) that has already been
completed.
The RulePriority enum defines the standard values useful for most cases, but any positive number may be used for advanced configurations.
In many cases it may be simpler to use forUnmatchedRequest() to set a fallback rule explicitly, rather than manually setting the priority here.
Close connections that match this rule immediately, without accepting the socket or sending any other response.
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.
Accept incoming WebSocket connections, and echo every message received on the WebSocket back to the client.
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.
Forward matched websockets on to the specified forwardToUrl. The url specified must not include a path or an error will be thrown. The path portion of the original request url is used instead.
The url may optionally contain a protocol. If it does, it will override the protocol (and potentially the port, if unspecified) of the request. If no protocol is specified, the protocol (and potentially the port) of the original request URL will be used instead.
This method takes options to configure how the request is passed through. See PassThroughWebSocketHandlerOptions for the full details of the options available.
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.
Pass matched websockets through to their real destination. This works for proxied requests only, and direct requests will be rejected with an error.
This method takes options to configure how the request is passed through. See PassThroughWebSocketHandlerOptions for the full details of the options available.
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.
Accept incoming WebSocket connections, and simply listen to incoming messages without ever sending anything in return.
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.
Rejects connections, sending an HTTP response with the given status, message and body, to explicitly refuse the WebSocket handshake.
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.
Reset connections that match this rule immediately, sending a TCP RST packet directly, without accepting the socket or sending any other response, and without cleanly closing the TCP connection.
This is only supported in Node.js versions (>=16.17, >=18.3.0, or
later), where net.Socket
includes the resetAndDestroy
method.
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.
Hold open connections that match this rule, but never respond with anything at all, typically causing a timeout on the client side.
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.
Generated using TypeDoc
Mock rule builders should be constructed through the Mockttp instance you're using, not directly. You shouldn't ever need to call this constructor.