externalLastResponse
Index
Constructors
Methods
Constructors
externalconstructor
Returns LastResponse
Methods
staticexternalstatus
Returns QuestionAdapter<number>
staticexternalbody
Retrieves the body of the last response
A type-safe approach using generics
import { actorCalled } from '@serenity-js/core'
import { CallAnApi, LastResponse } 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(
// ...
// note body<T>() parameterised with type "Book"
Ensure.that(LastResponse.body<Book>(), equals({
title: 'Zen and the Art of Motorcycle Maintenance: An Inquiry into Values',
author: 'Robert M. Pirsig',
})),
)A not type-safe approach using
any
import { actorCalled } from '@serenity-js/core'
import { CallAnApi, LastResponse } from '@serenity-js/rest'
import { Ensure, equals } from '@serenity-js/assertions'
await actorCalled('Apisitt')
.whoCan(CallAnApi.at('https://api.example.org/'))
.attemptsTo(
// ...
// note body<T>() parameterised with "any" or with no parameter is not type-safe!
Ensure.that(LastResponse.body<any>(), equals({
title: 'Zen and the Art of Motorcycle Maintenance: An Inquiry into Values',
author: 'Robert M. Pirsig',
})),
)Iterating over the items in the response body
import { actorCalled } from '@serenity-js/core'
import { CallAnApi, LastResponse } from '@serenity-js/rest'
import { Ensure, equals } from '@serenity-js/assertions'
interface Product {
id: number;
name: string;
}
await actorCalled('Apisitt')
.whoCan(CallAnApi.at('https://api.example.org/'))
.attemptsTo(
Send.a(GetRequest.to(`/products`)),
List.of<Product>(LastResponse.body<{ products: Product[] }>().products)
.forEach(({ item, actor }) =>
actor.attemptsTo(
Send.a(GetRequest.to(`/products/${ item.id }`)),
Ensure.that(LastResponse.body<Product>().id, equals(item.id)),
)
),
)Type parameters
- T = any
Returns QuestionAdapter<T>
staticexternalheader
Retrieves a header of the last response, identified by
name
Asserting on a header
import { actorCalled } from '@serenity-js/core'
import { CallAnApi, LastResponse } 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(`/products`)),
Ensure.that(LastResponse.header('Content-Type'), equals('application/json')),
)Parameters
externalname: string
Returns QuestionAdapter<string>
staticexternalheaders
Retrieves all the headers of the last response.
Asserting on a header
import { actorCalled } from '@serenity-js/core'
import { CallAnApi, LastResponse } 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(`/products`)),
Ensure.that(LastResponse.headers()['Content-Type'], equals('application/json')),
)Returns QuestionAdapter<Partial<RawAxiosHeaders & { Content-Length: AxiosHeaderValue; Content-Encoding: AxiosHeaderValue; Content-Type: AxiosHeaderValue; Server: AxiosHeaderValue; Cache-Control: AxiosHeaderValue } & { set-cookie: string[] }>>
Provides access to the properties of the last
AxiosResponse
object, cached on the ability toCallAnApi
.Verify response to a GET request
Use Serenity/JS adapters to navigate complex response objects
Learn more