-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.js
More file actions
81 lines (65 loc) · 1.98 KB
/
Copy pathindex.test.js
File metadata and controls
81 lines (65 loc) · 1.98 KB
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
const response = require('./index')
function createRes() {
return {
statusCode: undefined,
headers: {},
sent: undefined,
ended: false,
endData: undefined,
endEncoding: undefined,
header(name, value) {
this.headers[name] = value
},
setHeader(name, value) {
this.headers[name] = value
},
send(data) {
this.sent = data
},
end(data, encoding) {
this.ended = true
this.endData = data
this.endEncoding = encoding
},
}
}
test('json sets status, content-type, and sends payload', () => {
const res = createRes()
response.json(res, { ok: true }, 201)
expect(res.statusCode).toBe(201)
expect(res.headers['Content-Type']).toBe('application/json; charset=utf-8')
expect(res.sent).toEqual({ ok: true })
})
test('notFound uses default message', () => {
const res = createRes()
response.notFound(res)
expect(res.statusCode).toBe(404)
expect(res.sent).toBe('404 Page Not Found')
})
test('redirect sets location header and ends response', () => {
const res = createRes()
response.redirect(res, '/new-url')
expect(res.statusCode).toBe(301)
expect(res.headers.Location).toBe('/new-url')
expect(res.ended).toBe(true)
})
test('jsonErrorResponse sends expected error shape', () => {
const res = createRes()
response.jsonErrorResponse(res, 422, 'validation.error', 'MISSING_FIELD', 'name is required')
expect(res.statusCode).toBe(422)
expect(res.headers['Content-Type']).toBe('application/json; charset=utf-8')
expect(res.sent).toEqual({
messageKey: 'validation.error',
errKey: 'MISSING_FIELD',
errorMessage: 'name is required',
})
})
test('svg sets svg and vary headers and ends response', () => {
const res = createRes()
response.svg(res, '<svg></svg>')
expect(res.statusCode).toBe(200)
expect(res.headers['Content-Type']).toBe('image/svg+xml')
expect(res.headers.Vary).toBe('Accept-Encoding')
expect(res.ended).toBe(true)
expect(res.endData).toBe('<svg></svg>')
})