/
index.spec.ts
97 lines (81 loc) · 2.76 KB
/
index.spec.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
import { jest, expect, describe } from "@jest/globals";
import { Request, Response } from "servie/dist/node";
import { redirects } from "./index";
describe("popsicle redirects", () => {
it("should follow 302 redirect", async () => {
const spy = jest.fn(async (req: Request) => {
if (spy.mock.calls.length === 1) {
return new Response(null, {
status: 302,
headers: {
Location: "/test",
},
});
}
expect(req.url).toEqual("http://example.com/test");
return new Response(null, { status: 200 });
});
const transport = redirects(spy);
const res = await transport(new Request("http://example.com"), async () => {
throw new TypeError("Unexpected response");
});
expect(res.status).toEqual(200);
expect(spy).toHaveBeenCalledTimes(2);
});
describe("secure headers", () => {
const headers = {
cookie: "example_cookie",
authorization: "example_authorization",
};
it("should maintain cookies when staying with original host", async () => {
const spy = jest.fn(async (req: Request) => {
if (spy.mock.calls.length === 1) {
return new Response(null, {
status: 302,
headers: {
Location: "/test",
},
});
}
expect(req.url).toEqual("http://example.com/test");
expect(req.headers.get("Cookie")).toBe(headers.cookie);
expect(req.headers.get("Authorization")).toBe(headers.authorization);
return new Response(null, { status: 200 });
});
const transport = redirects(spy);
const res = await transport(
new Request("http://example.com", { headers }),
async () => {
throw new TypeError("Unexpected response");
}
);
expect(res.status).toEqual(200);
expect(spy).toHaveBeenCalledTimes(2);
});
it("should discard cookies when leaving original host", async () => {
const spy = jest.fn(async (req: Request) => {
if (spy.mock.calls.length === 1) {
return new Response(null, {
status: 302,
headers: {
Location: "https://example.com",
},
});
}
expect(req.url).toEqual("https://example.com/");
expect(req.headers.get("Cookie")).toBe(null);
expect(req.headers.get("Authorization")).toBe(null);
return new Response(null, { status: 200 });
});
const transport = redirects(spy);
const res = await transport(
new Request("http://example.com", { headers }),
async () => {
throw new TypeError("Unexpected response");
}
);
expect(res.status).toEqual(200);
expect(spy).toHaveBeenCalledTimes(2);
});
});
});