ChangeApiConfig
Changes configuration of the CallAnApi Ability the Actor executing this Interaction has been configured with.
Examples:
import { actorCalled } from '@serenity-js/core';
import { Navigate, Target, Text } from '@serenity-js/protractor';
import { CallAnApi, ChangeApiConfig, GetRequest, LastResponse, Send } from '@serenity-js/rest'
import { Ensure, equals } from '@serenity-js/assertions';
import { protractor, by } from 'protractor';
import axios from 'axios';
const actor = actorCalled('Apisitt').whoCan(
BrowseTheWeb.using(protractor.browser),
// Note: no default base URL is given when the axios instance is created
CallAnApi.using(axios.create()),
);
// Let's imagine that the website under test displays
// a dynamically generated API URL we'd like to use
const ApiDetailsWidget = {
Url: Target.the('API URL').located(by.id('api-url')),
}
actor.attemptsTo(
Navigate.to('/profile'),
// We change the API URL based on the text displayed in the widget
// (although we could change it to some arbitrary string too).
ChangeApiConfig.setUrlTo(Text.of(ApiDetailsWidget.Url)),
// Any subsequent request will be sent to the newly set URL
Send.a(GetRequest.to('/projects')),
Ensure.that(LastResponse.status(), equals(200)),
);
import { actorCalled } from '@serenity-js/core';
import { LocalServer, ManageALocalServer, StartLocalServer } from '@serenity-js/local-server';
import { CallAnApi, ChangeApiConfig, GetRequest, LastResponse, Send } from '@serenity-js/rest'
import { Ensure, equals } from '@serenity-js/assertions';
const actor = actorCalled('Apisitt').whoCan(
ManageALocalServer.runningAHttpListener(someServer),
CallAnApi.at('http://localhost'),
);
actor.attemptsTo(
StartALocalServer.onRandomPort(),
ChangeApiConfig.setPortTo(LocalServer.port()),
Send.a(GetRequest.to('/api')),
Ensure.that(LastResponse.status(), equals(200)),
);
import { actorCalled, Question } from '@serenity-js/core';
import { CallAnApi, ChangeApiConfig, GetRequest, LastResponse, Send } from '@serenity-js/rest';
import { Ensure, equals } from '@serenity-js/assertions';
const actor = actorCalled('Apisitt').whoCan(
CallAnApi.at('http://localhost'),
);
// A sample Question reading Node process environment variable
const EnvVar = (var_name: string) =>
Question.about(`${ name } environment variable`, actor => process.env[var_name]);
actor.attemptsTo(
ChangeApiConfig.setHeader('Authorization', EnvVar('TOKEN')),
Send.a(GetRequest.to('/api')),
Ensure.that(LastResponse.status(), equals(200)),
);
Tests:
- ChangeApiConfig
- ChangeApiConfig when changing the API URL changes the base URL used by any subsequent requests
- ChangeApiConfig when changing the API port changes the base URL used by any subsequent requests
- ChangeApiConfig when changing the API port complains if the url has not been set prior to attempted port change
- ChangeApiConfig when changing the API port complains if the url to be changed is invalid
- ChangeApiConfig when setting a request header sets a header to be used by any subsequent requests
- ChangeApiConfig when setting a request header complains if the name of the header is empty
- ChangeApiConfig when setting a request header complains if the name of the header is undefined
Static Method Summary
Static Public Methods | ||
public static |
setHeader(name: Answerable<string>, value: Answerable<string>): Interaction Instructs the Actor to modify the configuration of the AxiosInstance used by CallAnApi Ability and set a HTTP request header for any subsequent HTTPRequest issued via Send. |
|
public static |
setPortTo(newApiPort: Answerable<string>): Interaction |
|
public static |
setUrlTo(newApiUrl: Answerable<string>): Interaction |
Static Public Methods
public static setHeader(name: Answerable<string>, value: Answerable<string>): Interaction source
Instructs the Actor to modify the configuration of the AxiosInstance used by CallAnApi Ability and set a HTTP request header for any subsequent HTTPRequest issued via Send.
Params:
Name | Type | Attribute | Description |
name | Answerable<string> | ||
value | Answerable<string> |
public static setPortTo(newApiPort: Answerable<string>): Interaction source
Params:
Name | Type | Attribute | Description |
newApiPort | Answerable<string> |
Tests:
public static setUrlTo(newApiUrl: Answerable<string>): Interaction source
Params:
Name | Type | Attribute | Description |
newApiUrl | Answerable<string> |