forked from jd1378/deno-another-cookiejar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_wrapper_test.ts
619 lines (550 loc) · 18 KB
/
fetch_wrapper_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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
import {
assert,
assertArrayIncludes,
assertEquals,
assertFalse,
assertRejects,
assertStrictEquals,
} from "https://deno.land/std@0.139.0/testing/asserts.ts";
import { CookieJar } from "./cookie_jar.ts";
import { wrapFetch } from "./fetch_wrapper.ts";
import { delay } from "https://deno.land/std@0.139.0/async/delay.ts";
import { Cookie } from "./cookie.ts";
function drop(resourceName: string) {
const rt: Deno.ResourceMap = Deno.resources();
for (const rid in rt) {
if (rt[rid] == resourceName) {
try {
Deno.close(Number(rid));
return true;
} catch {
return false;
}
}
}
}
const serverOnePort = 53250;
const serverTwoPort = 53251;
const serverOneHostname = "127.0.0.1";
const serverTwoHostname = "localhost";
const serverOneOptions = {
hostname: serverOneHostname,
port: serverOnePort,
};
const serverTwoOptions = {
hostname: serverTwoHostname,
port: serverTwoPort,
};
const serverOneUrl = `http://${serverOneHostname}:${serverOnePort}`;
const serverTwoUrl = `http://${serverTwoHostname}:${serverTwoPort}`;
async function serverHandler(request: Request): Promise<Response> {
const { pathname } = new URL(request.url);
if (pathname === "/echo_headers") {
const headers = JSON.stringify([...request.headers]);
return new Response(headers, { status: 200 });
} else if (pathname === "/echo_body") {
const body = await request.text();
return new Response(body, {
status: 200,
headers: { "Content-Type": "application/json" },
});
} else if (pathname === "/echo_foo") {
const body = "foo";
return new Response(body, {
status: 200,
headers: { "Content-Type": "application/text" },
});
} else if (pathname === "/echo_method") {
return new Response(request.method, { status: 200 });
} else if (pathname === "/set1") {
const headers = new Headers();
headers.append("Set-Cookie", "foo=bar; Path=/; HttpOnly");
headers.append("Set-Cookie", "baz=thud; Path=/; Secure");
return new Response("ok", { status: 200, headers });
} else if (pathname === "/set2") {
const headers = new Headers();
headers.append("Set-Cookie", "echo=one; Path=/; HttpOnly");
headers.append("Set-Cookie", "third=echo; Path=/; Secure");
return new Response("ok", { status: 200, headers });
} else if (pathname === "/redirect_to_server_two_set1") {
return Response.redirect(serverTwoUrl + "/set1");
} else if (pathname === "/redirect_to_server_two_set1_with_cookie") {
const headers = new Headers({
"Set-Cookie": "redirect_cookie=bar; Path=/; HttpOnly",
"location": serverTwoUrl + "/set1",
});
return new Response(null, { status: 301, headers });
} else if (pathname === "/redirect_to_server_two") {
const headers = new Headers({
"location": serverTwoUrl + "/echo_headers",
});
return new Response(null, { status: 301, headers });
} else if (pathname === "/redirect_to_server_two_echo_method") {
const headers = new Headers({
"location": serverTwoUrl + "/echo_method",
});
return new Response(null, { status: 301, headers });
} else if (pathname === "/redirect_to_server_two_echo_body") {
const headers = new Headers({
"location": serverTwoUrl + "/echo_body",
});
return new Response(null, { status: 301, headers });
} else if (pathname === "/redirect_to_echo_foo") {
const headers = new Headers({
"location": "/echo_foo",
});
return new Response(null, { status: 301, headers });
} else if (pathname === "/redirect_loop") {
const headers = new Headers({
"location": serverOneUrl + "/redirect_loop_2",
});
return new Response(null, { status: 301, headers });
} else if (pathname === "/redirect_loop_2") {
const headers = new Headers({
"location": serverOneUrl + "/redirect_loop",
});
return new Response(null, { status: 302, headers });
} else {
const bodyContent = request.headers.get("cookie") || "";
return new Response(bodyContent, { status: 200 });
}
}
type ListenAndServeOptions = {
hostname: string;
port: number;
abortController: AbortController;
};
async function listenAndServe(options: ListenAndServeOptions) {
const { hostname, port, abortController } = options;
const listener = Deno.listen({ hostname, port });
const handleAbort = () => {
abortController.signal.removeEventListener("abort", handleAbort);
listener.close();
drop("httpConn");
};
abortController.signal.addEventListener("abort", handleAbort);
try {
for await (const conn of listener) {
for await (
const { respondWith, request } of Deno.serveHttp(conn)
) {
respondWith(serverHandler(request));
}
}
} catch {
drop("httpConn");
}
}
function runServer(
options: Omit<ListenAndServeOptions, "abortController">,
) {
const abortController = new AbortController();
listenAndServe({
...options,
abortController,
}).catch((e) => {
abortController.abort();
delay(10);
throw e;
});
return abortController;
}
console.log(
"Test HTTP webserver running at:",
serverOneUrl,
serverTwoUrl,
);
console.log('GET "/" echos "cookie" header, GET "/set1" sets two cookies');
Deno.test("WrappedFetch saves cookies from set-cookie header", async () => {
const abortController = runServer(serverOneOptions);
try {
const cookieJar = new CookieJar();
const wrappedFetch = wrapFetch({ cookieJar });
const response = await wrappedFetch(serverOneUrl + "/set1");
await response.body?.cancel();
assertStrictEquals(cookieJar.getCookie({ name: "foo" })?.value, "bar");
assertStrictEquals(cookieJar.getCookie({ name: "baz" })?.value, "thud");
} finally {
abortController.abort();
}
});
Deno.test("WrappedFetch can use the Request and still inject cookies", async () => {
const abortController = runServer(serverOneOptions);
try {
const cookieJar = new CookieJar([
new Cookie({
name: "goo",
value: "baz",
expires: new Date("2600 10 10").valueOf(),
domain: new URL(serverOneUrl).hostname,
}),
]);
const wrappedFetch = wrapFetch({ cookieJar });
const request = new Request(serverOneUrl + "/echo_headers", {
headers: { foo: "bar", zoo: "bum" },
});
const res = await wrappedFetch(request, {
headers: [["thud", "fum"], ["zoo", "gut"]],
})
.then((r) => r.json());
assertArrayIncludes(res, [
["foo", "bar"],
["zoo", "gut"], // not 'bum' because init should replace original request options
["thud", "fum"],
[
"cookie",
"goo=baz",
],
]);
} finally {
abortController.abort();
}
});
Deno.test("WrappedFetch body is not tampered with", async () => {
const abortController = runServer(serverOneOptions);
try {
const wrappedFetch = wrapFetch();
const bodyData = { foo: "bar" };
const request = new Request(serverOneUrl + "/echo_body", {
body: JSON.stringify(bodyData),
method: "post",
headers: {
"Content-Type": "application/json",
},
});
const res = await wrappedFetch(request).then((r) => r.json());
assertEquals(res, bodyData);
} finally {
abortController.abort();
}
});
Deno.test("WrappedFetch merges headers with cookie header", async () => {
const abortController = runServer(serverOneOptions);
try {
const cookieJar = new CookieJar();
const wrappedFetch = wrapFetch({ cookieJar });
await wrappedFetch(serverOneUrl + "/set1").then((r) => r.text());
assertStrictEquals(cookieJar.getCookie({ name: "foo" })?.value, "bar");
const headersToSend = new Headers();
headersToSend.set("user-agent", "something");
let cookieString = await wrappedFetch(serverOneUrl + "/", {
headers: headersToSend,
}).then((r) => r.text());
assertStrictEquals(cookieString, "foo=bar");
// second type
cookieString = await wrappedFetch(serverOneUrl + "/", {
headers: {
"user-agent": "something",
},
}).then((r) => r.text());
assertStrictEquals(cookieString, "foo=bar");
// third type
cookieString = await wrappedFetch(serverOneUrl + "/", {
headers: [
["user-agent", "something"],
],
}).then((r) => r.text());
assertStrictEquals(cookieString, "foo=bar");
} finally {
abortController.abort();
}
});
Deno.test("WrappedFetch doesn't send secure cookies over unsecure urls", async () => {
const abortController = runServer(serverOneOptions);
try {
const cookieJar = new CookieJar();
const wrappedFetch = wrapFetch({ cookieJar });
await wrappedFetch(serverOneUrl + "/set1").then((r) => r.text());
assertStrictEquals(cookieJar.getCookie({ name: "foo" })?.value, "bar");
assertStrictEquals(cookieJar.getCookie({ name: "baz" })?.value, "thud");
// since `baz` cookie is secure, it should not be sent with fetch
assertStrictEquals(cookieJar.getCookie({ name: "baz" })?.secure, true);
const cookieString = await wrappedFetch(serverOneUrl + "/").then((r) =>
r.text()
);
assertStrictEquals(cookieString, "foo=bar");
} finally {
abortController.abort();
}
});
Deno.test("response.redirected is set when redirected", async () => {
const abortController = runServer(serverOneOptions);
const abortController2 = runServer(serverTwoOptions);
try {
const wrappedFetch = wrapFetch();
const response = await wrappedFetch(
serverOneUrl + "/redirect_to_server_two_set1",
);
await response.body?.cancel();
assertStrictEquals(response.redirected, true);
} finally {
abortController.abort();
abortController2.abort();
}
});
Deno.test("response.redirected is not set when not redirected", async () => {
const abortController = runServer(serverOneOptions);
try {
const wrappedFetch = wrapFetch();
const response = await wrappedFetch(
serverOneUrl + "/echo_headers",
);
await response.body?.cancel();
assertStrictEquals(response.redirected, false);
} finally {
abortController.abort();
}
});
Deno.test("Sets the correct domain in cookies when 302-redirected", async () => {
const abortController = runServer(serverOneOptions);
const abortController2 = runServer(serverTwoOptions);
try {
const cookieJar = new CookieJar();
const wrappedFetch = wrapFetch({ cookieJar });
await wrappedFetch(serverOneUrl + "/redirect_to_server_two_set1").then((
r,
) => r.text());
assertStrictEquals(
cookieJar.getCookie({ name: "foo" })?.domain,
`${serverTwoHostname}`,
);
} finally {
abortController.abort();
abortController2.abort();
}
});
Deno.test("Gets cookies both from 302-redirected and 200 response", async () => {
const abortController = runServer(serverOneOptions);
const abortController2 = runServer(serverTwoOptions);
try {
const cookieJar = new CookieJar();
const wrappedFetch = wrapFetch({ cookieJar });
await wrappedFetch(
serverOneUrl + "/redirect_to_server_two_set1_with_cookie",
).then((
r,
) => r.text());
assertStrictEquals(
cookieJar.getCookie({ name: "foo" })?.domain,
`${serverTwoHostname}`,
);
assertStrictEquals(
cookieJar.getCookie({ name: "redirect_cookie" })?.domain,
`${serverOneHostname}`,
);
} finally {
abortController.abort();
abortController2.abort();
}
});
Deno.test("Redirect loop ends when it reaches 20 redirects", async () => {
const abortController = runServer(serverOneOptions);
try {
const wrappedFetch = wrapFetch();
const pathname = "/redirect_loop";
await assertRejects(
async () => {
await wrappedFetch(serverOneUrl + pathname);
},
Error,
`Reached maximum redirect of 20 for URL: ${serverOneUrl}`,
);
} finally {
abortController.abort();
}
});
Deno.test("Respects when request.init.redirect is set to 'manual'", async () => {
const abortController = runServer(serverOneOptions);
try {
const wrappedFetch = wrapFetch();
const pathname = "/redirect_to_server_two_set1";
const response = await wrappedFetch(
serverOneUrl + pathname,
{
redirect: "manual",
},
);
await response.text();
assertStrictEquals(response.url, serverOneUrl + pathname);
} finally {
abortController.abort();
}
});
Deno.test("Respects when request.init.redirect is set to 'error'", async () => {
const abortController = runServer(serverOneOptions);
try {
const wrappedFetch = wrapFetch();
const pathname = "/redirect_to_server_two_set1";
const fn = async () => {
await wrappedFetch(serverOneUrl + pathname, {
redirect: "error",
});
};
await assertRejects(
async () => await fn(),
Error,
`URI requested responded with a redirect and redirect mode is set to error: ${serverOneUrl}${pathname}`,
);
} finally {
abortController.abort();
}
});
Deno.test("Cookies are not send cross domain", async () => {
const abortController = runServer(serverOneOptions);
const abortController2 = runServer(serverTwoOptions);
try {
const cookieJar = new CookieJar();
const wrappedFetch = wrapFetch({ cookieJar });
// server 1
await wrappedFetch(serverOneUrl + "/set1").then((r) => r.text());
assertStrictEquals(cookieJar.getCookie({ name: "foo" })?.value, "bar");
assertStrictEquals(cookieJar.getCookie({ name: "baz" })?.value, "thud");
// server 2
await wrappedFetch(serverTwoUrl + "/set2").then((r) => r.text());
assertStrictEquals(cookieJar.getCookie({ name: "echo" })?.value, "one");
assertStrictEquals(cookieJar.getCookie({ name: "third" })?.value, "echo");
// we got all the cookies, not should try to see if we send them right
let cookieString;
// try server1
cookieString = await wrappedFetch(serverOneUrl + "/").then((r) => r.text());
assertStrictEquals(cookieString, "foo=bar");
// try server2
cookieString = await wrappedFetch(serverTwoUrl + "/").then((r) => r.text());
assertStrictEquals(cookieString, "echo=one");
} finally {
abortController.abort();
abortController2.abort();
}
});
Deno.test("using wrapped fetch doesn't mutate user's initial init", async () => {
const abortController = runServer(serverOneOptions);
try {
const cookieJar = new CookieJar();
const wrappedFetch = wrapFetch({ cookieJar });
const abortController = new AbortController();
const originalUserInit: RequestInit = {
body: "foo",
cache: "reload",
credentials: "omit",
headers: [["a", "b"]],
integrity: "c",
keepalive: false,
method: "POST",
mode: "no-cors",
redirect: "follow",
referrer: "unknwn",
referrerPolicy: "unsafe-url",
signal: abortController.signal,
window: null,
};
const userInit: RequestInit = {
body: "foo",
cache: "reload",
credentials: "omit",
headers: [["a", "b"]],
integrity: "c",
keepalive: false,
method: "POST",
mode: "no-cors",
redirect: "follow",
referrer: "unknwn",
referrerPolicy: "unsafe-url",
signal: abortController.signal,
window: null,
};
const response = await wrappedFetch(serverOneUrl + "/set1", userInit);
await response.body?.cancel();
assertEquals(originalUserInit, userInit);
} finally {
abortController.abort();
}
});
Deno.test("doesn't send sensitive headers after redirect to different domains", async () => {
const abortController = runServer(serverOneOptions);
const abortController2 = runServer(serverTwoOptions);
try {
const cookieJar = new CookieJar();
const wrappedFetch = wrapFetch({ cookieJar });
const res = await wrappedFetch(
serverOneUrl + "/redirect_to_server_two",
{
headers: {
"foo": "bar",
"authorization": "foo",
"www-authenticate": "foo",
"cookie": "foo",
"cookie2": "foo",
},
},
).then((r) => r.json());
const resHeaders = new Headers(res);
assert(resHeaders.has("foo"), "request didn't have `foo` in headers");
// should not contain the headers above
assertFalse(
resHeaders.has("www-authenticate"),
"request had `www-authenticate` in headers",
);
assertFalse(
resHeaders.has("authorization"),
"request had `authorization` in headers",
);
assertFalse(
resHeaders.get("cookie"),
"`cookie` header is not empty",
);
assertFalse(
resHeaders.has("cookie2"),
"`cookie2` header shouldn't be sent! ",
);
} finally {
abortController.abort();
abortController2.abort();
}
});
Deno.test("doesn't POST body after redirection", async () => {
const abortController = runServer(serverOneOptions);
const abortController2 = runServer(serverTwoOptions);
try {
const cookieJar = new CookieJar();
const wrappedFetch = wrapFetch({ cookieJar });
const res = await wrappedFetch(
serverOneUrl + "/redirect_to_server_two_echo_method",
{ body: "FOO", method: "POST" },
).then((r) => r.text());
assertEquals(res, "GET", "method is NOT changed to GET after redirection");
const echoRes = await wrappedFetch(
serverOneUrl + "/redirect_to_server_two_echo_body",
{ body: "FOO", method: "POST" },
).then((r) => r.text());
assertEquals(echoRes, "", "no content should be sent after redirection");
const headerRes = await wrappedFetch(
serverOneUrl + "/redirect_to_server_two",
{ body: "FOO", method: "POST" },
).then((r) => r.text());
assertFalse(
headerRes.includes("content-length"),
"content-length header must not be sent after redirection",
);
} finally {
abortController.abort();
abortController2.abort();
}
});
Deno.test("handles path redirections", async () => {
const abortController2 = runServer(serverTwoOptions);
try {
const wrappedFetch = wrapFetch();
const res = await wrappedFetch(
serverTwoUrl + "/redirect_to_echo_foo",
{ method: "GET" },
).then((r) => r.text());
console.log(res);
assertEquals(
res,
"foo",
"path redirect breaks the app",
);
} finally {
abortController2.abort();
}
});