@serenity-js/rest 3.41.2 → 3.42.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/esm/index.d.ts +3 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +3 -0
- package/esm/index.js.map +1 -0
- package/esm/io/AxiosRequestConfigDefaults.d.ts +15 -0
- package/esm/io/AxiosRequestConfigDefaults.d.ts.map +1 -0
- package/esm/io/AxiosRequestConfigDefaults.js +2 -0
- package/esm/io/AxiosRequestConfigDefaults.js.map +1 -0
- package/esm/io/EnvironmentVariables.d.ts +9 -0
- package/esm/io/EnvironmentVariables.d.ts.map +1 -0
- package/esm/io/EnvironmentVariables.js +35 -0
- package/esm/io/EnvironmentVariables.js.map +1 -0
- package/esm/io/ProxyAgent.d.ts +52 -0
- package/esm/io/ProxyAgent.d.ts.map +1 -0
- package/esm/io/ProxyAgent.js +83 -0
- package/esm/io/ProxyAgent.js.map +1 -0
- package/esm/io/ProxyBypass.d.ts +6 -0
- package/esm/io/ProxyBypass.d.ts.map +1 -0
- package/esm/io/ProxyBypass.js +66 -0
- package/esm/io/ProxyBypass.js.map +1 -0
- package/esm/io/createAxios.d.ts +9 -0
- package/esm/io/createAxios.d.ts.map +1 -0
- package/esm/io/createAxios.js +40 -0
- package/esm/io/createAxios.js.map +1 -0
- package/esm/io/createUrl.d.ts +9 -0
- package/esm/io/createUrl.d.ts.map +1 -0
- package/esm/io/createUrl.js +26 -0
- package/esm/io/createUrl.js.map +1 -0
- package/esm/io/index.d.ts +3 -0
- package/esm/io/index.d.ts.map +1 -0
- package/esm/io/index.js +3 -0
- package/esm/io/index.js.map +1 -0
- package/esm/io/proxy.d.ts +14 -0
- package/esm/io/proxy.d.ts.map +1 -0
- package/esm/io/proxy.js +66 -0
- package/esm/io/proxy.js.map +1 -0
- package/esm/screenplay/abilities/AxiosRequestConfigDefaults.d.ts +14 -0
- package/esm/screenplay/abilities/AxiosRequestConfigDefaults.d.ts.map +1 -0
- package/esm/screenplay/abilities/AxiosRequestConfigDefaults.js +2 -0
- package/esm/screenplay/abilities/AxiosRequestConfigDefaults.js.map +1 -0
- package/esm/screenplay/abilities/CallAnApi.d.ts +443 -0
- package/esm/screenplay/abilities/CallAnApi.d.ts.map +1 -0
- package/esm/screenplay/abilities/CallAnApi.js +555 -0
- package/esm/screenplay/abilities/CallAnApi.js.map +1 -0
- package/esm/screenplay/abilities/index.d.ts +2 -0
- package/esm/screenplay/abilities/index.d.ts.map +1 -0
- package/esm/screenplay/abilities/index.js +2 -0
- package/esm/screenplay/abilities/index.js.map +1 -0
- package/esm/screenplay/index.d.ts +5 -0
- package/esm/screenplay/index.d.ts.map +1 -0
- package/esm/screenplay/index.js +5 -0
- package/esm/screenplay/index.js.map +1 -0
- package/esm/screenplay/interactions/ChangeApiConfig.d.ts +135 -0
- package/esm/screenplay/interactions/ChangeApiConfig.d.ts.map +1 -0
- package/esm/screenplay/interactions/ChangeApiConfig.js +212 -0
- package/esm/screenplay/interactions/ChangeApiConfig.js.map +1 -0
- package/esm/screenplay/interactions/Send.d.ts +48 -0
- package/esm/screenplay/interactions/Send.d.ts.map +1 -0
- package/esm/screenplay/interactions/Send.js +83 -0
- package/esm/screenplay/interactions/Send.js.map +1 -0
- package/esm/screenplay/interactions/index.d.ts +3 -0
- package/esm/screenplay/interactions/index.d.ts.map +1 -0
- package/esm/screenplay/interactions/index.js +3 -0
- package/esm/screenplay/interactions/index.js.map +1 -0
- package/esm/screenplay/models/DeleteRequest.d.ts +66 -0
- package/esm/screenplay/models/DeleteRequest.d.ts.map +1 -0
- package/esm/screenplay/models/DeleteRequest.js +69 -0
- package/esm/screenplay/models/DeleteRequest.js.map +1 -0
- package/esm/screenplay/models/GetRequest.d.ts +73 -0
- package/esm/screenplay/models/GetRequest.d.ts.map +1 -0
- package/esm/screenplay/models/GetRequest.js +76 -0
- package/esm/screenplay/models/GetRequest.js.map +1 -0
- package/esm/screenplay/models/HTTPRequest.d.ts +40 -0
- package/esm/screenplay/models/HTTPRequest.d.ts.map +1 -0
- package/esm/screenplay/models/HTTPRequest.js +62 -0
- package/esm/screenplay/models/HTTPRequest.js.map +1 -0
- package/esm/screenplay/models/HeadRequest.d.ts +58 -0
- package/esm/screenplay/models/HeadRequest.d.ts.map +1 -0
- package/esm/screenplay/models/HeadRequest.js +61 -0
- package/esm/screenplay/models/HeadRequest.js.map +1 -0
- package/esm/screenplay/models/OptionsRequest.d.ts +61 -0
- package/esm/screenplay/models/OptionsRequest.d.ts.map +1 -0
- package/esm/screenplay/models/OptionsRequest.js +64 -0
- package/esm/screenplay/models/OptionsRequest.js.map +1 -0
- package/esm/screenplay/models/PatchRequest.d.ts +65 -0
- package/esm/screenplay/models/PatchRequest.d.ts.map +1 -0
- package/esm/screenplay/models/PatchRequest.js +70 -0
- package/esm/screenplay/models/PatchRequest.js.map +1 -0
- package/esm/screenplay/models/PostRequest.d.ts +105 -0
- package/esm/screenplay/models/PostRequest.d.ts.map +1 -0
- package/esm/screenplay/models/PostRequest.js +110 -0
- package/esm/screenplay/models/PostRequest.js.map +1 -0
- package/esm/screenplay/models/PutRequest.d.ts +75 -0
- package/esm/screenplay/models/PutRequest.d.ts.map +1 -0
- package/esm/screenplay/models/PutRequest.js +80 -0
- package/esm/screenplay/models/PutRequest.js.map +1 -0
- package/esm/screenplay/models/index.d.ts +9 -0
- package/esm/screenplay/models/index.d.ts.map +1 -0
- package/esm/screenplay/models/index.js +9 -0
- package/esm/screenplay/models/index.js.map +1 -0
- package/esm/screenplay/questions/LastResponse.d.ts +189 -0
- package/esm/screenplay/questions/LastResponse.d.ts.map +1 -0
- package/esm/screenplay/questions/LastResponse.js +205 -0
- package/esm/screenplay/questions/LastResponse.js.map +1 -0
- package/esm/screenplay/questions/index.d.ts +2 -0
- package/esm/screenplay/questions/index.d.ts.map +1 -0
- package/esm/screenplay/questions/index.js +2 -0
- package/esm/screenplay/questions/index.js.map +1 -0
- package/lib/index.d.ts +2 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -2
- package/lib/index.js.map +1 -1
- package/lib/io/ProxyBypass.js +2 -2
- package/lib/io/ProxyBypass.js.map +1 -1
- package/lib/io/createAxios.d.ts +1 -1
- package/lib/io/createAxios.d.ts.map +1 -1
- package/lib/io/createAxios.js +2 -2
- package/lib/io/createAxios.js.map +1 -1
- package/lib/io/index.d.ts +2 -2
- package/lib/io/index.d.ts.map +1 -1
- package/lib/io/index.js +2 -2
- package/lib/io/index.js.map +1 -1
- package/lib/io/proxy.d.ts +2 -2
- package/lib/io/proxy.d.ts.map +1 -1
- package/lib/io/proxy.js +9 -9
- package/lib/io/proxy.js.map +1 -1
- package/lib/package.json +1 -0
- package/lib/screenplay/abilities/CallAnApi.d.ts +1 -1
- package/lib/screenplay/abilities/CallAnApi.d.ts.map +1 -1
- package/lib/screenplay/abilities/CallAnApi.js +4 -4
- package/lib/screenplay/abilities/CallAnApi.js.map +1 -1
- package/lib/screenplay/abilities/index.d.ts +1 -1
- package/lib/screenplay/abilities/index.d.ts.map +1 -1
- package/lib/screenplay/abilities/index.js +1 -1
- package/lib/screenplay/abilities/index.js.map +1 -1
- package/lib/screenplay/index.d.ts +4 -4
- package/lib/screenplay/index.d.ts.map +1 -1
- package/lib/screenplay/index.js +4 -4
- package/lib/screenplay/index.js.map +1 -1
- package/lib/screenplay/interactions/ChangeApiConfig.js +4 -4
- package/lib/screenplay/interactions/ChangeApiConfig.js.map +1 -1
- package/lib/screenplay/interactions/Send.js +3 -3
- package/lib/screenplay/interactions/Send.js.map +1 -1
- package/lib/screenplay/interactions/index.d.ts +2 -2
- package/lib/screenplay/interactions/index.d.ts.map +1 -1
- package/lib/screenplay/interactions/index.js +2 -2
- package/lib/screenplay/interactions/index.js.map +1 -1
- package/lib/screenplay/models/DeleteRequest.d.ts +1 -1
- package/lib/screenplay/models/DeleteRequest.d.ts.map +1 -1
- package/lib/screenplay/models/DeleteRequest.js +2 -2
- package/lib/screenplay/models/DeleteRequest.js.map +1 -1
- package/lib/screenplay/models/GetRequest.d.ts +1 -1
- package/lib/screenplay/models/GetRequest.d.ts.map +1 -1
- package/lib/screenplay/models/GetRequest.js +2 -2
- package/lib/screenplay/models/GetRequest.js.map +1 -1
- package/lib/screenplay/models/HTTPRequest.js +1 -1
- package/lib/screenplay/models/HTTPRequest.js.map +1 -1
- package/lib/screenplay/models/HeadRequest.d.ts +1 -1
- package/lib/screenplay/models/HeadRequest.d.ts.map +1 -1
- package/lib/screenplay/models/HeadRequest.js +2 -2
- package/lib/screenplay/models/HeadRequest.js.map +1 -1
- package/lib/screenplay/models/OptionsRequest.d.ts +1 -1
- package/lib/screenplay/models/OptionsRequest.d.ts.map +1 -1
- package/lib/screenplay/models/OptionsRequest.js +2 -2
- package/lib/screenplay/models/OptionsRequest.js.map +1 -1
- package/lib/screenplay/models/PatchRequest.d.ts +1 -1
- package/lib/screenplay/models/PatchRequest.d.ts.map +1 -1
- package/lib/screenplay/models/PatchRequest.js +2 -2
- package/lib/screenplay/models/PatchRequest.js.map +1 -1
- package/lib/screenplay/models/PostRequest.d.ts +1 -1
- package/lib/screenplay/models/PostRequest.d.ts.map +1 -1
- package/lib/screenplay/models/PostRequest.js +2 -2
- package/lib/screenplay/models/PostRequest.js.map +1 -1
- package/lib/screenplay/models/PutRequest.d.ts +1 -1
- package/lib/screenplay/models/PutRequest.d.ts.map +1 -1
- package/lib/screenplay/models/PutRequest.js +2 -2
- package/lib/screenplay/models/PutRequest.js.map +1 -1
- package/lib/screenplay/models/index.d.ts +8 -8
- package/lib/screenplay/models/index.d.ts.map +1 -1
- package/lib/screenplay/models/index.js +8 -8
- package/lib/screenplay/models/index.js.map +1 -1
- package/lib/screenplay/questions/LastResponse.js +5 -5
- package/lib/screenplay/questions/LastResponse.js.map +1 -1
- package/lib/screenplay/questions/index.d.ts +1 -1
- package/lib/screenplay/questions/index.d.ts.map +1 -1
- package/lib/screenplay/questions/index.js +1 -1
- package/lib/screenplay/questions/index.js.map +1 -1
- package/package.json +28 -10
- package/src/index.ts +2 -2
- package/src/io/ProxyBypass.ts +1 -1
- package/src/io/createAxios.ts +2 -2
- package/src/io/index.ts +2 -2
- package/src/io/proxy.ts +5 -5
- package/src/screenplay/abilities/CallAnApi.ts +3 -3
- package/src/screenplay/abilities/index.ts +1 -1
- package/src/screenplay/index.ts +4 -4
- package/src/screenplay/interactions/ChangeApiConfig.ts +1 -1
- package/src/screenplay/interactions/Send.ts +3 -3
- package/src/screenplay/interactions/index.ts +2 -2
- package/src/screenplay/models/DeleteRequest.ts +1 -1
- package/src/screenplay/models/GetRequest.ts +1 -1
- package/src/screenplay/models/HTTPRequest.ts +1 -1
- package/src/screenplay/models/HeadRequest.ts +1 -1
- package/src/screenplay/models/OptionsRequest.ts +1 -1
- package/src/screenplay/models/PatchRequest.ts +1 -1
- package/src/screenplay/models/PostRequest.ts +1 -1
- package/src/screenplay/models/PutRequest.ts +1 -1
- package/src/screenplay/models/index.ts +8 -8
- package/src/screenplay/questions/LastResponse.ts +1 -1
- package/src/screenplay/questions/index.ts +1 -1
- package/tsconfig-cjs.build.json +17 -0
- package/tsconfig-esm.build.json +18 -0
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
import { URL } from 'node:url';
|
|
2
|
+
import { Ability, type SerialisedAbility } from '@serenity-js/core';
|
|
3
|
+
import { type AxiosDefaults, type AxiosInstance, type AxiosRequestConfig, type AxiosResponse } from 'axios';
|
|
4
|
+
import type { AxiosRequestConfigDefaults } from '../../io/index.js';
|
|
5
|
+
/**
|
|
6
|
+
* An [ability](https://serenity-js.org/api/core/class/Ability/) that wraps [axios client](https://axios-http.com/docs/api_intro) and enables
|
|
7
|
+
* the [actor](https://serenity-js.org/api/core/class/Actor/) to [send](https://serenity-js.org/api/rest/class/Send/)
|
|
8
|
+
* [HTTP requests](https://serenity-js.org/api/rest/class/HTTPRequest/) to HTTP APIs.
|
|
9
|
+
*
|
|
10
|
+
* ## Configuring the ability to call an API
|
|
11
|
+
*
|
|
12
|
+
* The easiest way to configure the ability to `CallAnApi` is to provide the `baseURL` of your HTTP API,
|
|
13
|
+
* and rely on Serenity/JS to offer other sensible defaults:
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { actorCalled } from '@serenity-js/core'
|
|
17
|
+
* import { CallAnApi, GetRequest, LastResponse, Send } from '@serenity-js/rest'
|
|
18
|
+
* import { Ensure, equals } from '@serenity-js/assertions'
|
|
19
|
+
*
|
|
20
|
+
* await actorCalled('Apisitt')
|
|
21
|
+
* .whoCan(
|
|
22
|
+
* CallAnApi.at('https://api.example.org/')
|
|
23
|
+
* )
|
|
24
|
+
* .attemptsTo(
|
|
25
|
+
* Send.a(GetRequest.to('/v1/users/2')), // GET https://api.example.org/v1/users/2
|
|
26
|
+
* Ensure.that(LastResponse.status(), equals(200)),
|
|
27
|
+
* )
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* Please note that the following Serenity/JS test runner adapters already provide the ability to `CallAnApi` as part of their default configuration,
|
|
31
|
+
* so you don't need to configure it yourself:
|
|
32
|
+
* - [Playwright Test](https://serenity-js.org/handbook/test-runners/playwright-test/)
|
|
33
|
+
* - [WebdriverIO](https://serenity-js.org/handbook/test-runners/webdriverio/)
|
|
34
|
+
* - [Protractor](https://serenity-js.org/handbook/test-runners/protractor/)
|
|
35
|
+
*
|
|
36
|
+
* ### Resolving relative URLs
|
|
37
|
+
*
|
|
38
|
+
* Serenity/JS resolves request URLs using Node.js [WHATWG URL API](https://nodejs.org/api/url.html#new-urlinput-base).
|
|
39
|
+
* This means that the request URL is determined using the resource path resolved in the context of base URL, i.e. `new URL(resourcePath, [baseURL])`.
|
|
40
|
+
*
|
|
41
|
+
* Consider the following example:
|
|
42
|
+
*
|
|
43
|
+
* ```ts
|
|
44
|
+
* import { actorCalled } from '@serenity-js/core'
|
|
45
|
+
* import { CallAnApi, GetRequest, LastResponse, Send } from '@serenity-js/rest'
|
|
46
|
+
* import { Ensure, equals } from '@serenity-js/assertions'
|
|
47
|
+
*
|
|
48
|
+
* await actorCalled('Apisitt')
|
|
49
|
+
* .whoCan(
|
|
50
|
+
* CallAnApi.at(baseURL)
|
|
51
|
+
* )
|
|
52
|
+
* .attemptsTo(
|
|
53
|
+
* Send.a(GetRequest.to(resourcePath)),
|
|
54
|
+
* Ensure.that(LastResponse.status(), equals(200)),
|
|
55
|
+
* )
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* In the above example:
|
|
59
|
+
* - when `resourcePath` is defined as a full URL, it overrides the base URL
|
|
60
|
+
* - when `resourcePath` starts with a forward slash `/`, it replaces any path defined in the base URL
|
|
61
|
+
* - when `resourcePath` is not a full URL and doesn't start with a forward slash, it gets appended to the base URL
|
|
62
|
+
*
|
|
63
|
+
* | baseURL | resourcePath | result |
|
|
64
|
+
* | ----------------------------- | -------------------------- | ------------------------------------ |
|
|
65
|
+
* | `https://api.example.org/` | `/v1/users/2` | `https://api.example.org/v1/users/2` |
|
|
66
|
+
* | `https://example.org/api/v1/` | `users/2` | `https://example.org/api/v1/users/2` |
|
|
67
|
+
* | `https://example.org/api/v1/` | `/secure/oauth` | `https://example.org/secure/oauth` |
|
|
68
|
+
* | `https://v1.example.org/api/` | `https://v2.example.org/` | `https://v2.example.org/` |
|
|
69
|
+
*
|
|
70
|
+
* ### Using Axios configuration object
|
|
71
|
+
*
|
|
72
|
+
* When you need more control over how your Axios instance is configured, provide
|
|
73
|
+
* an [Axios configuration object](https://axios-http.com/docs/req_config). For example:
|
|
74
|
+
*
|
|
75
|
+
* ```ts
|
|
76
|
+
* import { actorCalled } from '@serenity-js/core'
|
|
77
|
+
* import { CallAnApi, GetRequest, LastResponse, Send } from '@serenity-js/rest'
|
|
78
|
+
* import { Ensure, equals } from '@serenity-js/assertions'
|
|
79
|
+
*
|
|
80
|
+
* await actorCalled('Apisitt')
|
|
81
|
+
* .whoCan(
|
|
82
|
+
* CallAnApi.using({
|
|
83
|
+
* baseURL: 'https://api.example.org/',
|
|
84
|
+
* timeout: 30_000,
|
|
85
|
+
* // ... other configuration options
|
|
86
|
+
* })
|
|
87
|
+
* )
|
|
88
|
+
* .attemptsTo(
|
|
89
|
+
* Send.a(GetRequest.to('/users/2')),
|
|
90
|
+
* Ensure.that(LastResponse.status(), equals(200)),
|
|
91
|
+
* )
|
|
92
|
+
* ```
|
|
93
|
+
*
|
|
94
|
+
* ## Working with proxy servers
|
|
95
|
+
*
|
|
96
|
+
* `CallAnApi` uses an approach described in ["Node.js Axios behind corporate proxies"](https://janmolak.com/node-js-axios-behind-corporate-proxies-8b17a6f31f9d)
|
|
97
|
+
* to automatically detect proxy server configuration based on your environment variables.
|
|
98
|
+
*
|
|
99
|
+
* You can override this default proxy detection mechanism by providing your own proxy configuration object.
|
|
100
|
+
*
|
|
101
|
+
* ### Automatic proxy support
|
|
102
|
+
*
|
|
103
|
+
* When the URL you're sending the request to uses the HTTP protocol, Serenity/JS will automatically detect your proxy configuration based on the following environment variables:
|
|
104
|
+
* - `npm_config_http_proxy`
|
|
105
|
+
* - `http_proxy` and `HTTP_PROXY`
|
|
106
|
+
* - `npm_config_proxy`
|
|
107
|
+
* - `all_proxy`
|
|
108
|
+
*
|
|
109
|
+
* Similarly, when the request target URL uses the HTTPS protocol, the following environment variables are used instead:
|
|
110
|
+
* - `npm_config_https_proxy`
|
|
111
|
+
* - `https_proxy` and `HTTPS_PROXY`
|
|
112
|
+
* - `npm_config_proxy`
|
|
113
|
+
* - `all_proxy`
|
|
114
|
+
*
|
|
115
|
+
* Proxy configuration is ignored for both HTTP and HTTPS target URLs matching the proxy bypass rules defined in the following environment variables:
|
|
116
|
+
* - `npm_config_no_proxy`
|
|
117
|
+
* - `no_proxy` and `NO_PROXY`
|
|
118
|
+
*
|
|
119
|
+
* ### Custom proxy configuration
|
|
120
|
+
*
|
|
121
|
+
* To override the automatic proxy detection based on the environment variables, provide a proxy configuration object:
|
|
122
|
+
*
|
|
123
|
+
* ```ts
|
|
124
|
+
* import { actorCalled } from '@serenity-js/core'
|
|
125
|
+
* import { CallAnApi, GetRequest, LastResponse, Send } from '@serenity-js/rest'
|
|
126
|
+
* import { Ensure, equals } from '@serenity-js/assertions'
|
|
127
|
+
*
|
|
128
|
+
* await actorCalled('Apisitt')
|
|
129
|
+
* .whoCan(
|
|
130
|
+
* CallAnApi.using({
|
|
131
|
+
* baseURL: 'https://api.example.org/',
|
|
132
|
+
* proxy: {
|
|
133
|
+
* protocol: 'http',
|
|
134
|
+
* host: 'proxy.example.org',
|
|
135
|
+
* port: 9000,
|
|
136
|
+
* auth: { // optional
|
|
137
|
+
* username: 'proxy-username',
|
|
138
|
+
* password: 'proxy-password',
|
|
139
|
+
* },
|
|
140
|
+
* bypass: 'status.example.org, internal.example.org' // optional
|
|
141
|
+
* }
|
|
142
|
+
* // ... other configuration options
|
|
143
|
+
* })
|
|
144
|
+
* )
|
|
145
|
+
* .attemptsTo(
|
|
146
|
+
* Send.a(GetRequest.to('/users/2')),
|
|
147
|
+
* Ensure.that(LastResponse.status(), equals(200)),
|
|
148
|
+
* )
|
|
149
|
+
* ```
|
|
150
|
+
*
|
|
151
|
+
* Note that Serenity/JS uses [proxy-agents](https://github.com/TooTallNate/proxy-agents)
|
|
152
|
+
* and the approach described in ["Node.js Axios behind corporate proxies"](https://janmolak.com/node-js-axios-behind-corporate-proxies-8b17a6f31f9d)
|
|
153
|
+
* to work around [limited proxy support capabilities](https://github.com/axios/axios/issues?q=is%3Aissue+is%3Aopen+proxy) in Axios itself.
|
|
154
|
+
*
|
|
155
|
+
* ### Bypassing proxy configuration
|
|
156
|
+
*
|
|
157
|
+
* To bypass the proxy configuration for specific hostnames and IP addresses, you can either:
|
|
158
|
+
* - provide the `bypass` property in the proxy configuration object, or
|
|
159
|
+
* - use the `no_proxy` environment variable.
|
|
160
|
+
*
|
|
161
|
+
* The value of the `bypass` property or the `no_proxy` environment variable should be a comma-separated list of hostnames and IP addresses
|
|
162
|
+
* that should not be routed through the proxy server, for example: `.com, .serenity-js.org, .domain.com`.
|
|
163
|
+
*
|
|
164
|
+
* Note that setting the `bypass` property to `example.org` makes the requests to following URLs bypass the proxy server:
|
|
165
|
+
* - `api.example.org`
|
|
166
|
+
* - `sub.sub.example.org`
|
|
167
|
+
* - `example.org`
|
|
168
|
+
* - `my-example.org`
|
|
169
|
+
*
|
|
170
|
+
* :::info
|
|
171
|
+
* Serenity/JS doesn't currently support `bypass` rules expressed using CIDR notation, like `192.168.17.0/24`.
|
|
172
|
+
* Instead, it uses a simple comma-separated list of hostnames and IP addresses.
|
|
173
|
+
* If you need support for CIDR notation, please [raise an issue](https://github.com/serenity-js/serenity-js/issues).
|
|
174
|
+
* :::
|
|
175
|
+
*
|
|
176
|
+
* ### Using Axios instance with proxy support
|
|
177
|
+
*
|
|
178
|
+
* To have full control over the Axios instance used by the ability to `CallAnApi`, you can create it yourself
|
|
179
|
+
* and inject it into the ability.
|
|
180
|
+
* This approach allows you to still benefit from automated proxy detection in configuration, while taking advantage
|
|
181
|
+
* of the many [Axios plugins](https://www.npmjs.com/search?q=axios).
|
|
182
|
+
*
|
|
183
|
+
* ```ts
|
|
184
|
+
* import { actorCalled } from '@serenity-js/core'
|
|
185
|
+
* import { createAxios, CallAnApi, GetRequest, LastResponse, Send } from '@serenity-js/rest'
|
|
186
|
+
* import { Ensure, equals } from '@serenity-js/assertions'
|
|
187
|
+
*
|
|
188
|
+
* import axiosRetry from 'axios-retry'
|
|
189
|
+
*
|
|
190
|
+
* const instance = createAxios({ baseURL: 'https://api.example.org/' })
|
|
191
|
+
* axiosRetry(instance, { retries: 3 })
|
|
192
|
+
*
|
|
193
|
+
* await actorCalled('Apisitt')
|
|
194
|
+
* .whoCan(
|
|
195
|
+
* CallAnApi.using(instance)
|
|
196
|
+
* )
|
|
197
|
+
* .attemptsTo(
|
|
198
|
+
* Send.a(GetRequest.to('/users/2')),
|
|
199
|
+
* Ensure.that(LastResponse.status(), equals(200)),
|
|
200
|
+
* )
|
|
201
|
+
* ```
|
|
202
|
+
*
|
|
203
|
+
* ### Using raw Axios instance
|
|
204
|
+
*
|
|
205
|
+
* If you don't want Serenity/JS to enhance your Axios instance with proxy support, instantiate the ability to
|
|
206
|
+
* `CallAnApi` using its constructor directly.
|
|
207
|
+
* Note, however, that by using this approach you're taking the responsibility for all the aspects of configuring Axios.
|
|
208
|
+
*
|
|
209
|
+
* ```ts
|
|
210
|
+
* import { actorCalled } from '@serenity-js/core'
|
|
211
|
+
* import { CallAnApi, GetRequest, LastResponse, Send } from '@serenity-js/rest'
|
|
212
|
+
* import { Ensure, equals } from '@serenity-js/assertions'
|
|
213
|
+
*
|
|
214
|
+
* import { axiosCreate } from '@serenity-js/rest'
|
|
215
|
+
* import axiosRetry from 'axios-retry'
|
|
216
|
+
*
|
|
217
|
+
* const instance = axiosCreate({ baseURL: 'https://api.example.org/' })
|
|
218
|
+
* axiosRetry(instance, { retries: 3 })
|
|
219
|
+
*
|
|
220
|
+
* await actorCalled('Apisitt')
|
|
221
|
+
* .whoCan(
|
|
222
|
+
* new CallAnApi(instance) // using the constructor ensures your axios instance is not modified in any way.
|
|
223
|
+
* )
|
|
224
|
+
* .attemptsTo(
|
|
225
|
+
* // ...
|
|
226
|
+
* )
|
|
227
|
+
* ```
|
|
228
|
+
*
|
|
229
|
+
* ### Serenity/JS defaults
|
|
230
|
+
*
|
|
231
|
+
* When using [`CallAnApi.at`](https://serenity-js.org/api/rest/class/CallAnApi/#at) or [`CallAnApi.using`](https://serenity-js.org/api/rest/class/CallAnApi/#using) with a configuration object, Serenity/JS
|
|
232
|
+
* merges your [Axios request configuration](https://axios-http.com/docs/req_config) with the following defaults:
|
|
233
|
+
* - `timeout`: 10 seconds
|
|
234
|
+
*
|
|
235
|
+
*
|
|
236
|
+
* You can override them by specifying the given property in your configuration object, for example:
|
|
237
|
+
* ```ts
|
|
238
|
+
* import { actorCalled } from '@serenity-js/core'
|
|
239
|
+
* import { CallAnApi, GetRequest, LastResponse, Send } from '@serenity-js/rest'
|
|
240
|
+
* import { Ensure, equals } from '@serenity-js/assertions'
|
|
241
|
+
*
|
|
242
|
+
* await actorCalled('Apisitt')
|
|
243
|
+
* .whoCan(
|
|
244
|
+
* CallAnApi.using({
|
|
245
|
+
* baseURL: 'https://api.example.org/',
|
|
246
|
+
* timeout: 30_000
|
|
247
|
+
* })
|
|
248
|
+
* )
|
|
249
|
+
* .attemptsTo(
|
|
250
|
+
* Send.a(GetRequest.to('/users/2')),
|
|
251
|
+
* Ensure.that(LastResponse.status(), equals(200)),
|
|
252
|
+
* )
|
|
253
|
+
* ```
|
|
254
|
+
*
|
|
255
|
+
* ## Interacting with multiple APIs
|
|
256
|
+
*
|
|
257
|
+
* Some test scenarios might require you to interact with multiple HTTP APIs. With Serenity/JS you can do this
|
|
258
|
+
* using either API-specific actors, or by specifying full URLs when performing the requests.
|
|
259
|
+
*
|
|
260
|
+
* The following examples will assume that the test scenarios needs to interact with the following APIs:
|
|
261
|
+
* - `https://testdata.example.org/api/v1/`
|
|
262
|
+
* - `https://shop.example.org/api/v1/`
|
|
263
|
+
*
|
|
264
|
+
* Let's also assume that the `testdata` API allows the automation to manage the test data used by the `shop` API.
|
|
265
|
+
*
|
|
266
|
+
* ### Using API-specific actors
|
|
267
|
+
*
|
|
268
|
+
* To create API-specific actors, configure your [test runner](https://serenity-js.org/handbook/test-runners/) with a [cast](https://serenity-js.org/api/core/class/Cast/)
|
|
269
|
+
* that gives your actors appropriate abilities based, for example, on their name:
|
|
270
|
+
*
|
|
271
|
+
* ```ts
|
|
272
|
+
* import { beforeEach } from 'mocha'
|
|
273
|
+
* import { Actor, Cast, engage } from '@serenity-js/core'
|
|
274
|
+
* import { CallAnApi } from '@serenity-js/rest'
|
|
275
|
+
*
|
|
276
|
+
* export class MyActors implements Cast {
|
|
277
|
+
* prepare(actor: Actor): Actor {
|
|
278
|
+
* switch(actor.name) {
|
|
279
|
+
* case 'Ted':
|
|
280
|
+
* return actor.whoCan(CallAnApi.at('https://testdata.example.org/api/v1/'))
|
|
281
|
+
* case 'Shelly':
|
|
282
|
+
* return actor.whoCan(CallAnApi.at('https://shop.example.org/api/v1/'))
|
|
283
|
+
* default:
|
|
284
|
+
* return actor;
|
|
285
|
+
* }
|
|
286
|
+
* }
|
|
287
|
+
* }
|
|
288
|
+
*
|
|
289
|
+
* beforeEach(() => engage(new MyActors()))
|
|
290
|
+
* ```
|
|
291
|
+
*
|
|
292
|
+
* Next, retrieve the appropriate actor in your test scenario using [`actorCalled`](https://serenity-js.org/api/core/function/actorCalled/), for example:
|
|
293
|
+
*
|
|
294
|
+
* ```ts
|
|
295
|
+
* import { describe, it, beforeEach } from 'mocha'
|
|
296
|
+
* import { actorCalled, engage } from '@serenity-js/core
|
|
297
|
+
* import { Send, GetRequest, PostRequest, LastResponse } from '@serenity-js/rest'
|
|
298
|
+
* import { Ensure, equals } from '@serenity-js/assertions'
|
|
299
|
+
*
|
|
300
|
+
* describe('Multi-actor API testing', () => {
|
|
301
|
+
* beforeEach(() => engage(new MyActors()))
|
|
302
|
+
*
|
|
303
|
+
* it('allows each actor to interact with their API', async () => {
|
|
304
|
+
*
|
|
305
|
+
* await actorCalled('Ted').attemptsTo(
|
|
306
|
+
* Send.a(PostRequest.to('products').with({ name: 'Apples', price: '£2.50' })),
|
|
307
|
+
* Ensure.that(LastResponse.status(), equals(201)),
|
|
308
|
+
* )
|
|
309
|
+
*
|
|
310
|
+
* await actorCalled('Shelly').attemptsTo(
|
|
311
|
+
* Send.a(GetRequest.to('?product=Apples')),
|
|
312
|
+
* Ensure.that(LastResponse.status(), equals(200)),
|
|
313
|
+
* Ensure.that(LastResponse.body(), equals([
|
|
314
|
+
* { name: 'Apples', price: '£2.50' }
|
|
315
|
+
* ])),
|
|
316
|
+
* )
|
|
317
|
+
* })
|
|
318
|
+
* })
|
|
319
|
+
* ```
|
|
320
|
+
*
|
|
321
|
+
* ### Using full URLs
|
|
322
|
+
*
|
|
323
|
+
* If you prefer to have a single actor interacting with multiple APIs, you can specify the full URL for every request:
|
|
324
|
+
*
|
|
325
|
+
* ```ts
|
|
326
|
+
* import { describe, it, beforeEach } from 'mocha'
|
|
327
|
+
* import { actorCalled, Cast, engage } from '@serenity-js/core
|
|
328
|
+
* import { CallAnApi, Send, GetRequest, PostRequest, LastResponse } from '@serenity-js/rest'
|
|
329
|
+
* import { Ensure, equals } from '@serenity-js/assertions'
|
|
330
|
+
*
|
|
331
|
+
* describe('Multi-actor API testing', () => {
|
|
332
|
+
* beforeEach(() => engage(
|
|
333
|
+
* Cast.where(actor => actor.whoCan(CallAnApi.using({})))
|
|
334
|
+
* ))
|
|
335
|
+
*
|
|
336
|
+
* it('allows each actor to interact with their API', async () => {
|
|
337
|
+
*
|
|
338
|
+
* await actorCalled('Alice').attemptsTo(
|
|
339
|
+
* Send.a(PostRequest.to('https://testdata.example.org/api/v1/products')
|
|
340
|
+
* .with({ name: 'Apples', price: '£2.50' })),
|
|
341
|
+
* Ensure.that(LastResponse.status(), equals(201)),
|
|
342
|
+
*
|
|
343
|
+
* Send.a(GetRequest.to('https://shop.example.org/api/v1/?product=Apples')),
|
|
344
|
+
* Ensure.that(LastResponse.status(), equals(200)),
|
|
345
|
+
* Ensure.that(LastResponse.body(), equals([
|
|
346
|
+
* { name: 'Apples', price: '£2.50' }
|
|
347
|
+
* ])),
|
|
348
|
+
* )
|
|
349
|
+
* })
|
|
350
|
+
* })
|
|
351
|
+
* ```
|
|
352
|
+
*
|
|
353
|
+
* ## Learn more
|
|
354
|
+
* - [Axios: Configuring requests](https://axios-http.com/docs/req_config)
|
|
355
|
+
* - [MDN: HTTP methods documentation](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)
|
|
356
|
+
*
|
|
357
|
+
* @group Abilities
|
|
358
|
+
*/
|
|
359
|
+
export declare class CallAnApi extends Ability {
|
|
360
|
+
private readonly axiosInstance;
|
|
361
|
+
private lastResponse;
|
|
362
|
+
/**
|
|
363
|
+
* Produces an [ability](https://serenity-js.org/api/core/class/Ability/) to call a REST API at a specified `baseURL`;
|
|
364
|
+
*
|
|
365
|
+
* This is the same as invoking `CallAnApi.using({ baseURL: 'https://example.org' })`
|
|
366
|
+
*
|
|
367
|
+
* @param baseURL
|
|
368
|
+
*/
|
|
369
|
+
static at(baseURL: URL | string): CallAnApi;
|
|
370
|
+
/**
|
|
371
|
+
* Produces an [ability](https://serenity-js.org/api/core/class/Ability/) to call an HTTP API using the given Axios instance,
|
|
372
|
+
* or an Axios request configuration object.
|
|
373
|
+
*
|
|
374
|
+
* When you provide an [Axios configuration object](https://axios-http.com/docs/req_config),
|
|
375
|
+
* it gets shallow-merged with the following defaults:
|
|
376
|
+
* - request timeout of 10 seconds
|
|
377
|
+
* - automatic proxy support based on
|
|
378
|
+
* your [environment variables](https://www.npmjs.com/package/proxy-from-env#environment-variables)
|
|
379
|
+
*
|
|
380
|
+
* When you provide an Axios instance, it's enhanced with proxy support and no other modifications are made.
|
|
381
|
+
*
|
|
382
|
+
* If you don't want Serenity/JS to augment or modify your Axios instance in any way,
|
|
383
|
+
* please use the [`CallAnApi.constructor`](https://serenity-js.org/api/rest/class/CallAnApi/#constructor) directly.
|
|
384
|
+
*
|
|
385
|
+
* @param axiosInstanceOrConfig
|
|
386
|
+
*/
|
|
387
|
+
static using(axiosInstanceOrConfig: AxiosInstance | AxiosRequestConfigDefaults): CallAnApi;
|
|
388
|
+
/**
|
|
389
|
+
* #### Learn more
|
|
390
|
+
* - [AxiosInstance](https://axios-http.com/docs/instance)
|
|
391
|
+
*
|
|
392
|
+
* @param axiosInstance
|
|
393
|
+
* A pre-configured instance of the Axios HTTP client
|
|
394
|
+
*/
|
|
395
|
+
constructor(axiosInstance: AxiosInstance);
|
|
396
|
+
/**
|
|
397
|
+
* Allows for the original Axios config to be changed after
|
|
398
|
+
* the [ability](https://serenity-js.org/api/core/class/Ability/) to [`CallAnApi`](https://serenity-js.org/api/rest/class/CallAnApi/)
|
|
399
|
+
* has been instantiated and given to the [`Actor`](https://serenity-js.org/api/core/class/Actor/).
|
|
400
|
+
*
|
|
401
|
+
* #### Learn more
|
|
402
|
+
* - [AxiosRequestConfig](https://axios-http.com/docs/req_config)
|
|
403
|
+
*
|
|
404
|
+
* @param fn
|
|
405
|
+
*/
|
|
406
|
+
modifyConfig(fn: (original: AxiosDefaults<any>) => any): void;
|
|
407
|
+
/**
|
|
408
|
+
* Sends an HTTP request to a specified url.
|
|
409
|
+
* Response will be cached and available via [`CallAnApi.mapLastResponse`](https://serenity-js.org/api/rest/class/CallAnApi/#mapLastResponse).
|
|
410
|
+
*
|
|
411
|
+
* #### Learn more
|
|
412
|
+
* - [AxiosRequestConfig](https://axios-http.com/docs/req_config)
|
|
413
|
+
* - [AxiosResponse](https://axios-http.com/docs/res_schema)
|
|
414
|
+
*
|
|
415
|
+
* @param config
|
|
416
|
+
* Axios request configuration, which can be used to override the defaults
|
|
417
|
+
* provided when the [ability](https://serenity-js.org/api/core/class/Ability/)
|
|
418
|
+
* to [`CallAnApi`](https://serenity-js.org/api/rest/class/CallAnApi/) was instantiated.
|
|
419
|
+
*/
|
|
420
|
+
request(config: AxiosRequestConfig): Promise<AxiosResponse>;
|
|
421
|
+
/**
|
|
422
|
+
* Resolves the final URL, based on the [`AxiosRequestConfig`](https://axios-http.com/docs/req_config) provided
|
|
423
|
+
* and any defaults that the [`AxiosInstance`](https://axios-http.com/docs/instance) has been configured with.
|
|
424
|
+
*
|
|
425
|
+
* Note that unlike Axios, this method uses the Node.js [WHATWG URL API](https://nodejs.org/api/url.html#new-urlinput-base)
|
|
426
|
+
* to ensure URLs are correctly resolved.
|
|
427
|
+
*
|
|
428
|
+
* @param config
|
|
429
|
+
*/
|
|
430
|
+
resolveUrl(config: AxiosRequestConfig): string;
|
|
431
|
+
/**
|
|
432
|
+
* Maps the last cached response to another type.
|
|
433
|
+
* Useful when you need to extract a portion of the [`AxiosResponse`](https://axios-http.com/docs/res_schema) object.
|
|
434
|
+
*
|
|
435
|
+
* #### Learn more
|
|
436
|
+
* - [AxiosResponse](https://axios-http.com/docs/res_schema)
|
|
437
|
+
*
|
|
438
|
+
* @param mappingFunction
|
|
439
|
+
*/
|
|
440
|
+
mapLastResponse<T>(mappingFunction: (response: AxiosResponse) => T): T;
|
|
441
|
+
toJSON(): SerialisedAbility;
|
|
442
|
+
}
|
|
443
|
+
//# sourceMappingURL=CallAnApi.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CallAnApi.d.ts","sourceRoot":"","sources":["../../../src/screenplay/abilities/CallAnApi.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAE/B,OAAO,EACH,OAAO,EAGP,KAAK,iBAAiB,EAEzB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EACH,KAAK,aAAa,EAElB,KAAK,aAAa,EAClB,KAAK,kBAAkB,EACvB,KAAK,aAAa,EACrB,MAAM,OAAO,CAAC;AAIf,OAAO,KAAK,EAAE,0BAA0B,EAAmC,MAAM,mBAAmB,CAAC;AAGrG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiWG;AACH,qBAAa,SAAU,SAAQ,OAAO;IA+CtB,OAAO,CAAC,QAAQ,CAAC,aAAa;IA7C1C,OAAO,CAAC,YAAY,CAAgB;IAEpC;;;;;;OAMG;IACH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,GAAG,MAAM,GAAG,SAAS;IAQ3C;;;;;;;;;;;;;;;;OAgBG;IACH,MAAM,CAAC,KAAK,CAAC,qBAAqB,EAAE,aAAa,GAAG,0BAA0B,GAAG,SAAS;IAI1F;;;;;;OAMG;gBAC0B,aAAa,EAAE,aAAa;IAIzD;;;;;;;;;OASG;IACH,YAAY,CAAC,EAAE,EAAE,CAAC,QAAQ,EAAE,aAAa,CAAC,GAAG,CAAC,KAAK,GAAG,GAAG,IAAI;IAI7D;;;;;;;;;;;;OAYG;IACG,OAAO,CAAC,MAAM,EAAE,kBAAkB,GAAG,OAAO,CAAC,aAAa,CAAC;IA+BjE;;;;;;;;OAQG;IACH,UAAU,CAAC,MAAM,EAAE,kBAAkB,GAAG,MAAM;IAQ9C;;;;;;;;OAQG;IACH,eAAe,CAAC,CAAC,EAAE,eAAe,EAAE,CAAC,QAAQ,EAAE,aAAa,KAAK,CAAC,GAAG,CAAC;IAQtE,MAAM,IAAI,iBAAiB;CAkB9B"}
|