-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.spec.js
195 lines (150 loc) · 5.22 KB
/
index.spec.js
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
import { jest } from '@jest/globals'
import { combineMiddleware, withHttpMethods, withContentTypes, withAuthorization, withCors } from './index'
const createRequest = (method, headers) => {
return {
method,
headers
}
}
const createResponse = () => {
return {
status: jest.fn(function () { return this }),
send: jest.fn(function () { return this }),
json: jest.fn(function () { return this }),
setHeader: jest.fn(function () { return this }),
}
}
describe('combineMiddleware', () => {
it('should apply middleware', () => {
const middleware = [(next) => (req, res) => {
req['name'] = 'world'
return next(req, res)
}]
const handler = jest.fn((req, res) => {
res.status(200).send(`Hello, ${req.name}!`)
})
const req = createRequest()
const res = createResponse()
combineMiddleware(...middleware)(handler)(req, res)
expect(handler).toBeCalled()
expect(res.status).toBeCalledWith(200)
expect(res.send).toBeCalledWith('Hello, world!')
})
})
describe('withHttpMethods', () => {
it('should prevent requests with unallowed methods', () => {
const middleware = [withHttpMethods(['GET'])]
const handler = jest.fn()
const req = createRequest('POST')
const res = createResponse()
combineMiddleware(...middleware)(handler)(req, res)
expect(handler).not.toBeCalled()
expect(res.status).toBeCalledWith(405)
expect(res.send).toBeCalledWith('Method Not Allowed')
})
it('should allow requests with accepted methods', () => {
const middleware = [withHttpMethods(['GET'])]
const handler = jest.fn()
const req = createRequest('GET')
const res = createResponse()
combineMiddleware(...middleware)(handler)(req, res)
expect(handler).toBeCalled()
})
})
describe('withContentTypes', () => {
it('should prevent unsupported media types', () => {
const middleware = [withContentTypes(['application/json'])]
const handler = jest.fn()
const req = createRequest('POST', { 'content-type': 'application/xml' })
const res = createResponse()
combineMiddleware(...middleware)(handler)(req, res)
expect(handler).not.toBeCalled()
expect(res.status).toBeCalledWith(415)
expect(res.send).toBeCalledWith('Unsupported Media Type')
})
it('should allow supported media types', () => {
const middleware = [withContentTypes(['application/json'])]
const handler = jest.fn()
const req = createRequest('POST', { 'content-type': 'application/json' })
const res = createResponse()
combineMiddleware(...middleware)(handler)(req, res)
expect(handler).toBeCalled()
})
})
describe('withAuthorization', () => {
it('should prevent unauthorized requests', () => {
const middleware = [withAuthorization((_) => false)]
const handler = jest.fn()
const req = createRequest()
const res = createResponse()
combineMiddleware(...middleware)(handler)(req, res)
expect(handler).not.toBeCalled()
expect(res.status).toBeCalledWith(401)
expect(res.send).toBeCalledWith('Unauthorized')
})
it('should prevent requests which fail authorization parsing', () => {
const middleware = [withAuthorization((_) => {
throw new Error('Cannot parse header')
})]
const handler = jest.fn()
const req = createRequest('GET', { authorization: 'Bearer invalid' })
const res = createResponse()
combineMiddleware(...middleware)(handler)(req, res)
expect(handler).not.toBeCalled()
expect(res.status).toBeCalledWith(401)
expect(res.send).toBeCalledWith('Unauthorized')
})
it('should allow authorized requests', () => {
const middleware = [withAuthorization((_) => true)]
const handler = jest.fn()
const req = createRequest('GET', { authorization: 'Bearer valid' })
const res = createResponse()
combineMiddleware(...middleware)(handler)(req, res)
expect(handler).toBeCalled()
})
})
describe('withCors', () => {
it('should respond to OPTIONS requests', () => {
const middleware = [withCors({
allowOrigin: '*',
allowCredentials: true,
allowHeaders: 'x-special-header',
allowMethods: 'GET',
maxAge: 0,
})]
const handler = jest.fn()
const req = createRequest('OPTIONS')
const res = createResponse()
combineMiddleware(...middleware)(handler)(req, res)
expect(res.status).toBeCalledWith(204)
expect(res.setHeader.mock.calls).toEqual(
expect.arrayContaining([
['Access-Control-Allow-Origin', '*'],
['Access-Control-Allow-Methods', 'GET'],
['Access-Control-Allow-Headers', 'x-special-header'],
['Access-Control-Allow-Credentials', 'true'],
['Access-Control-Max-Age', 0],
])
)
expect(res.send).toBeCalled()
})
it('should forward all other requests', () => {
const middleware = [withCors({
allowOrigin: '*',
allowCredentials: true,
allowHeaders: 'x-special-header',
allowMethods: 'GET',
maxAge: 0,
})]
const handler = jest.fn(
(_, res) => (
res.status(200).send()
)
)
const req = createRequest('GET')
const res = createResponse()
combineMiddleware(...middleware)(handler)(req, res)
expect(res.status).toBeCalledWith(200)
expect(res.send).toBeCalled()
})
})