-
-
Notifications
You must be signed in to change notification settings - Fork 473
/
prepareResponse.test.ts
52 lines (43 loc) · 1.33 KB
/
prepareResponse.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
import { prepareResponse } from './prepareResponse'
test('parses a JSON response body given a "Content-Type:*/json" header', async () => {
const res = await prepareResponse({
status: 200,
statusText: 'OK',
headers: {
'Content-Type': 'application/json',
},
body: `{"property":2}`,
})
// Preserves all the properties
expect(res.status).toEqual(200)
expect(res.statusText).toEqual('OK')
expect(res.headers).toEqual({ 'Content-Type': 'application/json' })
// Parses a JSON response body
expect(res.body).toEqual({ property: 2 })
})
test('returns a stringified valid JSON body given a non-JSON "Content-Type" header', () => {
const res = prepareResponse({
status: 200,
statusText: 'OK',
headers: {},
body: `{"property":2}`,
})
expect(res.status).toEqual(200)
expect(res.statusText).toEqual('OK')
expect(res.headers).toEqual({})
// Returns a non-JSON response body as-is
expect(res.body).toEqual(`{"property":2}`)
})
test('returns a non-JSON response body as-is', () => {
const res = prepareResponse({
status: 200,
statusText: 'OK',
headers: {},
body: `text-body`,
})
expect(res.status).toEqual(200)
expect(res.statusText).toEqual('OK')
expect(res.headers).toEqual({})
// Returns a non-JSON response body as-is
expect(res.body).toEqual('text-body')
})