Skip to main content

externalLastResponse

Provides access to the properties of the last AxiosResponse object, cached on the ability to CallAnApi.

Verify response to a GET request

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

Use Serenity/JS adapters to navigate complex response objects

import { actorCalled } from '@serenity-js/core'
import { CallAnApi, GetRequest, LastResponse, Send } from '@serenity-js/rest'
import { Ensure, equals } from '@serenity-js/assertions'

interface Developer {
name: string;
id: string;
projects: Project[];
}

interface Project {
name: string;
repoUrl: string;
}

await actorCalled('Apisitt')
.whoCan(CallAnApi.at('https://api.example.org/'))
.attemptsTo(
Send.a(GetRequest.to('/developers/jan-molak')),
Ensure.that(LastResponse.status(), equals(200)),
Ensure.that(LastResponse.body<Developer>().name, equals('Jan Molak')),
Ensure.that(LastResponse.body<Developer>().projects[0].name, equals('Serenity/JS')),
)

Learn more

Index

Constructors

externalconstructor

Methods

staticexternalstatus

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

  • headers(): QuestionAdapter<Partial<RawAxiosHeaders & { Content-Length: AxiosHeaderValue; Content-Encoding: AxiosHeaderValue; Content-Type: AxiosHeaderValue; Server: AxiosHeaderValue; Cache-Control: AxiosHeaderValue } & { set-cookie: string[] }>>
  • 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[] }>>