-
Notifications
You must be signed in to change notification settings - Fork 0
/
vary_test.ts
337 lines (280 loc) · 11 KB
/
vary_test.ts
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import {
assert,
assertStrictEq,
assertThrows,
assertEquals,
} from "https://deno.land/std/testing/asserts.ts";
import { vary, append } from "../mod.ts";
const { test } = Deno;
test("vary(res, field) arguments field should accept string", function () {
const header = new Headers();
vary(header, "foo");
assertStrictEq(header.get("vary"), "foo");
});
test("vary(res, field) arguments field should accept array of string", function () {
const header = new Headers();
vary(header, ["foo", "bar"]);
assertStrictEq(header.get("vary"), "foo, bar");
});
test("vary(res, field) arguments field should accept string that is Vary header", function () {
const header = new Headers();
vary(header, "foo, bar");
assertStrictEq(header.get("vary"), "foo, bar");
});
test('vary(res, field) arguments field should not allow separator ":"', function () {
const header = new Headers();
assertThrows(
() => vary(header, "invalid:header"),
TypeError,
"field argument contains an invalid header name `invalid:header`",
);
});
test('vary(res, field) arguments field should not allow separator " "', function () {
const header = new Headers();
assertThrows(
() => vary(header, "invalid header"),
TypeError,
"field argument contains an invalid header name `invalid header`",
);
});
test("vary(res, field) when no Vary should set value", function () {
const header = new Headers();
vary(header, "Origin");
assertStrictEq(header.get("vary"), "Origin");
});
test("vary(res, field) when no Vary should set value with multiple calls", function () {
const header = new Headers();
vary(header, ["Origin", "User-Agent"]);
assertStrictEq(header.get("vary"), "Origin, User-Agent");
});
test("vary(res, field) when no Vary should preserve case", function () {
const header = new Headers();
vary(header, ["ORIGIN", "user-agent", "AccepT"]);
assertStrictEq(header.get("vary"), "ORIGIN, user-agent, AccepT");
});
test("vary(res, field) when no Vary should not set Vary on empty array", function () {
const header = new Headers();
vary(header, []);
shouldNotHaveHeader(header);
});
test("vary(res, field) when existing Vary should set value", function () {
const header = new Headers();
alterVary("Accept", "Origin", header);
assertStrictEq(header.get("vary"), "Accept, Origin");
});
test("vary(res, field) when existing Vary should set value with multiple calls", function () {
const header = new Headers([["Vary", "Accept"]]);
vary(header, "Origin");
vary(header, "User-Agent");
assertStrictEq(header.get("vary"), "Accept, Origin, User-Agent");
});
test("vary(res, field) when existing Vary should not duplicate existing value", function () {
const header = new Headers();
alterVary("Accept", "Accept", header);
assertStrictEq(header.get("vary"), "Accept");
});
test("vary(res, field) when existing Vary should compare case-insensitive", function () {
const header = new Headers();
alterVary("Accept", "accEPT", header);
assertStrictEq(header.get("vary"), "Accept");
});
test("vary(res, field) when existing Vary should preserve case", function () {
const header = new Headers();
alterVary("AccepT", ["accEPT", "ORIGIN"], header);
assertStrictEq(header.get("vary"), "AccepT, ORIGIN");
});
test("vary(res, field) when Vary: * should set value", function () {
const header = new Headers();
vary(header, "*");
assertStrictEq(header.get("vary"), "*");
});
test("vary(res, field) when Vary: * should act as if all values already set", function () {
const header = new Headers();
alterVary("*", ["Origin", "User-Agent"], header);
assertStrictEq(header.get("vary"), "*");
});
test("vary(res, field) when Vary: * should erradicate existing values", function () {
const header = new Headers();
alterVary("Accept, Accept-Encoding", "*", header);
assertStrictEq(header.get("vary"), "*");
});
test("vary(res, field) when Vary: * should update bad existing header", function () {
const header = new Headers();
alterVary("Accept, Accept-Encoding, *", "Origin", header);
assertStrictEq(header.get("vary"), "*");
});
test("vary(res, field) when field is string should set value", function () {
const header = new Headers();
vary(header, "Accept");
assertStrictEq(header.get("vary"), "Accept");
});
test("vary(res, field) when field is string should set value when vary header", function () {
const header = new Headers();
vary(header, "Accept, Accept-Encoding");
assertStrictEq(header.get("vary"), "Accept, Accept-Encoding");
});
test("vary(res, field) when field is string should acept LWS", function () {
const header = new Headers();
vary(header, " Accept , Origin ");
assertStrictEq(header.get("vary"), "Accept, Origin");
});
test("vary(res, field) when field is string should handle contained *", function () {
const header = new Headers();
vary(header, "Accept,*");
assertStrictEq(header.get("vary"), "*");
});
test("vary(res, field) when field is array should set value", function () {
const header = new Headers();
vary(header, ["Accept", "Accept-Language"]);
assertStrictEq(header.get("vary"), "Accept, Accept-Language");
});
test("vary(res, field) when field is array should ignore double-entries", function () {
const header = new Headers();
vary(header, ["Accept", "Accept"]);
assertStrictEq(header.get("vary"), "Accept");
});
test("vary(res, field) when field is array should be case-insensitive", function () {
const header = new Headers();
vary(header, ["Accept", "ACCEPT"]);
assertStrictEq(header.get("vary"), "Accept");
});
test("vary(res, field) when field is array should handle contained *", function () {
const header = new Headers();
vary(header, ["Origin", "User-Agent", "*", "Accept"]);
assertStrictEq(header.get("vary"), "*");
});
test("vary(res, field) when field is array should handle existing values", function () {
const header = new Headers();
alterVary(
"Accept, Accept-Encoding",
["origin", "accept", "accept-charset"],
header,
);
assertStrictEq(
header.get("vary"),
"Accept, Accept-Encoding, origin, accept-charset",
);
});
test("append(header, field) arguments field should accept string", function () {
append("", "foo");
// assert.doesNotThrow(append.bind(null, '', 'foo'))
});
test("append(header, field) arguments field should accept string that is Vary header", function () {
append("", "foo, bar");
// assert.doesNotThrow(append.bind(null, '', 'foo, bar'))
});
test("append(header, field) arguments field should accept array of string", function () {
append("", ["foo", "bar"]);
// assert.doesNotThrow(append.bind(null, '', ['foo', 'bar']))
});
test('append(header, field) arguments field should not allow separator ":"', function () {
assertThrows(
() => append("", "invalid:header"),
TypeError,
"field argument contains an invalid header name `invalid:header`",
);
});
test('append(header, field) arguments field should not allow separator " "', function () {
assertThrows(
() => append("", "invalid header"),
TypeError,
"field argument contains an invalid header name `invalid header`",
);
});
test("append(header, field) arguments field should not allow non-token characters", function () {
assertThrows(
() => append("", "invalid\nheader"),
TypeError,
"invalid header name",
);
assertThrows(
() => append("", "invalid\u0080header"),
TypeError,
"invalid header name",
);
});
test("append(header, field) when header empty should set value", function () {
assertStrictEq(append("", "Origin"), "Origin");
});
test("append(header, field) when header empty should set value with array", function () {
assertStrictEq(append("", ["Origin", "User-Agent"]), "Origin, User-Agent");
});
test("append(header, field) when header empty should preserve case", function () {
assertStrictEq(
append("", ["ORIGIN", "user-agent", "AccepT"]),
"ORIGIN, user-agent, AccepT",
);
});
test("append(header, field) when header has values should set value", function () {
assertStrictEq(append("Accept", "Origin"), "Accept, Origin");
});
test("append(header, field) when header has values should set value with array", function () {
assertStrictEq(
append("Accept", ["Origin", "User-Agent"]),
"Accept, Origin, User-Agent",
);
});
test("append(header, field) when header has values should not duplicate existing value", function () {
assertStrictEq(append("Accept", "Accept"), "Accept");
});
test("append(header, field) when header has values should compare case-insensitive", function () {
assertStrictEq(append("Accept", "accEPT"), "Accept");
});
test("append(header, field) when header has values should preserve case", function () {
assertStrictEq(append("Accept", "AccepT"), "Accept");
});
test("append(header, field) when * should set value", function () {
assertStrictEq(append("", "*"), "*");
});
test("append(header, field) when * should act as if all values already set", function () {
assertStrictEq(append("*", "Origin"), "*");
});
test("append(header, field) when * should erradicate existing values", function () {
assertStrictEq(append("Accept, Accept-Encoding", "*"), "*");
});
test("append(header, field) when * should update bad existing header", function () {
assertStrictEq(append("Accept, Accept-Encoding, *", "Origin"), "*");
});
test("append(header, field) when field is string should set value", function () {
assertStrictEq(append("", "Accept"), "Accept");
});
test("append(header, field) when field is string should set value when vary header", function () {
assertStrictEq(
append("", "Accept, Accept-Encoding"),
"Accept, Accept-Encoding",
);
});
test("append(header, field) when field is string should acept LWS", function () {
assertStrictEq(append("", " Accept , Origin "), "Accept, Origin");
});
test("append(header, field) when field is string should handle contained *", function () {
assertStrictEq(append("", "Accept,*"), "*");
});
test("append(header, field) when field is array should set value", function () {
assertStrictEq(
append("", ["Accept", "Accept-Language"]),
"Accept, Accept-Language",
);
});
test("append(header, field) when field is array should ignore double-entries", function () {
assertStrictEq(append("", ["Accept", "Accept"]), "Accept");
});
test("append(header, field) when field is array should be case-insensitive", function () {
assertStrictEq(append("", ["Accept", "ACCEPT"]), "Accept");
});
test("append(header, field) when field is array should handle contained *", function () {
assertStrictEq(append("", ["Origin", "User-Agent", "*", "Accept"]), "*");
});
test("append(header, field) when field is array should handle existing values", function () {
assertStrictEq(
append("Accept, Accept-Encoding", ["origin", "accept", "accept-charset"]),
"Accept, Accept-Encoding, origin, accept-charset",
);
});
function alterVary(h: string, field: string | string[], header: Headers) {
header.set("Vary", h);
vary(header, field);
}
function shouldNotHaveHeader(header: Headers) {
assert(!header.has("vary"), "should not have header vary");
}