-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
149 lines (130 loc) · 4.81 KB
/
index.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
export default normalize;
import calculateBodyLength from "../calculate-body-length.js";
import fixturizePath from "../fixturize-path.js";
import headers from "../headers.js";
import normalizeCommon from "./common.js";
import setIfExists from "../set-if-exists.js";
import toEntityName from "../to-entity-name.js";
async function normalize(scenarioState, fixture) {
// fixture.rawHeaders is an array in the form of ['key1', 'value1', 'key2', 'value2']
// But the order of these can change, e.g. between local tests and CI on GitHub Actions.
// That’s why we turn them into an object before storing the fixtures and turn
// them back into an array before loading
fixture.headers = headers.toObject(fixture.rawHeaders);
delete fixture.rawHeaders;
fixture.path = fixturizePath(scenarioState, fixture.path);
if (fixture.headers.location) {
fixture.headers.location = fixturizePath(
scenarioState,
fixture.headers.location,
);
}
// set all dates to Universe 2017 Keynote time
setIfExists(fixture.headers, "date", "Tue, 10 Oct 2017 16:00:00 GMT");
setIfExists(fixture.headers, "expires", "Tue, 10 Oct 2017 16:00:00 GMT");
setIfExists(
fixture.headers,
"last-modified",
"Tue, 10 Oct 2017 16:00:00 GMT",
);
setIfExists(fixture.headers, "x-ratelimit-reset", "1507651200000");
// Set remaining rate limit to 59 for unauthenticated accounts and
// to 4999 to authenticated accounts
const rateLimitRemaining = parseInt(fixture.headers["x-ratelimit-limit"], 10);
setIfExists(
fixture.headers,
"x-ratelimit-remaining",
String(rateLimitRemaining - 1),
);
// Set used rate limit to 1
setIfExists(fixture.headers, "x-ratelimit-used", 1);
// zero random stuff
setIfExists(fixture.headers, "etag", '"00000000000000000000000000000000"');
setIfExists(fixture.headers, "x-runtime-rack", "0.000000");
setIfExists(
fixture.headers,
"x-github-request-id",
"0000:00000:0000000:0000000:00000000",
);
// leave out headers that tend to change on different environments
delete fixture.headers["accept-ranges"];
delete fixture.headers.region;
delete fixture.headers.server;
delete fixture.headers.vary;
delete fixture.headers["x-runtime-rack"];
// The GitHub API has several endpoints that require a PUT or a PATCH verb
// with an empty body. In that case it’s unclear what content-type to set,
// or if a content-type header is to be set at all. So we remove it from our
// fixtures in that case but set content-length to 0 as required via:
// https://developer.github.com/v3/#http-verbs
if (
(fixture.method === "put" || fixture.method === "patch") &&
!fixture.body
) {
delete fixture.reqheaders["content-type"];
fixture.reqheaders["content-length"] = 0;
}
// normalize content-type value
if (fixture.reqheaders["content-type"]) {
fixture.reqheaders["content-type"] = fixture.reqheaders["content-type"]
.replace(";charset=utf-8", "; charset=utf-8")
.replace("application/json", "application/json; charset=utf-8");
}
const responses = Array.isArray(fixture.response)
? fixture.response
: [fixture.response];
for (let response of responses) {
normalizeCommon(response);
const entityName = toEntityName(response, fixture);
if (entityName) {
await (
await import(`./${entityName}.js`)
).default(scenarioState, response, fixture);
}
}
// update content length
if (/^application\/json/.test(fixture.headers["content-type"])) {
fixture.headers["content-length"] = String(
calculateBodyLength(fixture.response),
);
}
if (fixture.responseIsBinary) {
fixture.headers["content-length"] = Buffer.from(
fixture.response,
"hex",
).length;
}
// remove `Transfer-Encoding: chunked` headers:
// https://github.com/octokit/fixtures/issues/97
if (fixture.headers["transfer-encoding"] === "chunked") {
delete fixture.headers["transfer-encoding"];
}
// normalize link header if response is paginated
if (fixture.headers.link) {
fixture.headers.link = fixture.headers.link.replace(
/https:\/\/[^;]+/g,
(url) => fixturizePath(scenarioState, url),
);
}
if (fixture.body) {
fixture.reqheaders["content-length"] = calculateBodyLength(fixture.body);
}
// handle redirect response
if (fixture.status > 300 && fixture.status < 400) {
fixture.headers.location = fixturizePath(
scenarioState,
fixture.headers.location,
);
if (fixture.response.url) {
fixture.response.url = fixture.headers.location;
}
}
// remove varnish headers if present
setIfExists(fixture.headers, "x-varnish", "1000");
setIfExists(fixture.headers, "age", "0");
if (fixture.headers["x-github-media-type"]) {
fixture.headers["x-github-media-type"] =
fixture.headers["x-github-media-type"];
}
return fixture;
}