webship-js 2.0.8 → 2.0.10

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.
@@ -0,0 +1,14 @@
1
+ Fix: send the REST and API requests with the HTTPS setting of the browser (#333)
2
+
3
+ The REST and API request steps sent their request from Node with axios,
4
+ outside the browser, so on HTTPS with a self-signed or locally issued
5
+ certificate they failed with "self-signed certificate" although the
6
+ browser itself opened the site.
7
+
8
+ Both now use Playwright's request API. The REST steps go through the
9
+ browser context and follow its cookies, credentials and HTTPS setting.
10
+ The API steps keep their own base URL, authentication and headers: they
11
+ use a request context of their own, with the HTTPS setting of the browser
12
+ context and no cookie of the browser.
13
+
14
+ Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@@ -0,0 +1,42 @@
1
+ p='docs/09-api-testing.md'
2
+ s=open(p).read()
3
+ old="Webship-js scenarios can talk to an HTTP API directly without leaving the cucumber-js runtime. The browser context's cookie jar is shared, so a UI login can be followed by REST-level assertions in the same scenario.\n"
4
+ new="""Webship-js scenarios can talk to an HTTP API directly without leaving the cucumber-js runtime. Every request is sent with Playwright's request API and follows the HTTPS setting of the browser context (`ignoreHTTPSErrors` in `playwright.config.ts`), so a site served with a self-signed or locally issued certificate answers these steps the same way it answers the browser.
5
+
6
+ There are two families of steps, and they differ in one point:
7
+
8
+ | Steps | Sent | Cookies of the browser |
9
+ | --- | --- | --- |
10
+ | `I send a REST "<method>" request to "<url>"` (`rest.steps.js`) | through the browser context | shared: a UI login can be followed by REST-level assertions in the same scenario |
11
+ | `I send a <METHOD> request to "<endpoint>"` (`api.steps.js`) | outside the browser session | not sent, and none comes back into the browser: the request carries only the base URL, the authentication and the headers the scenario set |
12
+ """
13
+ assert old in s; s=s.replace(old,new)
14
+ old="A parallel short-form lives in `rest.steps.js` for quick smoke tests:\n"
15
+ new="A parallel short-form lives in `rest.steps.js` for quick smoke tests. A path is joined to the launch URL, an absolute URL is requested as written, and the request goes through the browser context: it shares the session cookies and the HTTPS setting of the page.\n"
16
+ assert old in s; s=s.replace(old,new)
17
+ open(p,'w').write(s)
18
+ p='docs/api-step-definitions.md'
19
+ s=open(p).read()
20
+ old="""webship-js ships native HTTP / REST API step definitions powered by
21
+ Playwright's request fixture and `axios`. They share the browser
22
+ context's cookie jar so the same scenario can mix UI actions and
23
+ direct API calls.
24
+ """
25
+ new="""webship-js ships native HTTP / REST API step definitions powered by
26
+ Playwright's request API, so the same scenario can mix UI actions and
27
+ direct API calls.
28
+
29
+ The API steps on this page send their requests outside the browser
30
+ session: a request carries the base URL, the authentication and the
31
+ headers the scenario set, and no cookie of the browser. They follow the
32
+ HTTPS setting of the browser context (`ignoreHTTPSErrors` in
33
+ `playwright.config.ts`), so an API served with a self-signed or locally
34
+ issued certificate answers them the same way it answers the browser.
35
+
36
+ To send a request with the cookies of the browser session, after a UI
37
+ login for example, use the short-form REST steps
38
+ (`When I send a REST "GET" request to "/api/me"`), which go through the
39
+ browser context.
40
+ """
41
+ assert old in s; s=s.replace(old,new)
42
+ open(p,'w').write(s)
@@ -0,0 +1,8 @@
1
+ const http = require('http');
2
+ http.createServer((req, res) => {
3
+ let b = ''; req.on('data', c => b += c); req.on('end', () => {
4
+ res.setHeader('content-type', 'application/json');
5
+ res.setHeader('set-cookie', 'from_api=1; Path=/');
6
+ res.end(JSON.stringify({ method: req.method, cookie: req.headers.cookie || 'none', auth: req.headers.authorization || 'none', type: req.headers['content-type'] || 'none', body: b }));
7
+ });
8
+ }).listen(8990);
@@ -0,0 +1,35 @@
1
+ const http = require('http');
2
+ const axios = require('axios');
3
+ const { request } = require('playwright');
4
+ const srv = http.createServer((req, res) => {
5
+ let b = ''; req.on('data', c => b += c); req.on('end', () => {
6
+ res.setHeader('content-type', 'application/json');
7
+ res.end(JSON.stringify({ m: req.method, h: req.headers, b }));
8
+ });
9
+ }).listen(0, async () => {
10
+ const u = `http://localhost:${srv.address().port}/x`;
11
+ const cases = [
12
+ ['GET nodata', { method: 'GET', url: u, headers: {}, data: null }],
13
+ ['GET undefined', { method: 'GET', url: u, headers: {} }],
14
+ ['GET obj', { method: 'GET', url: u, headers: {}, data: { a: 1 } }],
15
+ ['POST null', { method: 'POST', url: u, headers: {}, data: null }],
16
+ ['POST obj', { method: 'POST', url: u, headers: {}, data: { a: 1 } }],
17
+ ['POST str json', { method: 'POST', url: u, headers: {}, data: '{"a": 1}' }],
18
+ ['POST str json ct', { method: 'POST', url: u, headers: { 'Content-Type': 'application/json' }, data: '{"a":1}' }],
19
+ ['POST str form', { method: 'POST', url: u, headers: {}, data: 'a=1&b=2' }],
20
+ ['POST str ct json nonjson', { method: 'POST', url: u, headers: { 'Content-Type': 'application/json' }, data: JSON.stringify({a:'1'}) }],
21
+ ['DELETE', { method: 'DELETE', url: u, headers: {} }],
22
+ ['post lowercase', { method: 'post', url: u, headers: {}, data: 'x' }],
23
+ ];
24
+ const ctx = await request.newContext();
25
+ for (const [n, c] of cases) {
26
+ const r = await axios({ ...c, validateStatus: () => true });
27
+ const d = r.data; delete d.h.host; delete d.h.connection;
28
+ console.log('AX', n, JSON.stringify(d));
29
+ const o = { method: c.method, headers: c.headers };
30
+ if (c.data !== undefined && c.data !== null) o.data = c.data;
31
+ const p = await ctx.fetch(u, o); const pj = await p.json(); delete pj.h.host; delete pj.h.connection;
32
+ console.log('PW', n, JSON.stringify(pj));
33
+ }
34
+ await ctx.dispose(); srv.close();
35
+ });
@@ -0,0 +1 @@
1
+ 2359762
@@ -0,0 +1,22 @@
1
+ Feature: cookie check (not committed)
2
+ Scenario: REST shares the browser cookies, API does not
3
+ Given I am on "/about-us.html"
4
+ And the cookie "session_id" is set to "abc123"
5
+ Given the API base URL is "http://localhost:8990"
6
+ And I am authenticating as "tester" with "secret" password
7
+ When I send a POST request to "/echo" with body:
8
+ """
9
+ {"a": 1}
10
+ """
11
+ Then the JSON response should have "cookie" equal to "none"
12
+ And the JSON response should have "auth" equal to "Basic dGVzdGVyOnNlY3JldA=="
13
+ And the JSON response should have "type" equal to "application/json"
14
+ And a cookie with the name "from_api" should not exist
15
+ When I send a REST "POST" request to "http://localhost:8990/echo" with body:
16
+ """
17
+ a=1&b=2
18
+ """
19
+ Then the REST response should contain "session_id=abc123"
20
+ And the REST response should contain "application/x-www-form-urlencoded"
21
+ And the REST response should contain "a=1&b=2"
22
+ And a cookie with the name "from_api" should exist
@@ -0,0 +1 @@
1
+ 2324561
@@ -0,0 +1 @@
1
+ 2324559
@@ -0,0 +1,49 @@
1
+ ### Problem/Motivation
2
+
3
+ `When I send a REST "<method>" request to "<url>"` (`tests/step-definitions/rest.steps.js`) and the API request steps `When I send a <METHOD> request to "<endpoint>"` with their `with body:`, `with values:` and `with form data:` forms (`tests/step-definitions/api.steps.js`) send their request from Node with `axios`, outside the browser. Node does not trust a self-signed or locally issued certificate, so on a site served over HTTPS with one the REST step fails with:
4
+
5
+ ```
6
+ Error: self-signed certificate
7
+ ```
8
+
9
+ and the API steps fail with:
10
+
11
+ ```
12
+ GET request failed: self-signed certificate
13
+ ```
14
+
15
+ (`unable to verify the first certificate` when the certificate comes from a local authority.) The browser itself opens the same site, because the browser context sets `ignoreHTTPSErrors`. It is the same weakness as #330, in the remaining steps that still send a request from Node.
16
+
17
+ #### Steps to reproduce
18
+
19
+ 1. Serve `examples/` over HTTPS with a self-signed certificate: `npx http-server examples -p 8443 -S -C cert.pem -K key.pem`.
20
+ 2. Run the suite with `LAUNCH_URL=https://localhost:8443`.
21
+ 3. `When I send a REST "GET" request to "/example-api.json"` (`tests/features/absolute-url.feature:41`) fails with `Error: self-signed certificate`.
22
+ 4. `Given the API base URL is "/"`, `When I send a GET request to "/example-api.json"` fails with `GET request failed: self-signed certificate`.
23
+
24
+ ### Proposed resolution
25
+
26
+ Send these requests with Playwright's request API so they follow the HTTPS setting of the browser context: the REST steps through the browser context itself, the API steps from a request context of their own that carries no cookie of the browser.
27
+
28
+ AI-Generated: Yes
29
+
30
+ ### Checkpoints:
31
+ - [x] File an issue
32
+ - [ ] Addition/Change/Update/Fix
33
+ - [ ] Testing to ensure no regression
34
+ - [ ] Automated unit testing coverage
35
+ - [ ] Automated functional testing coverage
36
+ - [ ] UX/UI designer responsibilities
37
+ - [ ] Readability
38
+ - [ ] Accessibility
39
+ - [ ] Performance
40
+ - [ ] Security
41
+ - [ ] Developer Documentation
42
+ - [ ] User Guide Documentation
43
+ - [ ] Reviewed by human
44
+ - [ ] Code review by maintainers
45
+ - [ ] Full testing and approval
46
+ - [ ] Credit contributors
47
+ - [ ] Review with the product owner
48
+ - [ ] Release notes snippet
49
+ - [ ] Release
@@ -0,0 +1,2 @@
1
+ const base = require('../cucumber.js').default;
2
+ module.exports = { default: { ...base, paths: process.env.ONE.split(','), format: ['progress', 'json:.scratch/one.json'] } };
@@ -0,0 +1,280 @@
1
+ [
2
+ {
3
+ "description": "",
4
+ "elements": [
5
+ {
6
+ "description": "",
7
+ "id": "cookie-check-(not-committed);rest-shares-the-browser-cookies,-api-does-not",
8
+ "keyword": "Scenario",
9
+ "line": 2,
10
+ "name": "REST shares the browser cookies, API does not",
11
+ "steps": [
12
+ {
13
+ "keyword": "Before",
14
+ "hidden": true,
15
+ "result": {
16
+ "status": "passed",
17
+ "duration": 59045699
18
+ }
19
+ },
20
+ {
21
+ "keyword": "Before",
22
+ "hidden": true,
23
+ "result": {
24
+ "status": "passed",
25
+ "duration": 146611
26
+ }
27
+ },
28
+ {
29
+ "keyword": "Before",
30
+ "hidden": true,
31
+ "result": {
32
+ "status": "passed",
33
+ "duration": 429690
34
+ }
35
+ },
36
+ {
37
+ "keyword": "Before",
38
+ "hidden": true,
39
+ "result": {
40
+ "status": "passed",
41
+ "duration": 358450
42
+ }
43
+ },
44
+ {
45
+ "keyword": "Before",
46
+ "hidden": true,
47
+ "result": {
48
+ "status": "passed",
49
+ "duration": 56777
50
+ }
51
+ },
52
+ {
53
+ "keyword": "Before",
54
+ "hidden": true,
55
+ "result": {
56
+ "status": "passed",
57
+ "duration": 106737
58
+ }
59
+ },
60
+ {
61
+ "arguments": [],
62
+ "keyword": "Given ",
63
+ "line": 3,
64
+ "name": "I am on \"/about-us.html\"",
65
+ "match": {
66
+ "location": "tests/step-definitions/navigation.steps.js:64"
67
+ },
68
+ "result": {
69
+ "status": "passed",
70
+ "duration": 520204689
71
+ }
72
+ },
73
+ {
74
+ "arguments": [],
75
+ "keyword": "And ",
76
+ "line": 4,
77
+ "name": "the cookie \"session_id\" is set to \"abc123\"",
78
+ "match": {
79
+ "location": "tests/step-definitions/storage.steps.js:31"
80
+ },
81
+ "result": {
82
+ "status": "passed",
83
+ "duration": 2178232
84
+ }
85
+ },
86
+ {
87
+ "arguments": [],
88
+ "keyword": "Given ",
89
+ "line": 5,
90
+ "name": "the API base URL is \"http://localhost:8990\"",
91
+ "match": {
92
+ "location": "tests/step-definitions/api.steps.js:101"
93
+ },
94
+ "result": {
95
+ "status": "passed",
96
+ "duration": 449720
97
+ }
98
+ },
99
+ {
100
+ "arguments": [],
101
+ "keyword": "And ",
102
+ "line": 6,
103
+ "name": "I am authenticating as \"tester\" with \"secret\" password",
104
+ "match": {
105
+ "location": "tests/step-definitions/api.steps.js:66"
106
+ },
107
+ "result": {
108
+ "status": "passed",
109
+ "duration": 356252
110
+ }
111
+ },
112
+ {
113
+ "arguments": [
114
+ {
115
+ "content": "{\"a\": 1}",
116
+ "line": 8
117
+ }
118
+ ],
119
+ "keyword": "When ",
120
+ "line": 7,
121
+ "name": "I send a POST request to \"/echo\" with body:",
122
+ "match": {
123
+ "location": "tests/step-definitions/api.steps.js:314"
124
+ },
125
+ "result": {
126
+ "status": "passed",
127
+ "duration": 15836827
128
+ }
129
+ },
130
+ {
131
+ "arguments": [],
132
+ "keyword": "Then ",
133
+ "line": 11,
134
+ "name": "the JSON response should have \"cookie\" equal to \"none\"",
135
+ "match": {
136
+ "location": "tests/step-definitions/api.steps.js:548"
137
+ },
138
+ "result": {
139
+ "status": "passed",
140
+ "duration": 230198
141
+ }
142
+ },
143
+ {
144
+ "arguments": [],
145
+ "keyword": "And ",
146
+ "line": 12,
147
+ "name": "the JSON response should have \"auth\" equal to \"Basic dGVzdGVyOnNlY3JldA==\"",
148
+ "match": {
149
+ "location": "tests/step-definitions/api.steps.js:548"
150
+ },
151
+ "result": {
152
+ "status": "passed",
153
+ "duration": 134050
154
+ }
155
+ },
156
+ {
157
+ "arguments": [],
158
+ "keyword": "And ",
159
+ "line": 13,
160
+ "name": "the JSON response should have \"type\" equal to \"application/json\"",
161
+ "match": {
162
+ "location": "tests/step-definitions/api.steps.js:548"
163
+ },
164
+ "result": {
165
+ "status": "passed",
166
+ "duration": 146486
167
+ }
168
+ },
169
+ {
170
+ "arguments": [],
171
+ "keyword": "And ",
172
+ "line": 14,
173
+ "name": "a cookie with the name \"from_api\" should not exist",
174
+ "match": {
175
+ "location": "tests/step-definitions/cookie.steps.js:132"
176
+ },
177
+ "result": {
178
+ "status": "passed",
179
+ "duration": 1479906
180
+ }
181
+ },
182
+ {
183
+ "arguments": [
184
+ {
185
+ "content": "a=1&b=2",
186
+ "line": 16
187
+ }
188
+ ],
189
+ "keyword": "When ",
190
+ "line": 15,
191
+ "name": "I send a REST \"POST\" request to \"http://localhost:8990/echo\" with body:",
192
+ "match": {
193
+ "location": "tests/step-definitions/rest.steps.js:88"
194
+ },
195
+ "result": {
196
+ "status": "passed",
197
+ "duration": 7299009
198
+ }
199
+ },
200
+ {
201
+ "arguments": [],
202
+ "keyword": "Then ",
203
+ "line": 19,
204
+ "name": "the REST response should contain \"session_id=abc123\"",
205
+ "match": {
206
+ "location": "tests/step-definitions/rest.steps.js:118"
207
+ },
208
+ "result": {
209
+ "status": "passed",
210
+ "duration": 308369
211
+ }
212
+ },
213
+ {
214
+ "arguments": [],
215
+ "keyword": "And ",
216
+ "line": 20,
217
+ "name": "the REST response should contain \"application/x-www-form-urlencoded\"",
218
+ "match": {
219
+ "location": "tests/step-definitions/rest.steps.js:118"
220
+ },
221
+ "result": {
222
+ "status": "passed",
223
+ "duration": 187290
224
+ }
225
+ },
226
+ {
227
+ "arguments": [],
228
+ "keyword": "And ",
229
+ "line": 21,
230
+ "name": "the REST response should contain \"a=1&b=2\"",
231
+ "match": {
232
+ "location": "tests/step-definitions/rest.steps.js:118"
233
+ },
234
+ "result": {
235
+ "status": "passed",
236
+ "duration": 100281
237
+ }
238
+ },
239
+ {
240
+ "arguments": [],
241
+ "keyword": "And ",
242
+ "line": 22,
243
+ "name": "a cookie with the name \"from_api\" should exist",
244
+ "match": {
245
+ "location": "tests/step-definitions/cookie.steps.js:32"
246
+ },
247
+ "result": {
248
+ "status": "passed",
249
+ "duration": 1266994
250
+ }
251
+ },
252
+ {
253
+ "keyword": "After",
254
+ "hidden": true,
255
+ "result": {
256
+ "status": "passed",
257
+ "duration": 97005
258
+ }
259
+ },
260
+ {
261
+ "keyword": "After",
262
+ "hidden": true,
263
+ "result": {
264
+ "status": "passed",
265
+ "duration": 18792075
266
+ }
267
+ }
268
+ ],
269
+ "tags": [],
270
+ "type": "scenario"
271
+ }
272
+ ],
273
+ "id": "cookie-check-(not-committed)",
274
+ "line": 1,
275
+ "keyword": "Feature",
276
+ "name": "cookie check (not committed)",
277
+ "tags": [],
278
+ "uri": ".scratch/features/cookies.feature"
279
+ }
280
+ ]
@@ -0,0 +1,163 @@
1
+ import re,io
2
+ p='tests/step-definitions/webship.js'
3
+ s=open(p).read()
4
+ helper = r'''
5
+ // Transport settings of the browser context that a request sent outside the
6
+ // browser session still has to follow: the HTTPS setting, the proxy and the
7
+ // client certificates. Nothing of the session itself (cookies, storage,
8
+ // credentials, extra headers) is in this list.
9
+ function transportOptions() {
10
+ const from = playwrightConfig.contextOptions || {};
11
+ const options = {};
12
+ for (const key of ['ignoreHTTPSErrors', 'proxy', 'clientCertificates']) {
13
+ if (from[key] !== undefined) options[key] = from[key];
14
+ }
15
+ return options;
16
+ }
17
+
18
+ function hasHeader(headers, name) {
19
+ const wanted = name.toLowerCase();
20
+ return Object.keys(headers).some((key) => key.toLowerCase() === wanted);
21
+ }
22
+
23
+ /**
24
+ * Send an HTTP request with Playwright's request API and return the answer
25
+ * as `{ status, statusText, headers, data, config }`: header names are in
26
+ * lower case and `data` is the parsed JSON when the body is JSON, the body
27
+ * text otherwise. No status code throws.
28
+ *
29
+ * `session: true` sends the request through the browser context, so it
30
+ * shares the cookies, the credentials and the extra headers of the page.
31
+ * `session: false` sends it from a request context of its own: no cookie of
32
+ * the browser goes with it and none comes back into the browser. Both follow
33
+ * the HTTPS setting of the browser context, so a self-signed or locally
34
+ * issued certificate is accepted whenever the browser accepts it.
35
+ *
36
+ * @param {Object} world
37
+ * @param {{method: string, url: string, headers?: Object, data?: *, session?: boolean}} request
38
+ * @returns {Promise<{status: number, statusText: string, headers: Object, data: *, config: Object}>}
39
+ */
40
+ async function sendRequest(world, { method, url, headers, data, session }) {
41
+ const verb = String(method || 'GET').toUpperCase();
42
+ const sent = Object.assign({}, headers || {});
43
+ if (!hasHeader(sent, 'accept')) sent.Accept = 'application/json, text/plain, */*';
44
+ const options = { method: verb, headers: sent, failOnStatusCode: false };
45
+ if (data !== undefined && data !== null) {
46
+ options.data = data;
47
+ // A raw text body with no declared type goes out as a form body.
48
+ if (typeof data === 'string' && !hasHeader(sent, 'content-type') && /^(POST|PUT|PATCH)$/.test(verb)) {
49
+ sent['Content-Type'] = 'application/x-www-form-urlencoded';
50
+ }
51
+ }
52
+ const shared = session && world && world.context ? world.context.request : null;
53
+ const own = shared ? null : await playwright.request.newContext(transportOptions());
54
+ try {
55
+ const response = await (shared || own).fetch(url, options);
56
+ const text = await response.text();
57
+ let body = text;
58
+ try { body = JSON.parse(text); } catch (e) { /* not JSON: keep the text */ }
59
+ return {
60
+ status: response.status(),
61
+ statusText: response.statusText(),
62
+ headers: response.headers(),
63
+ data: body,
64
+ config: { method: verb.toLowerCase(), url },
65
+ };
66
+ } finally {
67
+ if (own) await own.dispose();
68
+ }
69
+ }
70
+
71
+ /**
72
+ * Navigate to a URL with friendly error messages for common failures'''
73
+ marker = '''
74
+ /**
75
+ * Navigate to a URL with friendly error messages for common failures'''
76
+ assert s.count(marker)==1
77
+ s=s.replace(marker,helper)
78
+ s=s.replace(" resolveUrl,\n fillField,"," resolveUrl,\n sendRequest,\n fillField,")
79
+ open(p,'w').write(s)
80
+
81
+ p='tests/step-definitions/rest.steps.js'
82
+ s=open(p).read()
83
+ s=s.replace("""// Lightweight REST testing — distinct from the existing api.steps.js steps
84
+ // Preserves exact step phrasing for compatibility.
85
+ ""","""// Lightweight REST testing — distinct from the existing api.steps.js steps
86
+ // Preserves exact step phrasing for compatibility.
87
+ //
88
+ // A REST request is sent through the browser context, so it follows the
89
+ // browser session: its cookies, its credentials and its HTTPS setting (a
90
+ // self-signed or locally issued certificate is accepted like in the page).
91
+ """)
92
+ s=s.replace("const axios = require('axios');\nconst { resolveUrl } = require('./webship');","const { resolveUrl, sendRequest, friendly } = require('./webship');")
93
+ s=s.replace("""function ensureState(world) {
94
+ if (!world._rest) world._rest = { headers: {}, response: null };
95
+ return world._rest;
96
+ }
97
+ ""","""function ensureState(world) {
98
+ if (!world._rest) world._rest = { headers: {}, response: null };
99
+ return world._rest;
100
+ }
101
+
102
+ async function sendRest(world, method, url, body) {
103
+ const s = ensureState(world);
104
+ const target = resolveUrl(world.launchUrl, url);
105
+ try {
106
+ s.response = await sendRequest(world, {
107
+ method, url: target, headers: s.headers, data: body, session: true,
108
+ });
109
+ } catch (e) {
110
+ throw friendly({
111
+ action: `send the REST ${method} request to "${target}"`,
112
+ cause: e,
113
+ hint: 'check the URL is reachable from the machine that runs the tests.',
114
+ });
115
+ }
116
+ }
117
+ """)
118
+ old1=""" const s = ensureState(this);
119
+ const target = resolveUrl(this.launchUrl, url);
120
+ s.response = await axios.request({
121
+ method, url: target, headers: s.headers, validateStatus: () => true,
122
+ });
123
+ """
124
+ old2=""" const s = ensureState(this);
125
+ const target = resolveUrl(this.launchUrl, url);
126
+ s.response = await axios.request({
127
+ method, url: target, headers: s.headers, data: body, validateStatus: () => true,
128
+ });
129
+ """
130
+ assert old1 in s and old2 in s
131
+ s=s.replace(old1," await sendRest(this, method, url);\n").replace(old2," await sendRest(this, method, url, body);\n")
132
+ open(p,'w').write(s)
133
+
134
+ p='tests/step-definitions/api.steps.js'
135
+ s=open(p).read()
136
+ s=s.replace("const axios = require('axios');\nconst assert = require('assert');\nconst { friendly } = require('./webship');","const assert = require('assert');\nconst { friendly, sendRequest } = require('./webship');")
137
+ s=s.replace("""// Global variables to store API response and request data""","""// The API steps talk to an API on its own terms: its own base URL, its own
138
+ // authentication and headers. A request is sent outside the browser session
139
+ // (no cookie of the browser goes with it), and it follows the HTTPS setting
140
+ // of the browser context, so a self-signed or locally issued certificate is
141
+ // accepted whenever the browser accepts it.
142
+ function sendApiRequest(world, method, url, headers, data) {
143
+ return sendRequest(world, { method, url, headers, data, session: false });
144
+ }
145
+
146
+ // Global variables to store API response and request data""")
147
+ pat=re.compile(r"""apiResponse = await axios\(\{
148
+ method: method,
149
+ url: url,
150
+ headers: (.*?),
151
+ data: (\w+(?:\.stringify\(fields\))?),
152
+ validateStatus: function \(status\) \{
153
+ return true; // Don't throw on any status code
154
+ \}
155
+ \}\);""",re.S)
156
+ def rep(m):
157
+ h=re.sub(r"\s+"," ",m.group(1)).replace("{ ...apiHeaders, 'Content-Type': 'application/x-www-form-urlencoded' }","{ ...apiHeaders, 'Content-Type': 'application/x-www-form-urlencoded' }")
158
+ return "apiResponse = await sendApiRequest(this, method, url, %s, %s);"%(h,m.group(2))
159
+ s,n=pat.subn(rep,s)
160
+ assert n==4,n
161
+ s,n=re.subn(r"throw friendly\(`\$\{method\} request( with \w+(?: data)?)? failed: \$\{error\.message\}`\);", lambda m:"throw friendly(`${method} request%s to \"${url}\" failed.`, error);"%(m.group(1) or ''), s)
162
+ assert n==4,n
163
+ open(p,'w').write(s)
package/.scratch/pr.md ADDED
@@ -0,0 +1,30 @@
1
+ The REST request steps (`I send a REST "<method>" request to "<url>"`, with and without a body) and the API request steps (`I send a <METHOD> request to "<endpoint>"`, plain and with a body, values or form data) now send their request with Playwright's request API instead of `axios` from Node, so they follow the HTTPS setting of the browser context and reach a site served with a self-signed or locally issued certificate. The REST steps go through the browser context and share its cookies; the API steps keep their own base URL, authentication and headers and carry no cookie of the browser. Step phrases and the status, header, body and JSON assertions that follow them are unchanged.
2
+
3
+ Closes #333
4
+
5
+ Proof: the new `tests/features/rest-and-api-requests.feature` (14 scenarios: GET and POST with a JSON body, 404 and 405 answers, a response header and JSON assertions, a path and an absolute URL) fails 7 of 14 before the change over HTTPS with a self-signed certificate (`self-signed certificate`) and passes after it. Full suite locally, over HTTP and over HTTPS with a self-signed certificate: 345 scenarios, 1652 steps, all passed in both.
6
+
7
+ AI-Generated: Yes
8
+
9
+ ### Checkpoints:
10
+ - [x] File an issue
11
+ - [x] Addition/Change/Update/Fix
12
+ - [x] Testing to ensure no regression
13
+ - [ ] Automated unit testing coverage
14
+ - [x] Automated functional testing coverage
15
+ - [ ] UX/UI designer responsibilities
16
+ - [ ] Readability
17
+ - [ ] Accessibility
18
+ - [ ] Performance
19
+ - [ ] Security
20
+ - [ ] Developer Documentation
21
+ - [x] User Guide Documentation
22
+ - [ ] Reviewed by human
23
+ - [ ] Code review by maintainers
24
+ - [ ] Full testing and approval
25
+ - [ ] Credit contributors
26
+ - [ ] Review with the product owner
27
+ - [ ] Release notes snippet
28
+ - [ ] Release
29
+
30
+ 🤖 Generated with [Claude Code](https://claude.com/claude-code)