UNPKG

8.63 kBJavaScriptView Raw
1'use strict';
2
3// Small Express router that runs request handlers written as ES6 generators
4// using Node co.
5
6const express = require('abacus-express');
7const request = require('abacus-request');
8const batch = require('abacus-batch');
9
10const router = require('..');
11
12/* jshint noyield: true */
13
14describe('abacus-router', () => {
15 let exit;
16 beforeEach(() => {
17 // Save process.exit function as tests mock it
18 exit = process.exit;
19 });
20 afterEach(() => {
21 // Restore original process.exit function
22 process.exit = exit;
23 });
24
25 it('handles HTTP requests', (done) => {
26 // Create a test Express app
27 const app = express();
28
29 // Create a router
30 const routes = router();
31
32 // Add a simple router level middleware
33 routes.use((req, res, next) => next());
34
35 // Add a few test routes
36 routes.get('/ok/request', (req, res) => {
37 // Return an OK response with a body
38 res.send('okay');
39 });
40 routes.get('/ok/generator/request', function *(req) {
41 // Return an OK response with a body
42 return {
43 body: 'okay'
44 };
45 });
46 routes.get('/500/request', (req, res) => {
47 // Return an error status
48 res.status(500).end();
49 });
50 routes.get('/err/request/:message', (req, res) => {
51 // Return a JSON error object with a message field
52 res.status(500).send({
53 message: req.params.message
54 });
55 });
56
57 // Add our router to the app
58 app.use(routes);
59
60 // Listen on an ephemeral port
61 const server = app.listen(0);
62
63 let cbs = 0;
64 const done1 = () => {
65 if(++cbs === 4) done();
66 };
67
68 // Send an HTTP request, expecting an OK response
69 request.get('http://localhost::p/:v/:r', {
70 p: server.address().port,
71 v: 'ok',
72 r: 'request'
73 }, (err, val) => {
74 expect(err).to.equal(undefined);
75 expect(val.statusCode).to.equal(200);
76 expect(val.body).to.equal('okay');
77 done1();
78 });
79
80 // Send an HTTP request, expecting an OK response
81 request.get('http://localhost::p/:v/generator/:r', {
82 p: server.address().port,
83 v: 'ok',
84 r: 'request'
85 }, (err, val) => {
86 expect(err).to.equal(undefined);
87 expect(val.statusCode).to.equal(200);
88 expect(val.body).to.equal('okay');
89 done1();
90 });
91
92 // Send an HTTP request, expecting a 500 status code
93 // Here test the option to pass the URI as a field of the options object
94 request.get({
95 uri: 'http://localhost::p/:v/:r',
96 p: server.address().port,
97 v: '500',
98 r: 'request'
99 }, (err, val) => {
100 expect(err.message).to.equal('HTTP response status code 500');
101 expect(val).to.equal(undefined);
102 done1();
103 });
104
105 // Send an HTTP request, expecting an error message
106 request.get('http://localhost::p/:v/:r/:m', {
107 p: server.address().port,
108 v: 'err',
109 r: 'request',
110 m: 'duh'
111 }, (err, val) => {
112 expect(err.message).to.equal('duh');
113 expect(val).to.equal(undefined);
114 done1();
115 });
116 });
117
118 it('handles exceptions', (done) => {
119 process.exit = spy();
120
121 // Create a test Express app
122 const app = express();
123
124 // Create a router
125 const routes = router();
126
127 // Add a test route
128 routes.get('/err/generator/request/:message', function *(req) {
129 // Throw an exception with a message field
130 throw new Error(req.params.message);
131 });
132
133 // Add our router to the app
134 app.use(routes);
135
136 // Listen on an ephemeral port
137 const server = app.listen(0);
138
139 // Send an HTTP request, expecting an error message
140 request.get('http://localhost::p/:v/generator/:r/:m', {
141 p: server.address().port,
142 v: 'err',
143 r: 'request',
144 m: 'boo'
145 }, (err, val) => {
146 expect(err.message).to.equal('HTTP response status code 500');
147 expect(val).to.equal(undefined);
148 expect(process.exit.args.length).to.equal(1);
149 done();
150 });
151 });
152
153 it('handles domain asynchronous exceptions', (done) => {
154 process.exit = spy();
155
156 // Create a test Express app
157 const app = express();
158
159 // Create a router
160 const routes = router();
161
162 // Add a test route
163 routes.get('/err/request/:message', (req, res) => {
164 // Throw an asynchronous exception with a message field
165 process.nextTick(() => {
166 throw new Error(req.params.message);
167 });
168 });
169
170 // Add our router to the app
171 app.use(routes);
172
173 // Listen on an ephemeral port
174 const server = app.listen(0);
175
176 // Send an HTTP request, expecting an error message
177 request.get('http://localhost::p/:v/:r/:m', {
178 p: server.address().port,
179 v: 'err',
180 r: 'request',
181 m: 'boo'
182 }, (err, val) => {
183 expect(err.message).to.equal('HTTP response status code 500');
184 expect(val).to.equal(undefined);
185 expect(process.exit.args.length).to.equal(1);
186 done();
187 });
188 });
189
190 it('handles batched HTTP requests', (done) => {
191 // Create a test Express app
192 const app = express();
193
194 // Create a router
195 const routes = router();
196
197 // Add a few test routes
198 routes.get('/ok/request', (req, res) => {
199 // Return an OK response with a body
200 res.send('okay');
201 });
202 routes.get('/ok/generator/request', function *(req) {
203 // Return an OK response with a body
204 return {
205 body: 'okay'
206 };
207 });
208 routes.get('/500/request', (req, res) => {
209 // Return an error status
210 res.status(500).end();
211 });
212 routes.get('/err/request/:message', (req, res) => {
213 // Return a JSON error object with a message field
214 res.status(500).send({
215 message: req.params.message
216 });
217 });
218
219 // Add our router to the app
220 app.use(routes);
221
222 // Add batch router middleware to the app
223 app.use(router.batch(routes));
224
225 // Listen on an ephemeral port
226 const server = app.listen(0);
227
228 let cbs = 0;
229 const done1 = () => {
230 if(++cbs === 4) done();
231 };
232
233 // Use a batch version of the request module
234 const brequest = batch(request);
235
236 // Send an HTTP request, expecting an OK response
237 brequest.get('http://localhost::p/:v/:r', {
238 p: server.address().port,
239 v: 'ok',
240 r: 'request'
241 }, (err, val) => {
242 expect(err).to.equal(undefined);
243 expect(val.statusCode).to.equal(200);
244 expect(val.body).to.equal('okay');
245 done1();
246 });
247
248 // Send an HTTP request, expecting an OK response
249 brequest.get('http://localhost::p/:v/generator/:r', {
250 p: server.address().port,
251 v: 'ok',
252 r: 'request'
253 }, (err, val) => {
254 expect(err).to.equal(undefined);
255 expect(val.statusCode).to.equal(200);
256 expect(val.body).to.equal('okay');
257 done1();
258 });
259
260 // Send an HTTP request, expecting a 500 status code
261 // Here test the option to pass the URI as a field of the options object
262 brequest.get({
263 uri: 'http://localhost::p/:v/:r',
264 p: server.address().port,
265 v: '500',
266 r: 'request'
267 }, (err, val) => {
268 expect(err.message).to.equal('HTTP response status code 500');
269 expect(val).to.equal(undefined);
270 done1();
271 });
272
273 // Send an HTTP request, expecting an error message
274 brequest.get('http://localhost::p/:v/:r/:m', {
275 p: server.address().port,
276 v: 'err',
277 r: 'request',
278 m: 'duh'
279 }, (err, val) => {
280 expect(err.message).to.equal('duh');
281 expect(val).to.equal(undefined);
282 done1();
283 });
284 });
285
286 it('handles unhandled exceptions at middleware and' +
287 'responds back with a 500 for a batch http request', (done) => {
288 // Create a test Express app
289 const app = express();
290
291 // Create a router
292 const routes = router();
293
294 // Add a test route that throws an exception
295 routes.get('/exception/request', (req, res) => {
296 if (req.cause.unhandled.exception)
297 res.status(200).send({ message: '' });
298 });
299
300 // Add our router to the app
301 app.use(routes);
302
303 // Add batch router middleware to the app
304 app.use(router.batch(routes));
305
306 // Listen on an ephemeral port
307 const server = app.listen(0);
308
309 // Use a batch version of the request module
310 const brequest = batch(request);
311
312 // Send an HTTP request, expecting a 500 status code
313 brequest.get({
314 uri: 'http://localhost::p/:v/:r',
315 p: server.address().port,
316 v: 'exception',
317 r: 'request'
318 }, (err, val) => {
319 expect(err.message).to.equal('HTTP response status code 500');
320 expect(val).to.equal(undefined);
321 done();
322 });
323 });
324});