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
package/docs/09-api-testing.md
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
# API Testing
|
|
2
2
|
|
|
3
|
-
Webship-js scenarios can talk to an HTTP API directly without leaving the cucumber-js runtime.
|
|
3
|
+
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.
|
|
4
|
+
|
|
5
|
+
There are two families of steps, and they differ in one point:
|
|
6
|
+
|
|
7
|
+
| Steps | Sent | Cookies of the browser |
|
|
8
|
+
| --- | --- | --- |
|
|
9
|
+
| `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 |
|
|
10
|
+
| `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 |
|
|
4
11
|
|
|
5
12
|
## Setting headers and base URL
|
|
6
13
|
|
|
@@ -96,7 +103,7 @@ Given the basic authentication with the username "admin" and the password "secre
|
|
|
96
103
|
|
|
97
104
|
## Short-form REST steps
|
|
98
105
|
|
|
99
|
-
A parallel short-form lives in `rest.steps.js` for quick smoke tests:
|
|
106
|
+
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.
|
|
100
107
|
|
|
101
108
|
```gherkin
|
|
102
109
|
Given a REST header "Authorization" with value "Bearer abc123"
|
|
@@ -4,10 +4,21 @@
|
|
|
4
4
|
## Overview
|
|
5
5
|
|
|
6
6
|
webship-js ships native HTTP / REST API step definitions powered by
|
|
7
|
-
Playwright's request
|
|
8
|
-
context's cookie jar so the same scenario can mix UI actions and
|
|
7
|
+
Playwright's request API, so the same scenario can mix UI actions and
|
|
9
8
|
direct API calls.
|
|
10
9
|
|
|
10
|
+
The API steps on this page send their requests outside the browser
|
|
11
|
+
session: a request carries the base URL, the authentication and the
|
|
12
|
+
headers the scenario set, and no cookie of the browser. They follow the
|
|
13
|
+
HTTPS setting of the browser context (`ignoreHTTPSErrors` in
|
|
14
|
+
`playwright.config.ts`), so an API served with a self-signed or locally
|
|
15
|
+
issued certificate answers them the same way it answers the browser.
|
|
16
|
+
|
|
17
|
+
To send a request with the cookies of the browser session, after a UI
|
|
18
|
+
login for example, use the short-form REST steps
|
|
19
|
+
(`When I send a REST "GET" request to "/api/me"`), which go through the
|
|
20
|
+
browser context.
|
|
21
|
+
|
|
11
22
|
## Available API Step Definitions
|
|
12
23
|
|
|
13
24
|
The documentation lists 22 distinct step definition categories:
|
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
|
|
6
6
|
"This step definition is used after making a request to the web service under test. Once the response is received, this step definition asserts HTTP status code returned by the server matches the expected code."
|
|
7
7
|
|
|
8
|
+
The status is the one the browser received for the page it is showing, after any redirect. It is read from the browser session itself, so it follows the session cookies, the HTTPS settings of the browser context (a self-signed or locally issued certificate is fine) and any stubbed URL. A redirect is followed, so the step sees the status of the page the redirect ends on, never the 301 or 302 of the redirect itself.
|
|
9
|
+
|
|
8
10
|
## Gherkin Syntax
|
|
9
11
|
|
|
10
12
|
```
|
|
@@ -45,13 +47,13 @@ Then the response status code should be 200
|
|
|
45
47
|
### Example 2
|
|
46
48
|
|
|
47
49
|
```gherkin
|
|
48
|
-
|
|
50
|
+
And the response status code should not be 404
|
|
49
51
|
```
|
|
50
52
|
|
|
51
53
|
### Example 3
|
|
52
54
|
|
|
53
55
|
```gherkin
|
|
54
|
-
|
|
56
|
+
Then the response status code should be 404
|
|
55
57
|
```
|
|
56
58
|
|
|
57
59
|
### Example 4
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<title>Response status: redirected section</title>
|
|
6
|
+
</head>
|
|
7
|
+
<body>
|
|
8
|
+
<h1>Redirected section</h1>
|
|
9
|
+
<p>The address without the trailing slash redirects here.</p>
|
|
10
|
+
<p><a href="../response-status.html">Back to the response status page</a></p>
|
|
11
|
+
</body>
|
|
12
|
+
</html>
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<title>Response status</title>
|
|
6
|
+
</head>
|
|
7
|
+
<body>
|
|
8
|
+
<h1>Response status</h1>
|
|
9
|
+
<p>Links to pages that answer with different HTTP status codes.</p>
|
|
10
|
+
<ul>
|
|
11
|
+
<li><a href="existing-page.html">Open the existing page</a></li>
|
|
12
|
+
<li><a href="not-existing-page.html">Open the missing page</a></li>
|
|
13
|
+
<li><a href="response-status">Open the redirected section</a></li>
|
|
14
|
+
<li><a href="#details">Jump to the details</a></li>
|
|
15
|
+
</ul>
|
|
16
|
+
<h2 id="details">Details</h2>
|
|
17
|
+
<p>The details are on this same page.</p>
|
|
18
|
+
</body>
|
|
19
|
+
</html>
|
package/package.json
CHANGED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
Feature: The response status code is the one the browser received
|
|
2
|
+
As a tester,
|
|
3
|
+
I want the response status and header steps to read the response of the page the browser is showing
|
|
4
|
+
So that they follow the browser session: its cookies, its HTTPS settings, redirects and stubbed pages
|
|
5
|
+
|
|
6
|
+
Scenario: Status after a direct visit
|
|
7
|
+
Given I am on "/existing-page.html"
|
|
8
|
+
Then the response status code should be 200
|
|
9
|
+
And the response status code should not be 404
|
|
10
|
+
And the response status code should not be 500
|
|
11
|
+
|
|
12
|
+
Scenario: Status of a missing page
|
|
13
|
+
Given I am on "/not-existing-page.html"
|
|
14
|
+
Then the response status code should be 404
|
|
15
|
+
And the response status code should not be 200
|
|
16
|
+
|
|
17
|
+
Scenario: Status after a redirect is the status of the page the redirect ends on
|
|
18
|
+
Given I am on "/response-status"
|
|
19
|
+
Then the path should be "/response-status/"
|
|
20
|
+
And I should see "Redirected section"
|
|
21
|
+
And the response status code should be 200
|
|
22
|
+
And the response status code should not be 302
|
|
23
|
+
|
|
24
|
+
Scenario: Status after clicking a link to an existing page
|
|
25
|
+
Given I am on "/response-status.html"
|
|
26
|
+
When I click "Open the existing page"
|
|
27
|
+
Then I should see "An example page that returns a 200 response code"
|
|
28
|
+
And the response status code should be 200
|
|
29
|
+
|
|
30
|
+
Scenario: Status after clicking a link to a missing page
|
|
31
|
+
Given I am on "/response-status.html"
|
|
32
|
+
Then the response status code should be 200
|
|
33
|
+
When I click "Open the missing page"
|
|
34
|
+
Then the path should be "/not-existing-page.html"
|
|
35
|
+
And the response status code should be 404
|
|
36
|
+
And the response status code should not be 200
|
|
37
|
+
|
|
38
|
+
Scenario: Status after clicking a link that redirects
|
|
39
|
+
Given I am on "/response-status.html"
|
|
40
|
+
When I click "Open the redirected section"
|
|
41
|
+
Then I should see "Redirected section"
|
|
42
|
+
And the response status code should be 200
|
|
43
|
+
|
|
44
|
+
Scenario: Status stays the same after a jump inside the page
|
|
45
|
+
Given I am on "/response-status.html"
|
|
46
|
+
When I click "Jump to the details"
|
|
47
|
+
Then the response status code should be 200
|
|
48
|
+
And the response status code should not be 404
|
|
49
|
+
|
|
50
|
+
Scenario: Status of a page that answers 403
|
|
51
|
+
Given I am on the homepage
|
|
52
|
+
And the URL "**/restricted.html" returns status 403 with body "Access denied"
|
|
53
|
+
When I go to "/restricted.html"
|
|
54
|
+
Then the response status code should be 403
|
|
55
|
+
And the response status code should not be 200
|
|
56
|
+
|
|
57
|
+
Scenario: Status of a page that answers 500
|
|
58
|
+
Given I am on the homepage
|
|
59
|
+
And the URL "**/broken.html" returns status 500 with body "Server error"
|
|
60
|
+
When I go to "/broken.html"
|
|
61
|
+
Then the response status code should be 500
|
|
62
|
+
And the response status code should not be 200
|
|
63
|
+
|
|
64
|
+
Scenario: Status follows the page after a visit to a missing page and a new visit
|
|
65
|
+
Given I am on "/not-existing-page.html"
|
|
66
|
+
Then the response status code should be 404
|
|
67
|
+
When I go to "/existing-page.html"
|
|
68
|
+
Then the response status code should be 200
|
|
69
|
+
And the response status code should not be 404
|
|
70
|
+
|
|
71
|
+
Scenario: Status after reloading the page
|
|
72
|
+
Given I am on "/existing-page.html"
|
|
73
|
+
When I reload the page
|
|
74
|
+
Then the response status code should be 200
|
|
75
|
+
|
|
76
|
+
Scenario: Status after moving back in the history
|
|
77
|
+
Given I am on "/existing-page.html"
|
|
78
|
+
When I go to "/not-existing-page.html"
|
|
79
|
+
Then the response status code should be 404
|
|
80
|
+
When I move backward one page
|
|
81
|
+
Then the path should be "/existing-page.html"
|
|
82
|
+
And the response status code should be 200
|
|
83
|
+
When I move forward one page
|
|
84
|
+
Then the response status code should be 404
|
|
85
|
+
|
|
86
|
+
Scenario: Status in a new anonymous session
|
|
87
|
+
Given I am an anonymous user
|
|
88
|
+
When I go to "/existing-page.html"
|
|
89
|
+
Then the response status code should be 200
|
|
90
|
+
When I go to "/not-existing-page.html"
|
|
91
|
+
Then the response status code should be 404
|
|
92
|
+
|
|
93
|
+
Scenario: Headers are read from the same response as the status
|
|
94
|
+
Given I am on "/existing-page.html"
|
|
95
|
+
Then the response should contain the header "content-type"
|
|
96
|
+
And the response header "content-type" should contain the value "html"
|
|
97
|
+
When I go to "/example-api.json"
|
|
98
|
+
Then the response status code should be 200
|
|
99
|
+
And the response header "content-type" should contain the value "application/json"
|
|
100
|
+
And the response header "content-type" should not contain the value "html"
|
|
101
|
+
|
|
102
|
+
Scenario: Status on a page opened after the browser session was replaced
|
|
103
|
+
Given the basic authentication with the username "tester" and the password "secret"
|
|
104
|
+
When I go to "/existing-page.html"
|
|
105
|
+
Then the response status code should be 200
|
|
106
|
+
And the response should contain the header "content-type"
|
|
107
|
+
When I go to "/not-existing-page.html"
|
|
108
|
+
Then the response status code should be 404
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
Feature: REST and API requests follow the HTTPS setting of the browser
|
|
2
|
+
As a tester,
|
|
3
|
+
I want the REST and API request steps to reach every site the browser reaches
|
|
4
|
+
So that they work on a site served over HTTPS with a self-signed or locally issued certificate
|
|
5
|
+
|
|
6
|
+
Background:
|
|
7
|
+
Given the API base URL is "/"
|
|
8
|
+
|
|
9
|
+
Scenario: A REST GET request to a path
|
|
10
|
+
When I send a REST "GET" request to "/example-api.json"
|
|
11
|
+
Then the REST response status code should be 200
|
|
12
|
+
And the REST response should contain "JSON:API paints my bikeshed!"
|
|
13
|
+
And the REST response should contain "articles"
|
|
14
|
+
|
|
15
|
+
Scenario: A REST GET request to an absolute URL
|
|
16
|
+
When I send a REST "GET" request to "http://localhost:8080/example-api.json"
|
|
17
|
+
Then the REST response status code should be 200
|
|
18
|
+
And the REST response should contain "The shortest article. Ever."
|
|
19
|
+
|
|
20
|
+
Scenario: A REST GET request to a missing page answers 404
|
|
21
|
+
When I send a REST "GET" request to "/not-existing-page.json"
|
|
22
|
+
Then the REST response status code should be 404
|
|
23
|
+
|
|
24
|
+
Scenario: A REST request after the browser opened a page
|
|
25
|
+
Given I am on "/about-us.html"
|
|
26
|
+
When we send a REST "GET" request to "/example-api.json"
|
|
27
|
+
Then the REST response status code should be 200
|
|
28
|
+
And the REST response should contain "people"
|
|
29
|
+
And I should see "About Us"
|
|
30
|
+
|
|
31
|
+
Scenario: A REST POST request with a JSON body to a path that takes no POST
|
|
32
|
+
Given a REST header "Content-Type" with value "application/json"
|
|
33
|
+
When I send a REST "POST" request to "/example-api.json" with body:
|
|
34
|
+
"""
|
|
35
|
+
{"title": "Webship", "body": "A REST request with a JSON body"}
|
|
36
|
+
"""
|
|
37
|
+
Then the REST response status code should be 405
|
|
38
|
+
|
|
39
|
+
Scenario: A REST POST request with a JSON body to an absolute URL
|
|
40
|
+
Given a REST header "Content-Type" with value "application/json"
|
|
41
|
+
When I send a REST "POST" request to "https://jsonplaceholder.typicode.com/posts" with body:
|
|
42
|
+
"""
|
|
43
|
+
{"title": "Webship", "body": "A REST request with a JSON body", "userId": 1}
|
|
44
|
+
"""
|
|
45
|
+
Then the REST response status code should be 201
|
|
46
|
+
And the REST response should contain "Webship"
|
|
47
|
+
And the REST response should contain "A REST request with a JSON body"
|
|
48
|
+
And the REST response should contain "id"
|
|
49
|
+
|
|
50
|
+
Scenario: A REST DELETE request to an absolute URL
|
|
51
|
+
When I send a REST "DELETE" request to "https://jsonplaceholder.typicode.com/posts/1"
|
|
52
|
+
Then the REST response status code should be 200
|
|
53
|
+
|
|
54
|
+
Scenario: An API GET request to a path under the launch URL
|
|
55
|
+
When I send a GET request to "/example-api.json"
|
|
56
|
+
Then the API response code should be 200
|
|
57
|
+
And the response should be valid JSON
|
|
58
|
+
And the response header "Content-Type" should contain "application/json"
|
|
59
|
+
And the JSON response should have "data.0.type" equal to "articles"
|
|
60
|
+
And the JSON response should have "included.0.attributes.name" equal to "John"
|
|
61
|
+
And the JSON property "included.0.attributes.age" should be 80
|
|
62
|
+
And the JSON response should have property "data.0.relationships.author"
|
|
63
|
+
And the JSON response should not have property "data.0.password"
|
|
64
|
+
And the API response should contain "JSON:API paints my bikeshed!"
|
|
65
|
+
|
|
66
|
+
Scenario: An API GET request to an absolute base URL
|
|
67
|
+
Given the API base URL is "http://localhost:8080"
|
|
68
|
+
When I send a GET request to "/example-api.json"
|
|
69
|
+
Then the API response code should be 200
|
|
70
|
+
And the header "Content-Type" should contain "application/json"
|
|
71
|
+
And the JSON response should have "data.0.id" equal to "1"
|
|
72
|
+
|
|
73
|
+
Scenario: An API GET request to a missing page answers 404
|
|
74
|
+
When I send a GET request to "/not-existing-page.json"
|
|
75
|
+
Then the API response code should be 404
|
|
76
|
+
And the API response should not contain "articles"
|
|
77
|
+
|
|
78
|
+
Scenario: An API POST request with a JSON body to a path that takes no POST
|
|
79
|
+
When I send a POST request to "/example-api.json" with body:
|
|
80
|
+
"""
|
|
81
|
+
{"title": "Webship", "body": "An API request with a JSON body"}
|
|
82
|
+
"""
|
|
83
|
+
Then the API response code should be 405
|
|
84
|
+
|
|
85
|
+
Scenario: An API POST request with a JSON body to an absolute base URL
|
|
86
|
+
Given the API base URL is "https://jsonplaceholder.typicode.com"
|
|
87
|
+
When I send a POST request to "/posts" with body:
|
|
88
|
+
"""
|
|
89
|
+
{"title": "Webship", "body": "An API request with a JSON body", "userId": 1}
|
|
90
|
+
"""
|
|
91
|
+
Then the API response code should be 201
|
|
92
|
+
And the response should be valid JSON
|
|
93
|
+
And the response header "Content-Type" should contain "application/json"
|
|
94
|
+
And the JSON response should have "title" equal to "Webship"
|
|
95
|
+
And the JSON response should have "userId" equal to 1
|
|
96
|
+
And the JSON response should have property "id"
|
|
97
|
+
|
|
98
|
+
Scenario: An API POST request with values and with form data
|
|
99
|
+
Given the API base URL is "https://jsonplaceholder.typicode.com"
|
|
100
|
+
When I send a POST request to "/posts" with values:
|
|
101
|
+
| title | Webship |
|
|
102
|
+
| body | Sent as values |
|
|
103
|
+
Then the API response code should be 201
|
|
104
|
+
And the JSON response should have "title" equal to "Webship"
|
|
105
|
+
When I send a POST request to "/posts" with form data:
|
|
106
|
+
"""
|
|
107
|
+
title=Webship
|
|
108
|
+
body=Sent as form data
|
|
109
|
+
"""
|
|
110
|
+
Then the API response code should be 201
|
|
111
|
+
And the JSON response should have "body" equal to "Sent as form data"
|
|
112
|
+
|
|
113
|
+
Scenario: An API request with a request body set before it and basic authentication
|
|
114
|
+
Given the API base URL is "https://jsonplaceholder.typicode.com"
|
|
115
|
+
And I am authenticating as "tester" with "secret" password
|
|
116
|
+
And I set the request body to '{"title": "Webship", "userId": 7}'
|
|
117
|
+
When I send a PUT request to "/posts/1"
|
|
118
|
+
Then the API response code should be 200
|
|
119
|
+
And the JSON response should have "title" equal to "Webship"
|
|
120
|
+
And the JSON response should have "userId" equal to 7
|
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
const { Given, When, Then, Before } = require('@cucumber/cucumber');
|
|
2
|
-
const axios = require('axios');
|
|
3
2
|
const assert = require('assert');
|
|
4
|
-
const { friendly } = require('./webship');
|
|
3
|
+
const { friendly, sendRequest } = require('./webship');
|
|
4
|
+
|
|
5
|
+
// The API steps talk to an API on its own terms: its own base URL, its own
|
|
6
|
+
// authentication and headers. A request is sent outside the browser session
|
|
7
|
+
// (no cookie of the browser goes with it), and it follows the HTTPS setting
|
|
8
|
+
// of the browser context, so a self-signed or locally issued certificate is
|
|
9
|
+
// accepted whenever the browser accepts it.
|
|
10
|
+
function sendApiRequest(world, method, url, headers, data) {
|
|
11
|
+
return sendRequest(world, { method, url, headers, data, session: false });
|
|
12
|
+
}
|
|
5
13
|
|
|
6
14
|
// Global variables to store API response and request data
|
|
7
15
|
let apiResponse = null;
|
|
@@ -223,17 +231,9 @@ When(/^(?:I |we )?send a ([A-Z]+) request to "([^"]+)"$/, async function (method
|
|
|
223
231
|
const url = baseURL + '/' + prepareUrl(endpoint);
|
|
224
232
|
|
|
225
233
|
try {
|
|
226
|
-
apiResponse = await
|
|
227
|
-
method: method,
|
|
228
|
-
url: url,
|
|
229
|
-
headers: apiHeaders,
|
|
230
|
-
data: apiRequestData,
|
|
231
|
-
validateStatus: function (status) {
|
|
232
|
-
return true; // Don't throw on any status code
|
|
233
|
-
}
|
|
234
|
-
});
|
|
234
|
+
apiResponse = await sendApiRequest(this, method, url, apiHeaders, apiRequestData);
|
|
235
235
|
} catch (error) {
|
|
236
|
-
throw friendly(`${method} request
|
|
236
|
+
throw friendly(`${method} request to "${url}" failed.`, error);
|
|
237
237
|
}
|
|
238
238
|
});
|
|
239
239
|
|
|
@@ -269,17 +269,9 @@ When(/^(?:I |we )?send a ([A-Z]+) request to "([^"]+)" with values:$/, async fun
|
|
|
269
269
|
});
|
|
270
270
|
|
|
271
271
|
try {
|
|
272
|
-
apiResponse = await
|
|
273
|
-
method: method,
|
|
274
|
-
url: url,
|
|
275
|
-
headers: { ...apiHeaders, 'Content-Type': 'application/json' },
|
|
276
|
-
data: JSON.stringify(fields),
|
|
277
|
-
validateStatus: function (status) {
|
|
278
|
-
return true; // Don't throw on any status code
|
|
279
|
-
}
|
|
280
|
-
});
|
|
272
|
+
apiResponse = await sendApiRequest(this, method, url, { ...apiHeaders, 'Content-Type': 'application/json' }, JSON.stringify(fields));
|
|
281
273
|
} catch (error) {
|
|
282
|
-
throw friendly(`${method} request with values
|
|
274
|
+
throw friendly(`${method} request with values to "${url}" failed.`, error);
|
|
283
275
|
}
|
|
284
276
|
});
|
|
285
277
|
|
|
@@ -342,17 +334,9 @@ When(/^(?:I |we )?send a ([A-Z]+) request to "([^"]+)" with body:$/, async funct
|
|
|
342
334
|
}
|
|
343
335
|
|
|
344
336
|
try {
|
|
345
|
-
apiResponse = await
|
|
346
|
-
method: method,
|
|
347
|
-
url: url,
|
|
348
|
-
headers: headers,
|
|
349
|
-
data: requestData,
|
|
350
|
-
validateStatus: function (status) {
|
|
351
|
-
return true; // Don't throw on any status code
|
|
352
|
-
}
|
|
353
|
-
});
|
|
337
|
+
apiResponse = await sendApiRequest(this, method, url, headers, requestData);
|
|
354
338
|
} catch (error) {
|
|
355
|
-
throw friendly(`${method} request with body
|
|
339
|
+
throw friendly(`${method} request with body to "${url}" failed.`, error);
|
|
356
340
|
}
|
|
357
341
|
});
|
|
358
342
|
|
|
@@ -404,20 +388,9 @@ When(/^(?:I |we )?send a ([A-Z]+) request to "([^"]+)" with form data:$/, async
|
|
|
404
388
|
.join('&');
|
|
405
389
|
|
|
406
390
|
try {
|
|
407
|
-
apiResponse = await
|
|
408
|
-
method: method,
|
|
409
|
-
url: url,
|
|
410
|
-
headers: {
|
|
411
|
-
...apiHeaders,
|
|
412
|
-
'Content-Type': 'application/x-www-form-urlencoded'
|
|
413
|
-
},
|
|
414
|
-
data: formData,
|
|
415
|
-
validateStatus: function (status) {
|
|
416
|
-
return true; // Don't throw on any status code
|
|
417
|
-
}
|
|
418
|
-
});
|
|
391
|
+
apiResponse = await sendApiRequest(this, method, url, { ...apiHeaders, 'Content-Type': 'application/x-www-form-urlencoded' }, formData);
|
|
419
392
|
} catch (error) {
|
|
420
|
-
throw friendly(`${method} request with form data
|
|
393
|
+
throw friendly(`${method} request with form data to "${url}" failed.`, error);
|
|
421
394
|
}
|
|
422
395
|
});
|
|
423
396
|
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
|
|
11
11
|
const { Then } = require('@cucumber/cucumber');
|
|
12
12
|
const assert = require('assert');
|
|
13
|
-
const axios = require('axios');
|
|
14
13
|
const { buildSelector, getLocatorText, friendly } = require('./webship');
|
|
14
|
+
const { getLastResponse } = require('./response.steps');
|
|
15
15
|
|
|
16
16
|
// ---------------------------------------------------------------------------
|
|
17
17
|
// Plain page-text
|
|
@@ -341,27 +341,23 @@ Then(/^(the )*response should( not)* contain "([^"]*)?"$/, async function (theCa
|
|
|
341
341
|
/**
|
|
342
342
|
* Assert that the current page's response status is or is not a given code.
|
|
343
343
|
*
|
|
344
|
+
* The status is the one the browser received for the page it is showing
|
|
345
|
+
* (after any redirect), read from the browser session itself.
|
|
346
|
+
*
|
|
344
347
|
* Example #1: Then the response status code should be 200
|
|
345
348
|
* Example #2: And the response status code should not be 404
|
|
346
|
-
* Example #3: Then the response status code should be
|
|
349
|
+
* Example #3: Then the response status code should be 404
|
|
350
|
+
* Example #4: Then the response status code should not be 500
|
|
351
|
+
* Example #5: When I am on "/admin"
|
|
352
|
+
* Then the response status code should be 403
|
|
347
353
|
*
|
|
348
354
|
*/
|
|
349
355
|
Then(/^(the )*response status code should( not)* be (\d+)$/, async function (theCase, notCase, expectedStatusCode) {
|
|
350
|
-
const
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
} else {
|
|
357
|
-
assert.strictEqual(status, parseInt(expectedStatusCode), `Expected status ${expectedStatusCode} but got ${status}`);
|
|
358
|
-
}
|
|
359
|
-
} catch (error) {
|
|
360
|
-
const status = error.response ? error.response.status : null;
|
|
361
|
-
if (notCase) {
|
|
362
|
-
if (status !== null) assert.notStrictEqual(status, parseInt(expectedStatusCode));
|
|
363
|
-
} else {
|
|
364
|
-
assert.strictEqual(status, parseInt(expectedStatusCode), `Expected status ${expectedStatusCode} but got ${status}`);
|
|
365
|
-
}
|
|
356
|
+
const expected = parseInt(expectedStatusCode, 10);
|
|
357
|
+
const { status } = await getLastResponse(this);
|
|
358
|
+
if (notCase) {
|
|
359
|
+
assert.notStrictEqual(status, expected, `Status code should NOT be ${expected}`);
|
|
360
|
+
} else {
|
|
361
|
+
assert.strictEqual(status, expected, `Expected status ${expected} but got ${status}`);
|
|
366
362
|
}
|
|
367
363
|
});
|
|
@@ -1,38 +1,85 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
// Inspect HTTP response
|
|
3
|
+
// Inspect the HTTP response of the page the browser is showing: its status
|
|
4
|
+
// code and its headers. Both are read from the response the browser itself
|
|
5
|
+
// received for the main document, so they follow the browser session — its
|
|
6
|
+
// cookies, its HTTPS settings, any stubbed route — and never a second request
|
|
7
|
+
// sent from outside the browser.
|
|
4
8
|
|
|
5
9
|
const { Then, Before } = require('@cucumber/cucumber');
|
|
6
10
|
const assert = require('assert');
|
|
11
|
+
const { friendly } = require('./webship');
|
|
7
12
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
const trackedPages = new WeakSet();
|
|
14
|
+
|
|
15
|
+
// A URL without its fragment: "#section" never loads a new document.
|
|
16
|
+
function withoutHash(url) {
|
|
17
|
+
return String(url || '').split('#')[0];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Record the response of every main-document navigation on the current page.
|
|
21
|
+
// Steps that swap the page (auth state, basic authentication, a new session)
|
|
22
|
+
// get the listener on their first use of the response steps.
|
|
23
|
+
function trackResponses(world) {
|
|
24
|
+
const page = world.page;
|
|
25
|
+
if (!page || trackedPages.has(page)) return;
|
|
26
|
+
trackedPages.add(page);
|
|
27
|
+
page.on('response', (resp) => {
|
|
14
28
|
try {
|
|
15
|
-
if (resp.request().isNavigationRequest() && resp.frame() ===
|
|
16
|
-
|
|
17
|
-
|
|
29
|
+
if (resp.request().isNavigationRequest() && resp.frame() === page.mainFrame()) {
|
|
30
|
+
world._lastResponse = { page, url: resp.url(), status: resp.status(), headers: resp.headers() };
|
|
31
|
+
world._lastResponseHeaders = world._lastResponse.headers;
|
|
32
|
+
world._lastResponseStatus = world._lastResponse.status;
|
|
18
33
|
}
|
|
19
34
|
} catch (e) { /* ignore */ }
|
|
20
35
|
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
Before({ order: 100 }, async function () {
|
|
39
|
+
this._lastResponse = null;
|
|
40
|
+
this._lastResponseHeaders = null;
|
|
41
|
+
this._lastResponseStatus = null;
|
|
42
|
+
trackResponses(this);
|
|
21
43
|
});
|
|
22
44
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
45
|
+
/**
|
|
46
|
+
* The response of the document the page is showing: { status, headers }.
|
|
47
|
+
*
|
|
48
|
+
* The recorded navigation response answers when it belongs to the current
|
|
49
|
+
* page and URL. Anything else — a page opened before the listener was there,
|
|
50
|
+
* a history move served from the browser cache, a URL changed by the History
|
|
51
|
+
* API — is asked for again through the browser context, which shares the
|
|
52
|
+
* session cookies and the HTTPS settings of the page.
|
|
53
|
+
*/
|
|
54
|
+
async function getLastResponse(world) {
|
|
55
|
+
const page = world.page;
|
|
56
|
+
if (!page) throw friendly('No page is open, so there is no response to read.');
|
|
57
|
+
trackResponses(world);
|
|
58
|
+
const url = page.url();
|
|
59
|
+
const last = world._lastResponse;
|
|
60
|
+
// 304 Not Modified only says the cached copy is still good: ask again for
|
|
61
|
+
// the status of the document itself.
|
|
62
|
+
if (last && last.page === page && last.status !== 304 && withoutHash(last.url) === withoutHash(url)) {
|
|
63
|
+
return last;
|
|
64
|
+
}
|
|
65
|
+
if (!url || url === 'about:blank') {
|
|
66
|
+
throw friendly({
|
|
67
|
+
message: 'No page has been opened yet, so there is no response to read.',
|
|
68
|
+
hint: 'open a page first, for example: Given I am on "/".',
|
|
69
|
+
});
|
|
70
|
+
}
|
|
28
71
|
try {
|
|
29
|
-
const resp = await
|
|
30
|
-
return resp.headers();
|
|
72
|
+
const resp = await page.request.get(url);
|
|
73
|
+
return { page, url, status: resp.status(), headers: resp.headers() };
|
|
31
74
|
} catch (e) {
|
|
32
|
-
|
|
75
|
+
throw friendly(`Could not read the response of "${url}".`, e);
|
|
33
76
|
}
|
|
34
77
|
}
|
|
35
78
|
|
|
79
|
+
async function getHeaders(world) {
|
|
80
|
+
return (await getLastResponse(world)).headers || {};
|
|
81
|
+
}
|
|
82
|
+
|
|
36
83
|
/**
|
|
37
84
|
* Assert the most recent navigation response carried a header (case-insensitive).
|
|
38
85
|
*
|
|
@@ -96,3 +143,5 @@ Then('the response header {string} should not contain the value {string}', async
|
|
|
96
143
|
if (!key) return;
|
|
97
144
|
assert.ok(String(h[key]).indexOf(value) === -1, `Header "${name}" should not contain "${value}".`);
|
|
98
145
|
});
|
|
146
|
+
|
|
147
|
+
module.exports = { getLastResponse };
|