-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.ts
200 lines (175 loc) · 5.96 KB
/
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
import test from 'ava'
import request, { del, get, post, Retry, SuccessResponse, ErrorResponse, ExceptionResponse, Response } from './server'
test('request: type, status, statusText, url and default headers', async t => {
interface SuccessData {
origin: string
url: string
headers: { [key: string]: string }
}
const response = await request<SuccessData>('https://httpbin.org/get')
if (response.type === 'success') {
const { data, type, status, statusText, url } = response
t.is(data.url, 'https://httpbin.org/get')
t.is(data.headers['Content-Type'], undefined)
t.is(data.headers['Accept'], 'application/json')
t.is(type, 'success')
t.is(status, 200)
t.is(statusText, 'OK')
t.is(url, 'https://httpbin.org/get')
}
})
test('request: querystring', async t => {
const response = (await request('https://httpbin.org/get', {
params: { a: 'b', c: 'd', e: undefined },
})) as SuccessResponse<{ url: string }>
t.is(response.data.url, 'https://httpbin.org/get?a=b&c=d')
})
test('request: custom stringify', async t => {
const { data } = await request('https://httpbin.org/get', {
params: { a: 'b', c: 'd' },
stringify: () => 'a=b',
})
t.is(data.url, 'https://httpbin.org/get?a=b')
})
test('request: json body, including content-type header', async t => {
const { data } = await request('https://httpbin.org/post', {
method: 'POST',
body: { a: 'b', c: 'd' },
})
t.is(data.headers['Content-Type'], 'application/json')
t.deepEqual(data.json, { a: 'b', c: 'd' })
})
test('request: redirect', async t => {
const { url, status } = (await request('https://google.com/')) as SuccessResponse
t.is(url, 'https://www.google.com/')
t.is(status, 200)
})
test('request: redirect manual', async t => {
const { type, status } = (await request('https://google.com/', {
redirect: 'manual',
})) as ErrorResponse
t.is(type, 'error')
t.is(status, 301)
})
test('request: can override default content-type header, case insensitive', async t => {
const { data } = await request('https://httpbin.org/headers', {
headers: { 'CONTENT-TYPE': '*/*' },
})
t.is(data.headers['Content-Type'], '*/*')
})
// client code must manually parse response JSON, no double stringify
test('request: jsonOut false', async t => {
const { data } = await request('https://httpbin.org/post', {
method: 'POST',
body: JSON.stringify({ a: 'b', c: 'd' }),
jsonOut: false,
})
const text = await data.text()
const parsed = JSON.parse(text)
t.deepEqual(parsed.json, { a: 'b', c: 'd' })
})
test('request: response headers are object literal', async t => {
const { headers } = (await request('https://httpbin.org/get')) as SuccessResponse
t.is(headers['content-type'], 'application/json')
})
test('request: error', async t => {
const { type, status } = (await request('https://httpbin.org/GET')) as ErrorResponse
t.is(status, 404)
t.is(type, 'error')
})
test('request: exception', async t => {
const { data, type, ...rest } = (await request('https://httpbin.smorg/get')) as ExceptionResponse
t.is(data.name, 'FetchError') // would be 'TypeError' in a browser
t.is(type, 'exception')
t.deepEqual(rest, {})
})
test('request: convenience methods', async t => {
let response = await post('https://httpbin.org/post', { body: { a: 'b' } })
t.is(response.type, 'success')
t.is(response.data.json.a, 'b')
response = await del('https://httpbin.org/delete')
t.is(response.type, 'success')
response = await get('https://httpbin.org/post')
t.is(response.type, 'error')
})
test('request: convenience methods (properties of request)', async t => {
let response = await request.post('https://httpbin.org/post', { body: { a: 'b' } })
t.is(response.type, 'success')
t.is(response.data.json.a, 'b')
response = await request.del('https://httpbin.org/delete')
t.is(response.type, 'success')
response = await request.get('https://httpbin.org/post')
t.is(response.type, 'error')
})
// backoff
test.cb('retry: retries on exception, increases delay', t => {
t.plan(5)
const shouldRetry: Retry['shouldRetry'] = ({ type }, { retries, delay }) => {
t.is(type, 'exception')
if (retries <= 1) {
t.is(delay, 1000)
t.end()
}
return type === 'exception'
}
request('https://httpbin.smorg/get', { retry: { shouldRetry, retries: 4, delay: 125 } })
})
test('retry: eventually returns response', async t => {
const { type } = await request('https://httpbin.smorg/get', { retry: { delay: 250, retries: 3 } })
t.is(type, 'exception')
})
test.cb('retry: callback style', t => {
t.plan(1)
request('https://httpbin.smorg/get', { retry: { delay: 250, retries: 3 } }).then(({ type }) => {
t.is(type, 'exception')
t.end()
})
})
test('timeout', async t => {
let response = await request('https://httpbin.org/get', { timeout: 10 })
t.is(response.type, 'exception')
t.is(response.data.name, 'AbortError')
response = await request('https://httpbin.org/get', { timeout: 10000 })
t.is(response.type, 'success')
})
test.cb('retry: retries on custom condition', t => {
t.plan(4)
const shouldRetry: Retry['shouldRetry'] = (response: Response, { retries }) => {
t.pass()
if (retries <= 1) t.end()
return response.type !== 'exception' && response.status === 500
}
request('https://httpbin.org/status/500', { retry: { shouldRetry, retries: 4, delay: 125 } })
})
test('retry: no exception -> no retry', async t => {
const shouldRetry: Retry['shouldRetry'] = ({ type }, { retries }) => {
t.pass()
if (retries < 3) t.fail()
return type === 'exception'
}
await request('https://httpbin.org/status/500', {
retry: { shouldRetry, retries: 3, delay: 125 },
})
})
test('graphql', async t => {
const query = `
{
continents {
name
code
countries {
name
code
currency
}
}
}
`
const { type, data } = await request('https://countries.trevorblades.com/', {
body: { query },
method: 'POST',
})
if (type === 'success') {
t.is(data.data.continents.length, 7)
}
})