Options
All
  • Public
  • Public/Protected
  • All
Menu

Class MockRuleBuilder

class

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.

Hierarchy

  • MockRuleBuilder

Index

Constructors

constructor

Properties

Private addRule

addRule: function

Type declaration

Private Optional isComplete

Private matchers

matchers: MatcherData[] = []

Methods

always

once

thenCallback

  • 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.

    Parameters

    Returns Promise<MockedEndpoint>

thenJSON

  • thenJSON(status: number, data: object, headers?: OutgoingHttpHeaders): Promise<MockedEndpoint>
  • 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.

    Parameters

    • status: number
    • data: object
    • Default value headers: OutgoingHttpHeaders = {}

    Returns Promise<MockedEndpoint>

thenPassThrough

  • 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.

    Returns Promise<MockedEndpoint>

thenReply

  • thenReply(status: number, data?: undefined | string, headers?: OutgoingHttpHeaders): Promise<MockedEndpoint>
  • 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.

    Parameters

    • status: number
    • Optional data: undefined | string
    • Optional headers: OutgoingHttpHeaders

    Returns Promise<MockedEndpoint>

thrice

times

twice

withForm

withHeaders

  • withHeaders(headers: object): this
  • Match only requests that include the given headers

    Parameters

    • headers: object
      • [key: string]: string

    Returns this

Generated using TypeDoc