-
Notifications
You must be signed in to change notification settings - Fork 546
/
httpRequest.spec.ts
119 lines (94 loc) · 4.06 KB
/
httpRequest.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { ProviderError } from "@aws-sdk/property-provider";
import http, { createServer } from "http";
import nock from "nock";
import { httpRequest } from "./httpRequest";
describe("httpRequest", () => {
const requestSpy = jest.spyOn(http, "request");
let port: number;
const hostname = "localhost";
const path = "/";
const getOpenPort = async (candidatePort = 4321): Promise<number> => {
try {
return new Promise<number>((resolve, reject) => {
const server = createServer();
server.on("error", () => reject());
server.listen(candidatePort);
server.close(() => resolve(candidatePort));
});
} catch (e) {
return await getOpenPort(candidatePort + 1);
}
};
beforeAll(async () => {
port = await getOpenPort();
});
afterEach(() => {
jest.clearAllMocks();
});
describe("returns response", () => {
it("defaults to method GET", async () => {
const expectedResponse = "expectedResponse";
const scope = nock(`http://${hostname}:${port}`).get(path).reply(200, expectedResponse);
const response = await httpRequest({ hostname, path, port });
expect(response.toString()).toStrictEqual(expectedResponse);
expect(requestSpy.mock.results[0].value.socket).toHaveProperty("destroyed", true);
scope.done();
});
it("uses method passed in options", async () => {
const method = "POST";
const expectedResponse = "expectedResponse";
const scope = nock(`http://${hostname}:${port}`).post(path).reply(200, expectedResponse);
const response = await httpRequest({ hostname, path, port, method });
expect(response.toString()).toStrictEqual(expectedResponse);
expect(requestSpy.mock.results[0].value.socket).toHaveProperty("destroyed", true);
scope.done();
});
it("works with IPv6 hostname with encapsulated brackets", async () => {
const expectedResponse = "expectedResponse";
const encapsulatedIPv6Hostname = "[::1]";
const scope = nock(`http://${encapsulatedIPv6Hostname}:${port}`).get(path).reply(200, expectedResponse);
const response = await httpRequest({ hostname: encapsulatedIPv6Hostname, path, port });
expect(response.toString()).toStrictEqual(expectedResponse);
expect(requestSpy.mock.results[0].value.socket).toHaveProperty("destroyed", true);
scope.done();
});
});
describe("throws error", () => {
const errorOnStatusCode = async (statusCode: number) => {
it(`statusCode: ${statusCode}`, async () => {
const scope = nock(`http://${hostname}:${port}`).get(path).reply(statusCode, "continue");
await expect(httpRequest({ hostname, path, port })).rejects.toStrictEqual(
Object.assign(new ProviderError("Error response received from instance metadata service"), { statusCode })
);
expect(requestSpy.mock.results[0].value.socket).toHaveProperty("destroyed", true);
scope.done();
});
};
it("when request throws error", async () => {
const scope = nock(`http://${hostname}:${port}`).get(path).replyWithError("error");
await expect(httpRequest({ hostname, path, port })).rejects.toStrictEqual(
new ProviderError("Unable to connect to instance metadata service")
);
expect(requestSpy.mock.results[0].value.socket).toHaveProperty("destroyed", true);
scope.done();
});
describe("when request returns with statusCode < 200", () => {
[100, 101, 103].forEach(errorOnStatusCode);
});
describe("when request returns with statusCode >= 300", () => {
[300, 400, 500].forEach(errorOnStatusCode);
});
});
it("timeout", async () => {
const timeout = 1000;
const scope = nock(`http://${hostname}:${port}`)
.get(path)
.delay(timeout * 2)
.reply(200, "expectedResponse");
await expect(httpRequest({ hostname, path, port, timeout })).rejects.toStrictEqual(
new ProviderError("TimeoutError from instance metadata service")
);
expect(requestSpy.mock.results[0].value.socket).toHaveProperty("destroyed", true);
scope.done();
});
});