Verifying responses
With Serenity/JS, you can verify responses to your API requests using LastResponse
and the exact same Serenity/JS assertions
module you'd use for other kinds of test scenarios.
Check the LastResponse
documentation for examples on how to extract interesting information from the response:
Verifying the response status​
To extract the response status, instruct the actor to Send
a HTTPRequest
and use LastResponse.status()
.
import { actorCalled } from '@serenity-js/core'
import { CallAnApi, GetRequest, LastResponse, Send } from '@serenity-js/rest'
import { Ensure, equals } from '@serenity-js/assertions'
await actorCalled('Apisitt')
.whoCan(CallAnApi.at('https://api.example.org/'))
.attemptsTo(
Send.a(GetRequest.to('/books/0-688-00230-7')),
Ensure.that(LastResponse.status(), equals(200)),
)
Verifying the response body​
To extract the response body, instruct the actor to Send
a HTTPRequest
and use LastResponse.body<T>()
.
Here T
is an optional generic parameter describing the shape of the response body.
import { actorCalled } from '@serenity-js/core'
import { CallAnApi, GetRequest, LastResponse, Send } from '@serenity-js/rest'
import { Ensure, equals } from '@serenity-js/assertions'
interface Book {
title: string;
author: string;
}
await actorCalled('Apisitt')
.whoCan(CallAnApi.at('https://api.example.org/'))
.attemptsTo(
Send.a(GetRequest.to('/books/0-688-00230-7')),
Ensure.that(LastResponse.status(), equals(200)),
Ensure.that(LastResponse.header('Content-Type'), equals('application/json')),
Ensure.that(LastResponse.body<Book>(), equals({
title: 'Zen and the Art of Motorcycle Maintenance: An Inquiry into Values',
author: 'Robert M. Pirsig',
})),
)