Skip to content

Latest commit

 

History

History
116 lines (73 loc) · 2.11 KB

request-matcher.md

File metadata and controls

116 lines (73 loc) · 2.11 KB

RequestMatcher

Created by a Respond object to match a request to a response:

const matcher: RequestMatcher = respond.ok().when;

method(method)

Matches requests with the given method.

Parameters

  • method RequesetMethod The request method to match

Returns this

respond.ok().when.method(RequestMethod.Put);

hasHeader(key)

Matches requests that have the header key set

Parameters

  • key string The request header key to check

Returns this

respond.ok().when.hasHeader('Auth');

hasHeader(key, value)

Matches requests that have the header key set to the value provided

Parameters

  • key string The request header key to check
  • value string | string[] The request header value to check

Return this

respond.ok().when.hasHeader('Auth', 'Token 123ABCD');

body(body)

Matches requests that have the same body

Parameters

  • body any The request body object to match. Non-string bodies are automatically serialized to JSON

Return this

respond.ok().when.body({ authenticated: true });

url(url)

Matches requests that have the same URL

Parameters

  • url string The request URL to match

Returns this

respond.ok().when.url('/api');

get(url) / delete(url)

Shortcuts to set URL with the GET or DELETE methods

Parameters

  • url string The request URL to match

Returns this

respond.ok().when.get('/api');
respond.ok().when.delete('/post/123');

post(url, body) / put(url, body)

Shortcuts to set the URL and body with the POST and PUT methods

Parameters

  • url string The request URL to match
  • body any The request body to match

Returns this

respond.ok().when.post('/users', { username: 'mikeryan' });
respond.ok().when.put('/posts/123', { title: 'Test' });

match(assertion)

Create a custom assertion used to match the request

Parameters

  • assertion (req: Request) => boolean Function to match the Request

Returns this

respond.ok().when.match(req => req.status === 404);