Skip to contents

A stub pairs a matcher with a response. Every supplied criterion must match (they are ANDed); an omitted criterion matches anything.

Usage

zu_stub(
  response,
  method = NULL,
  url = NULL,
  regex = NULL,
  headers = NULL,
  body = NULL,
  times = Inf
)

Arguments

response

A zu_response(), or a function of one request returning one. The function form is for stubs whose reply depends on the request.

method

HTTP method to match, case-insensitive. NULL matches any.

url

URL to match. A plain string must match exactly after client merging — that is, the absolute URL with the query attached. Use regex for anything looser.

regex

A regular expression matched against the URL, as an alternative to url.

headers

Named character vector of headers that must be present with these values. Names are matched case-insensitively (§18.3); headers not named here are ignored.

body

Raw or character body that must match exactly.

times

Maximum number of times this stub may match; Inf by default. A stub with times = 1 is how you assert a request is not retried.

Value

A zu_stub, for zu_mock_transport().

Lifecycle

Experimental (D-65). The testing tools are exported so that package tests can run without a network, and may still change before 1.0; the request functions, response accessors and conditions are the stable API.

Examples

stub <- zu_stub(zu_response(201L), method = "POST", regex = "/users$")
api  <- zu_client(transport = zu_mock_transport(stub))
zu_resp_status(zu_perform(zu_request("POST", "https://x.test/users"),
                          client = api))
#> [1] 201