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.
- package/.scratch/commit.txt +14 -0
- package/.scratch/docs.py +42 -0
- package/.scratch/echo-server.js +8 -0
- package/.scratch/echo.js +35 -0
- package/.scratch/echo.pid +1 -0
- package/.scratch/features/cookies.feature +22 -0
- package/.scratch/http.pid +1 -0
- package/.scratch/https.pid +1 -0
- package/.scratch/issue.md +49 -0
- package/.scratch/one.js +2 -0
- package/.scratch/one.json +280 -0
- package/.scratch/patch.py +163 -0
- package/.scratch/pr.md +30 -0
- package/docs/09-api-testing.md +9 -2
- package/docs/api-step-definitions.md +13 -2
- package/docs/step-definitions/then-response-status-code-should-be-number.md +4 -2
- package/examples/response-status/index.html +12 -0
- package/examples/response-status.html +19 -0
- package/package.json +1 -1
- package/tests/features/response-status-code.feature +108 -0
- package/tests/features/rest-and-api-requests.feature +120 -0
- package/tests/step-definitions/api.steps.js +18 -45
- package/tests/step-definitions/assertion.steps.js +14 -18
- package/tests/step-definitions/response.steps.js +67 -18
- package/tests/step-definitions/rest.steps.js +23 -12
- package/tests/step-definitions/webship.js +67 -0
|
@@ -2,17 +2,36 @@
|
|
|
2
2
|
|
|
3
3
|
// Lightweight REST testing — distinct from the existing api.steps.js steps
|
|
4
4
|
// Preserves exact step phrasing for compatibility.
|
|
5
|
+
//
|
|
6
|
+
// A REST request is sent through the browser context, so it follows the
|
|
7
|
+
// browser session: its cookies, its credentials and its HTTPS setting (a
|
|
8
|
+
// self-signed or locally issued certificate is accepted like in the page).
|
|
5
9
|
|
|
6
10
|
const { Given, When, Then } = require('@cucumber/cucumber');
|
|
7
11
|
const assert = require('assert');
|
|
8
|
-
const
|
|
9
|
-
const { resolveUrl } = require('./webship');
|
|
12
|
+
const { resolveUrl, sendRequest, friendly } = require('./webship');
|
|
10
13
|
|
|
11
14
|
function ensureState(world) {
|
|
12
15
|
if (!world._rest) world._rest = { headers: {}, response: null };
|
|
13
16
|
return world._rest;
|
|
14
17
|
}
|
|
15
18
|
|
|
19
|
+
async function sendRest(world, method, url, body) {
|
|
20
|
+
const s = ensureState(world);
|
|
21
|
+
const target = resolveUrl(world.launchUrl, url);
|
|
22
|
+
try {
|
|
23
|
+
s.response = await sendRequest(world, {
|
|
24
|
+
method, url: target, headers: s.headers, data: body, session: true,
|
|
25
|
+
});
|
|
26
|
+
} catch (e) {
|
|
27
|
+
throw friendly({
|
|
28
|
+
action: `send the REST ${method} request to "${target}"`,
|
|
29
|
+
cause: e,
|
|
30
|
+
hint: 'check the URL is reachable from the machine that runs the tests.',
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
16
35
|
/**
|
|
17
36
|
* Add an HTTP header to subsequent REST requests.
|
|
18
37
|
*
|
|
@@ -38,11 +57,7 @@ Given('a REST header {string} with value {string}', async function (name, value)
|
|
|
38
57
|
*
|
|
39
58
|
*/
|
|
40
59
|
When(/^(I |we )*send a REST "([^"]*)" request to "([^"]*)"$/, async function (pronoun, method, url) {
|
|
41
|
-
|
|
42
|
-
const target = resolveUrl(this.launchUrl, url);
|
|
43
|
-
s.response = await axios.request({
|
|
44
|
-
method, url: target, headers: s.headers, validateStatus: () => true,
|
|
45
|
-
});
|
|
60
|
+
await sendRest(this, method, url);
|
|
46
61
|
});
|
|
47
62
|
|
|
48
63
|
/**
|
|
@@ -71,11 +86,7 @@ When(/^(I |we )*send a REST "([^"]*)" request to "([^"]*)"$/, async function (pr
|
|
|
71
86
|
*
|
|
72
87
|
*/
|
|
73
88
|
When(/^(I |we )*send a REST "([^"]*)" request to "([^"]*)" with body:$/, async function (pronoun, method, url, body) {
|
|
74
|
-
|
|
75
|
-
const target = resolveUrl(this.launchUrl, url);
|
|
76
|
-
s.response = await axios.request({
|
|
77
|
-
method, url: target, headers: s.headers, data: body, validateStatus: () => true,
|
|
78
|
-
});
|
|
89
|
+
await sendRest(this, method, url, body);
|
|
79
90
|
});
|
|
80
91
|
|
|
81
92
|
/**
|
|
@@ -308,6 +308,72 @@ function resolveUrl(launchUrl, url) {
|
|
|
308
308
|
return (launchUrl || '') + value;
|
|
309
309
|
}
|
|
310
310
|
|
|
311
|
+
// Transport settings of the browser context that a request sent outside the
|
|
312
|
+
// browser session still has to follow: the HTTPS setting, the proxy and the
|
|
313
|
+
// client certificates. Nothing of the session itself (cookies, storage,
|
|
314
|
+
// credentials, extra headers) is in this list.
|
|
315
|
+
function transportOptions() {
|
|
316
|
+
const from = playwrightConfig.contextOptions || {};
|
|
317
|
+
const options = {};
|
|
318
|
+
for (const key of ['ignoreHTTPSErrors', 'proxy', 'clientCertificates']) {
|
|
319
|
+
if (from[key] !== undefined) options[key] = from[key];
|
|
320
|
+
}
|
|
321
|
+
return options;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function hasHeader(headers, name) {
|
|
325
|
+
const wanted = name.toLowerCase();
|
|
326
|
+
return Object.keys(headers).some((key) => key.toLowerCase() === wanted);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Send an HTTP request with Playwright's request API and return the answer
|
|
331
|
+
* as `{ status, statusText, headers, data, config }`: header names are in
|
|
332
|
+
* lower case and `data` is the parsed JSON when the body is JSON, the body
|
|
333
|
+
* text otherwise. No status code throws.
|
|
334
|
+
*
|
|
335
|
+
* `session: true` sends the request through the browser context, so it
|
|
336
|
+
* shares the cookies, the credentials and the extra headers of the page.
|
|
337
|
+
* `session: false` sends it from a request context of its own: no cookie of
|
|
338
|
+
* the browser goes with it and none comes back into the browser. Both follow
|
|
339
|
+
* the HTTPS setting of the browser context, so a self-signed or locally
|
|
340
|
+
* issued certificate is accepted whenever the browser accepts it.
|
|
341
|
+
*
|
|
342
|
+
* @param {Object} world
|
|
343
|
+
* @param {{method: string, url: string, headers?: Object, data?: *, session?: boolean}} request
|
|
344
|
+
* @returns {Promise<{status: number, statusText: string, headers: Object, data: *, config: Object}>}
|
|
345
|
+
*/
|
|
346
|
+
async function sendRequest(world, { method, url, headers, data, session }) {
|
|
347
|
+
const verb = String(method || 'GET').toUpperCase();
|
|
348
|
+
const sent = Object.assign({}, headers || {});
|
|
349
|
+
if (!hasHeader(sent, 'accept')) sent.Accept = 'application/json, text/plain, */*';
|
|
350
|
+
const options = { method: verb, headers: sent, failOnStatusCode: false };
|
|
351
|
+
if (data !== undefined && data !== null) {
|
|
352
|
+
options.data = data;
|
|
353
|
+
// A raw text body with no declared type goes out as a form body.
|
|
354
|
+
if (typeof data === 'string' && !hasHeader(sent, 'content-type') && /^(POST|PUT|PATCH)$/.test(verb)) {
|
|
355
|
+
sent['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
const shared = session && world && world.context ? world.context.request : null;
|
|
359
|
+
const own = shared ? null : await playwright.request.newContext(transportOptions());
|
|
360
|
+
try {
|
|
361
|
+
const response = await (shared || own).fetch(url, options);
|
|
362
|
+
const text = await response.text();
|
|
363
|
+
let body = text;
|
|
364
|
+
try { body = JSON.parse(text); } catch (e) { /* not JSON: keep the text */ }
|
|
365
|
+
return {
|
|
366
|
+
status: response.status(),
|
|
367
|
+
statusText: response.statusText(),
|
|
368
|
+
headers: response.headers(),
|
|
369
|
+
data: body,
|
|
370
|
+
config: { method: verb.toLowerCase(), url },
|
|
371
|
+
};
|
|
372
|
+
} finally {
|
|
373
|
+
if (own) await own.dispose();
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
311
377
|
/**
|
|
312
378
|
* Navigate to a URL with friendly error messages for common failures
|
|
313
379
|
* (server down, DNS, empty response). All other errors rethrow with
|
|
@@ -629,6 +695,7 @@ module.exports = {
|
|
|
629
695
|
buildSelector,
|
|
630
696
|
gotoUrl,
|
|
631
697
|
resolveUrl,
|
|
698
|
+
sendRequest,
|
|
632
699
|
fillField,
|
|
633
700
|
getLocatorText,
|
|
634
701
|
pad,
|