LastResponse
Provides access to the properties of the last AxiosResponse object, cached on the CallAnApi Ability.
Examples:
Verify response to a GET request
import { Actor } 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
}
const actor = Actor.named('Apisit').whoCan(CallAnApi.at('https://myapp.com/api'));
actor.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',
})),
);
Tests:
- LastResponse
- LastResponse when asserting on the response to the last HTTP request enables access to the response status
- LastResponse when asserting on the response to the last HTTP request enables access to the response body
- LastResponse when asserting on the response to the last HTTP request enables access to a specific response header
- LastResponse when asserting on the response to the last HTTP request enables access to all response headers
Static Method Summary
Static Public Methods | ||
public static |
Enables asserting on the LastResponse body |
|
public static |
Enables asserting on one of the LastResponse's headers |
|
public static |
Enables asserting on all of the LastResponse's headers, returned as an object where the keys represent header names. |
|
public static |
Enables asserting on the LastResponse status |
Static Public Methods
public static body(): Question<any> source
Enables asserting on the LastResponse body
Examples:
A type-safe approach using generics
interface Book {
title: string;
author: string
}
actor.attemptsTo(
// ...
Ensure.that(LastResponse.body<Book>(), equals({
title: 'Zen and the Art of Motorcycle Maintenance: An Inquiry into Values',
author: 'Robert M. Pirsig',
})),
);
A non-type-safe approach using `any`
actor.attemptsTo(
// ...
Ensure.that(LastResponse.body<any>(), equals({
title: 'Zen and the Art of Motorcycle Maintenance: An Inquiry into Values',
author: 'Robert M. Pirsig',
})),
);
public static header(name: string): Question<string> source
Enables asserting on one of the LastResponse's headers
Params:
Name | Type | Attribute | Description |
name | string |
public static headers(): Question<object> source
Enables asserting on all of the LastResponse's headers, returned as an object where the keys represent header names.