-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.spec.js
331 lines (251 loc) · 8.17 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
const supertest = require('supertest');
const http = require('http');
const fogo = require('./dist/index');
let server;
let request;
const handlers = {
'/': (req, res) => {
res.writeHead(200);
res.end();
},
'/methods': {
get: (req, res) => {
res.writeHead(200);
res.end('get');
},
post: (req, res) => {
res.writeHead(200);
res.end('post');
},
patch: (req, res) => {
res.writeHead(200);
res.end('patch');
},
put: (req, res) => {
res.writeHead(200);
res.end('put');
},
delete: (req, res) => {
res.writeHead(200);
res.end('delete');
}
},
'/params/:id/:name?': (req, res, url, params) => {
res.setHeader('content-type', 'application/json');
res.writeHead(200);
res.end(JSON.stringify({ ...params }));
},
'/headers': (req, res) => {
res.setHeader('x-id', '123');
res.end();
},
'/url': (req, res, url) => {
res.setHeader('content-type', 'application/json');
res.writeHead(200);
res.end(JSON.stringify({ ...url }));
},
'/error': () => {
throw new Error('mocked error');
}
};
beforeAll(() => {
server = fogo.createServer(handlers);
request = supertest(server);
server.listen();
});
afterAll(() => server.close());
describe('createServer', () => {
it('should be an instance of http.Server', () => {
expect(fogo.createServer() instanceof http.Server).toBe(true);
});
it('should listen to request events', async done => {
const customServer = fogo.createServer();
customServer.on('request', () => {
done();
});
customServer.listen();
await supertest(customServer).get('/');
customServer.close();
});
it('should listen to close connection events', done => {
const customServer = fogo.createServer();
customServer.on('close', () => {
done();
});
customServer.listen();
customServer.close();
});
it('should invoke a callback when the server starts', done => {
const customServer = fogo.createServer();
customServer.listen(() => {
customServer.close();
setTimeout(done, 10);
});
});
});
describe('unique route for all http methods', () => {
it('should receive a get request', async () => {
const result = await request.get('/');
expect(result.status).toBe(200);
});
it('should receive a post request', async () => {
const result = await request.post('/');
expect(result.status).toBe(200);
});
it('should receive a patch request', async () => {
const result = await request.patch('/');
expect(result.status).toBe(200);
});
it('should receive a delete request', async () => {
const result = await request.delete('/');
expect(result.status).toBe(200);
});
});
describe('same route handling different http methods', () => {
it('should handle a get request', async () => {
const result = await request.get('/methods');
expect(result.status).toBe(200);
expect(result.text).toBe('get');
});
it('should handle a post request', async () => {
const result = await request.post('/methods');
expect(result.status).toBe(200);
expect(result.text).toBe('post');
});
it('should handle a patch request', async () => {
const result = await request.patch('/methods');
expect(result.status).toBe(200);
expect(result.text).toBe('patch');
});
it('should handle a put request', async () => {
const result = await request.put('/methods');
expect(result.status).toBe(200);
expect(result.text).toBe('put');
});
it('should handle a delete request', async () => {
const result = await request.delete('/methods');
expect(result.status).toBe(200);
expect(result.text).toBe('delete');
});
});
describe('not found requests - default handler', () => {
it('should return status code 404 when a route does not exist', async () => {
const result = await request.get('/not-found-route');
expect(result.status).toBe(404);
});
it('should return a not found message when a route does not exist', async () => {
const result = await request.get('/not-found-route');
expect(result.text).toBe(http.STATUS_CODES[404]);
});
it('should return 404 if a http method does not exist in an existent route', async () => {
const result = await request.options('/methods');
expect(result.status).toBe(404);
expect(result.text).toBe(http.STATUS_CODES[404]);
});
});
describe('not found requests - custom handler', () => {
let customServer;
let customRequest;
beforeAll(() => {
customServer = fogo.createServer(handlers, (req, res) => {
res.writeHead(404);
res.end(http.STATUS_CODES[404]);
});
customRequest = supertest(customServer);
});
afterAll(() => customServer.close());
it('should return status code 404 when a route does not exist', async () => {
const result = await customRequest.get('/not-found-route');
expect(result.status).toBe(404);
});
it('should return a custom message when a route does not exist', async () => {
const result = await customRequest.get('/not-found-route');
expect(result.text).toBe(http.STATUS_CODES[404]);
});
it('should return a custom error if a http method does not exist in an existent route', async () => {
const result = await customRequest.options('/methods');
expect(result.status).toBe(404);
expect(result.text).toBe(http.STATUS_CODES[404]);
});
});
describe('dynamic routes', () => {
it('should return all params', async () => {
const result = await request.get('/params/123/bar');
const { id, name } = result.body;
expect(id).toBe('123');
expect(name).toBe('bar');
});
it('should accept requests without passing optional parameters', async () => {
const result = await request.get('/params/123');
expect(result.body.id).toBe('123');
});
it('should accept requests with the last bar', async () => {
const result = await request.get('/params/123/');
expect(result.body.id).toBe('123');
});
it('should not return optional parameters when not passing them', async () => {
const result = await request.get('/params/123');
expect(result.body.name).toBe(undefined);
});
});
describe('url', () => {
it('should receive the pathname', async () => {
const result = await request.get('/url?id=123&name=fogo');
const { pathname } = result.body;
expect(pathname).toBe('/url');
});
it('should receive search parameters', async () => {
const result = await request.get('/url?id=123&name=fogo');
const { search } = result.body;
expect(search).toBe('?id=123&name=fogo');
});
it('should receive all query string parameters parsed', async () => {
const result = await request.get('/url?id=123&name=fogo');
const { query } = result.body;
expect(query.id).toBe('123');
expect(query.name).toBe('fogo');
});
});
describe('headers', () => {
it('should receive a custom header', async () => {
const result = await request.get('/headers');
expect(result.header['x-id']).toBe('123');
});
});
describe('error handler - default handler', () => {
it('should return status code 501 when an error happens', async () => {
const result = await request.get('/error');
expect(result.status).toBe(500);
});
it('should read the error message', async () => {
const result = await request.get('/error');
expect(result.text).toBe(http.STATUS_CODES[500]);
});
});
describe('error handler - custom handler', () => {
let customServer;
let customRequest;
beforeAll(() => {
customServer = fogo.createServer(
handlers,
(req, res) => {
res.writeHead(404);
res.end(http.STATUS_CODES[404]);
},
(req, res, url, err) => {
res.writeHead(501);
res.end(`custom error + ${err.message}`);
}
);
customRequest = supertest(customServer);
});
afterAll(() => customServer.close());
it('should return status code 501 when an error happens', async () => {
const result = await customRequest.get('/error');
expect(result.status).toBe(501);
});
it('should read the error message', async () => {
const result = await customRequest.get('/error');
expect(result.text).toBe('custom error + mocked error');
});
});