import {LastResponse} from '@serenity-js/rest/lib/screenplay/questions'
public class | source

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',
     })),
 );

Static Method Summary

Static Public Methods
public static

body(): Question<any>

Enables asserting on the LastResponse body

public static

header(name: string): Question<string>

Enables asserting on one of the LastResponse's headers

public static

headers(): Question<object>

Enables asserting on all of the LastResponse's headers, returned as an object where the keys represent header names.

public static

status(): Question<number>

Enables asserting on the LastResponse status

Static Public Methods

public static body(): Question<any> source

Enables asserting on the LastResponse body

Returns:

Question<any>

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',
     })),
 );
Retrieving an item at path using Property.of
 import { Property } from '@serenity-js/core';

 actor.attemptsTo(
     Ensure.that(
         Property.of(LastResponse.body<Book>()).title,
         equals('Zen and the Art of Motorcycle Maintenance: An Inquiry into Values'),
     )
 )
Filtering response body using List
 import { Property } from '@serenity-js/core';
 import { property, startsWith } from '@serenity-js/assertions';

 actor.attemptsTo(
     Ensure.that(
         // imagine the API returns an array of books
         List.of(LastResponse.body<Book[]>())
             .where(Property.at<Book>().author, equals('Robert M. Pirsig'))
             .first(),
         property('title', startsWith('Zen and the Art of Motorcycle Maintenance')),
     )
 )

public static header(name: string): Question<string> source

Enables asserting on one of the LastResponse's headers

Params:

NameTypeAttributeDescription
name string

Returns:

Question<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.

Returns:

Question<object>