/
additions.js
45 lines (36 loc) · 1.26 KB
/
additions.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
export default fixtureAdditions;
import { URL } from "url";
import mapValuesDeep from "./map-values-deep.js";
function fixtureAdditions(state, { id, fixture }) {
fixture = mapValuesDeep(fixture, (value) => {
if (typeof value !== "string") {
return value;
}
// e.g. https://api.github.com/user -> http://localhost/api.github.com/fixturesid123/user
return value.replace(
/https?:\/\/([^/]+)\//g,
`${state.fixturesUrl}/$1/${id}/`
);
});
if (fixture.responseIsBinary) {
fixture.response = Buffer.from(fixture.response, "hex");
} else {
fixture.headers["content-length"] = String(
calculateBodyLength(fixture.response)
);
}
// We make the fixtures id part of the hostname to avoid conflicts with other
// mocks loaded with nock as they are global for the same process. Looking up
// fixtures by unique hostname is more efficient, too.
const { protocol, hostname, port } = new URL(fixture.scope);
const newHostame = `fixtures-${id}.${hostname}`;
fixture.scope = `${protocol}//${newHostame}:${port}`;
fixture.reqheaders.host = newHostame;
return fixture;
}
function calculateBodyLength(body) {
if (typeof body !== "string") {
body = JSON.stringify(body);
}
return Buffer.byteLength(body, "utf8");
}