-
-
Notifications
You must be signed in to change notification settings - Fork 364
/
fetch.test.ts
169 lines (141 loc) · 4.75 KB
/
fetch.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
import * as http from "http"
import * as node_fetch from "node-fetch"
import type { HttpProxyAgent } from "http-proxy-agent"
import type { HttpsProxyAgent } from "https-proxy-agent"
import { api } from "../fetch"
interface ResponseMock {
body?: any
statusCode?: number
contentType?: string
}
class TestServer {
private port = 30001
private hostname = "localhost"
private response: ResponseMock = null as any
private router = (_req: any, res: any) => {
res.statusCode = this.response && this.response.statusCode ? this.response.statusCode : 200
res.setHeader(
"Content-Type",
this.response && this.response.contentType ? this.response.contentType : "application/json"
)
res.end(this.response ? this.response.body : null)
}
private server = http.createServer(this.router)
start = async (response: ResponseMock): Promise<void> => {
this.response = response
return new Promise<void>((resolve, reject) => {
this.server.listen(this.port, this.hostname, (err: any) => (err ? reject(err) : resolve()))
})
}
stop = async (): Promise<void> => {
this.response = null as any
return new Promise<void>((resolve, reject) => {
this.server.close((err: any) => (err ? reject(err) : resolve()))
})
}
}
class TestProxy {
isRunning = false
private port = 30002
private hostname = "localhost"
private router = (_req: any, res: any) => {
res.statusCode = 200
res.end(null)
}
private server = http.createServer(this.router)
start = async (): Promise<void> => {
return new Promise<void>((resolve, reject) => {
this.isRunning = true
this.server.listen(this.port, this.hostname, (err: any) => (err ? reject(err) : resolve()))
})
}
stop = async (): Promise<void> => {
return new Promise<void>((resolve, reject) => {
this.isRunning = false
this.server.close((err: any) => (err ? reject(err) : resolve()))
})
}
}
describe("fetch", () => {
let url: string
let server = new TestServer()
let proxy = new TestProxy()
beforeEach(() => {
url = "http://localhost:30001/"
})
afterEach(async () => {
await server.stop()
if (proxy.isRunning) {
await proxy.stop()
}
})
it("handles json success", async () => {
let body = { key: "valid json" }
await server.start({
body: JSON.stringify(body),
})
let response = await api(url, {})
expect(response.ok).toBe(true)
expect(response.status).toBe(200)
expect(await response.json()).toMatchObject(body)
})
it("handles json error", async () => {
let body = { key: "valid json" }
await server.start({
body: JSON.stringify(body),
statusCode: 500,
})
let response = await api(url, {})
expect(response.ok).toBe(false)
expect(response.status).toBe(500)
expect(await response.json()).toMatchObject(body)
})
it("handles plain text error", async () => {
let body = "any plain text response"
await server.start({
body: body,
statusCode: 500,
contentType: "text/plain",
})
let response = await api(url, {})
expect(response.ok).toBe(false)
expect(response.status).toBe(500)
expect(await response.text()).toBe(body)
})
it("sets proxy agent when HTTPS_PROXY env variable is defined", async () => {
const proxyUrl = "http://localhost:30002/"
await proxy.start()
await server.start({})
let options: node_fetch.RequestInit = { agent: undefined }
await api(url, options, true, { HTTPS_PROXY: proxyUrl })
let agent = options.agent as HttpsProxyAgent
expect(agent["proxy"].href).toBe(proxyUrl)
})
it("sets proxy agent when https_proxy env variable is defined", async () => {
const proxyUrl = "http://localhost:30002/"
await proxy.start()
await server.start({})
let options: node_fetch.RequestInit = { agent: undefined }
await api(url, options, true, { https_proxy: proxyUrl })
let agent = options.agent as HttpsProxyAgent
expect(agent["proxy"].href).toBe(proxyUrl)
})
it("sets proxy agent when HTTP_PROXY env variable is defined", async () => {
const proxyUrl = "http://localhost:30002/"
await proxy.start()
await server.start({})
let options: node_fetch.RequestInit = { agent: undefined }
await api(url, options, true, { HTTP_PROXY: proxyUrl })
let agent = options.agent as HttpProxyAgent
expect(agent["proxy"].href).toBe(proxyUrl)
})
it("sets proxy agent when http_proxy env variable is defined", async () => {
const proxyUrl = "http://localhost:30002/"
await proxy.start()
await server.start({})
let options: node_fetch.RequestInit = { agent: undefined }
await api(url, options, true, { http_proxy: proxyUrl })
let agent = options.agent as HttpProxyAgent
expect(agent["proxy"].href).toBe(proxyUrl)
})
})