-
Notifications
You must be signed in to change notification settings - Fork 11
/
fetch-use-token.js
62 lines (57 loc) · 2.59 KB
/
fetch-use-token.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
async function testPage(page) {
await page.setRequestInterception(true);
// Intercept the next request, change its method to POST and add the request body
page.once("request", async interceptedRequest => {
interceptedRequest.continue({
method: "POST",
postData: '{"username": "exampleUser","password": "examplePassword"}',
headers: {
...interceptedRequest.headers(),
"content-type": "application/json"
}
});
});
// Sending the first request to get the token
const response = await page.goto("https://private-xxxxx.apiary-mock.com/authenticate");
bodyJSON = await response.json();
// response.text() prints out the response body as a string, useful if you're not using JSON
// response.json() parses the response body JSON and throws an error if it isn't valid JSON
console.log({
url: response.url(),
statusCode: response.status(),
body: await response.text(),
bodyJSON
});
// Secure URL which requires authentication and the appropriate headers which we'll add to the request
const secureURL = "https://private-xxxxx.apiary-mock.com/exampleSecureEndpoint";
const additionalHeaders = {
"content-type": "application/json",
"Authorization": `Bearer ${bodyJSON.example_header_token}`
};
// Intercept the next request, change its method to POST and add the token from the response we got from the first request
// Use page.on instead of page.once and check against URLs you want to intercept when intercepting additional requests after the
// first one, since that way we can avoid intercepting other requests made from the first page instead of the one we need to modify
page.on("request", async interceptedRequest => {
// Ensure that the URL capitalization is the same when performing the comparison
if (interceptedRequest.url() === secureURL.toLowerCase()) {
interceptedRequest.continue({
method: "POST",
postData: { "token": `${bodyJSON.example_body_token}` },
headers: {
...interceptedRequest.headers(),
...additionalHeaders
}
});
} else {
interceptedRequest.continue();
}
});
// Sending the second request using the info from the first response
const result = await page.goto(secureURL);
console.log({
url: result.url(),
statusCode: result.status(),
body: await result.text()
});
}
module.exports = testPage;