F
F
Frisby
Search…
Running Assertions
Frisby has many ways of running assertions against the HTTP response. Below are the following methods available by default:

expect(handler, [...args])

Frisby comes with many handy built-in expect handlers to help you test the HTTP response of your API.
  • status - Check HTTP status
  • header - Check HTTP header key + value
  • json - Match json structure + values
  • jsonStrict - Match EXACT json structure + values (extra keys not tested for cause test failures)
  • jsonTypes - Match json structure + value types
  • jsonTypesStrict - Match EXACT json structure + value types (extra keys not tested for cause test failures)
  • bodyContains - Match partial body content (string or regex)

expect('status', statusCode)

1
it('should be a teapot', function () {
2
return frisby.get('https://httpbin.org/status/418')
3
.expect('status', 418);
4
});
Copied!

expect('header', key [, value])

1
it('should have a JSON Content-Type header', function () {
2
return frisby.get('https://httpbin.org/headers')
3
.expect('header', 'Content-Type', 'application/json');
4
});
Copied!

expect('json' [, path], data)

1
it('should have a "Host" header with a value of "httpbin.org"', function () {
2
return frisby.get('https://httpbin.org/headers')
3
.expect('json', 'headers', {
4
Host: 'httpbin.org'
5
});
6
});
Copied!
A more complex example:
1
it ('should return a list of feed items', function () {
2
return frisby
3
.get('https://jsonfeed.org/feed.json')
4
.expect('status', 200)
5
.expect('json', 'version', 'https://jsonfeed.org/version/1')
6
.expect('json', 'title', 'JSON Feed')
7
.expect('jsonTypes', 'items.*', { // Assert *each* object in 'items' array
8
'id': Joi.string().required(),
9
'url': Joi.string().uri().required(),
10
'title': Joi.string().required(),
11
'date_published': Joi.date().iso().required(),
12
});
13
});
Copied!

expect('jsonTypes' [, path], data)

1
it('should return all headers as strings', function () {
2
return frisby.get('https://httpbin.org/headers')
3
// Using a wildcard in the path '*' check EACH value
4
.expect('jsonTypes', 'headers.*', frisby.Joi.string());
5
});
Copied!

expectNot(handler, [...args])

Runs an inverse assertion that passes when there is an error thrown. Uses all the same types and arguments as expect().
1
it('should not return an error', function () {
2
return frisby.get('https://httpbin.org/headers')
3
// Should not return an error
4
.expectNot('json', { result: 'error' });
5
});
Copied!
Last modified 1yr ago