forked from gooddata/gooddata-ui-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integrated-test.setup.js
74 lines (60 loc) · 2.45 KB
/
integrated-test.setup.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
63
64
65
66
67
68
69
70
71
72
73
74
// (C) 2020 GoodData Corporation
import https from "https";
/*
* integrated tests run against wiremock which uses self-signed certificates. tell node.js not to bomb and
* instead just issue warnings.
*
* alas, this does not work at the moment due to jest bug: https://github.com.cnpmjs.org/facebook/jest/issues/8449
* the var must be set outside of jest in package.json. Once the bug gets fixed the variable can be removed from
* package json.
*/
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
/*
* Cookie domain rewriting - this patching is in place because Wiremock standalone does not support cookie
* rewriting. It is possible through its programmatic API but that would mean we cannot use the 3rd party
* standalone docker image.
*
* Thus, wiremock running on localhost returns platform's cookies with Domain=secure... but trying to add that
* into the cookie-jar will bomb (Cookie not for current host's domain).
*
* So.. one way to go around this is to do rewriting on client side - while processing the request:
*
* - monkey-patch the https module (all communication is through https, never http)
* - before sending request to real https, add Domain=secure...; to the Cookie (if any)
* - create the real request
* - instrument it with response listener that will be stripping Domain=secure...' from set-cookie
* - return the request
*
* This is transparent to the node-fetch that is used by the api-client.
*/
const originalRequest = https.request;
function stripDomain(cookies, domain) {
if (!Array.isArray(cookies)) {
return cookies.replace(`Domain=${domain};`, "");
}
return cookies.map((c) => {
return c.replace(`Domain=${domain};`, "");
});
}
function addDomain(cookies, domain) {
if (!Array.isArray(cookies)) {
return cookies + `; Domain=${domain}`;
}
return cookies.map((c) => {
return c + `; Domain=${domain}`;
});
}
https.request = function cookieRewritingRequestFactory(_options, req) {
const requestCookies = req.headers["cookie"];
if (requestCookies) {
req.headers["cookie"] = addDomain(requestCookies, "secure.gooddata.com");
}
const newRequest = originalRequest.apply(this, arguments);
newRequest.on("response", (res) => {
const responseCookies = res.headers["set-cookie"];
if (responseCookies) {
res.headers["set-cookie"] = stripDomain(responseCookies, "secure.gooddata.com");
}
});
return newRequest;
};