1 | ;
|
2 |
|
3 | const server = require('simple-server-setup');
|
4 | const Client = require('../lib/Client');
|
5 | const Request = require('../lib/Request');
|
6 | const Response = require('../lib/Response');
|
7 |
|
8 | //var HTTPS_SERVER_OPTIONS = {
|
9 | // secure: true,
|
10 | // key: __dirname+'/server.key',
|
11 | // cert: __dirname+'/server.cert'
|
12 | //};
|
13 | //
|
14 | //var HTTPS_CLIENT_OPTIONS = {
|
15 | // https_ignore_errors: true
|
16 | //};
|
17 |
|
18 | describe('Client', function() {
|
19 |
|
20 | describe('.request()', () => {
|
21 |
|
22 | describe(' => middleware', () => {
|
23 |
|
24 | describe('.before()', () => {
|
25 |
|
26 | it('should be called with a Request', (done) => {
|
27 | let called = false;
|
28 |
|
29 | server.create(app => {
|
30 | app.get('/', (req, res) => {
|
31 | res.send('go-fetch');
|
32 | });
|
33 | })
|
34 | .then(server =>
|
35 |
|
36 | new Client()
|
37 | .before(req => {
|
38 | called = true;
|
39 | expect(req).to.be.an.instanceof(Request);
|
40 | })
|
41 | .get(server.url)
|
42 | .then(res => expect(called).to.be.true)
|
43 | .then(
|
44 | () => server.close(done),
|
45 | err => server.close(() => done(err))
|
46 | )
|
47 |
|
48 | )
|
49 | ;
|
50 |
|
51 | });
|
52 |
|
53 | it('should be able to modify the Request', (done) => {
|
54 |
|
55 | server.create(app => {
|
56 | app.get('/', (req, res) => {
|
57 | res.header('x-test', req.headers['x-test']);
|
58 | res.send('go-fetch');
|
59 | });
|
60 | })
|
61 | .then(server =>
|
62 |
|
63 | new Client()
|
64 | .before((req, next) => {
|
65 | req.headers['x-test'] = 'foo';
|
66 | next(null, req);
|
67 | })
|
68 | .get(server.url)
|
69 | .then(res => expect(res.headers).to.have.property('x-test').equal('foo'))
|
70 | .then(
|
71 | () => server.close(done),
|
72 | err => server.close(() => done(err))
|
73 | )
|
74 |
|
75 | )
|
76 | ;
|
77 |
|
78 | });
|
79 |
|
80 | });
|
81 |
|
82 | describe('.after()', () => {
|
83 |
|
84 | it('should be called with a Response', (done) => {
|
85 | let called = false;
|
86 |
|
87 | server.create(app => {
|
88 | app.get('/', (req, res) => {
|
89 | res.send('go-fetch');
|
90 | });
|
91 | })
|
92 | .then(server =>
|
93 |
|
94 | new Client()
|
95 | .after(req => {
|
96 | called = true;
|
97 | expect(req).to.be.an.instanceof(Response);
|
98 | })
|
99 | .get(server.url)
|
100 | .then(res => expect(called).to.be.true)
|
101 | .then(
|
102 | () => server.close(done),
|
103 | err => server.close(() => done(err))
|
104 | )
|
105 |
|
106 | )
|
107 | ;
|
108 |
|
109 | });
|
110 |
|
111 | it('should be able to modify the Response', (done) => {
|
112 |
|
113 | server.create(app => {
|
114 | app.get('/', (req, res) => {
|
115 | res.send('go-fetch');
|
116 | });
|
117 | })
|
118 | .then(server =>
|
119 |
|
120 | new Client()
|
121 | .after((res) => {
|
122 | res.headers['x-test'] = 'foo';
|
123 | })
|
124 | .get(server.url)
|
125 | .then(res => expect(res.headers).to.have.property('x-test').equal('foo'))
|
126 | .then(
|
127 | () => server.close(done),
|
128 | err => server.close(() => done(err))
|
129 | )
|
130 |
|
131 | )
|
132 | ;
|
133 |
|
134 | });
|
135 |
|
136 | });
|
137 |
|
138 | });
|
139 |
|
140 | });
|
141 |
|
142 | describe('.get()', () => {
|
143 | it('should set the method, URL and headers');
|
144 |
|
145 | describe('=> network', () => {
|
146 |
|
147 | it('should receive a response', () => {
|
148 | return new Client()
|
149 | .get('http://httpbin.org/get', {'x-test': 'foobar'})
|
150 | .then(res => {
|
151 | expect(res.version).to.be.equal('1.1');
|
152 | expect(res.status).to.be.equal(200);
|
153 | expect(res.reason).to.be.equal('OK');
|
154 | expect(res.headers).to.have.property('content-type').equal('application/json');
|
155 | return res.text().then(text => expect(JSON.parse(text)).to.have.property('headers').to.have.property('X-Test', 'foobar'));
|
156 | });
|
157 | });
|
158 | });
|
159 |
|
160 | });
|
161 |
|
162 | describe('.post()', () => {
|
163 | it('should set the method, URL, headers and body');
|
164 |
|
165 | describe('=> network', () => {
|
166 |
|
167 | it('should receive a response', () => {
|
168 | return new Client()
|
169 | .post('http://httpbin.org/post', JSON.stringify({msg: 'Hello World!'}))
|
170 | .then(res => {
|
171 | expect(res.version).to.be.equal('1.1');
|
172 | expect(res.status).to.be.equal(200);
|
173 | expect(res.reason).to.be.equal('OK');
|
174 | expect(res.headers).to.have.property('content-type').equal('application/json');
|
175 | return res.text().then(text => expect(JSON.parse(text)).to.have.property('json').to.have.property('msg', 'Hello World!'));
|
176 | })
|
177 | ;
|
178 |
|
179 | });
|
180 |
|
181 | });
|
182 |
|
183 | });
|
184 |
|
185 | describe('.put()', () => {
|
186 | it('should set the method, URL, headers and body');
|
187 | });
|
188 |
|
189 | describe('.delete()', () => {
|
190 | it('should set the method, URL and headers');
|
191 | });
|
192 |
|
193 |
|
194 | //
|
195 | //describe('constructor', function() {
|
196 | //
|
197 | // it('should create a new instance with the new keyword', function() {
|
198 | // var client = new Client();
|
199 | // assert(client instanceof Client);
|
200 | // });
|
201 | //
|
202 | //});
|
203 | //
|
204 | //describe('.send()', function() {
|
205 | //
|
206 | // it('should fetch a HTTP resource', function(done) {
|
207 | //
|
208 | // var srv = server.create(function(app) {
|
209 | // app.get('/', function(req, res) {
|
210 | // res.send('HTTP');
|
211 | // });
|
212 | // });
|
213 | //
|
214 | // srv.on('configured', function() {
|
215 | //
|
216 | // new Client(HTTPS_CLIENT_OPTIONS).get(srv.url)
|
217 | // .then(response => {
|
218 | // assert(!error);
|
219 | // assert.equal(response.getStatus(), 200);
|
220 | // assert.equal(error, undefined);
|
221 | // srv.close(done);
|
222 | // })
|
223 | // .catch(err => done(!err))
|
224 | // ;
|
225 | //
|
226 | // });
|
227 | //
|
228 | // });
|
229 | //
|
230 | // it('should fetch a HTTPS resource', function(done) {
|
231 | //
|
232 | // var srv = server.create(HTTPS_SERVER_OPTIONS, function(app) {
|
233 | // app.get('/', function(req, res) {
|
234 | // res.send('HTTPS');
|
235 | // });
|
236 | // });
|
237 | //
|
238 | // srv.on('configured', function() {
|
239 | //
|
240 | // new Client(HTTPS_CLIENT_OPTIONS).get(srv.url)
|
241 | // .then(response => {
|
242 | // assert.equal(response.getStatus(), 200);
|
243 | // assert.equal(error, undefined);
|
244 | // srv.close(done);
|
245 | // })
|
246 | // .catch(err => assert(!err))
|
247 | // ;
|
248 | //
|
249 | // });
|
250 | //
|
251 | // });
|
252 | //
|
253 | // it('should emit `before`', function(done) {
|
254 | //
|
255 | // var srv = server.create(function(app) {
|
256 | // app.get('/', function(req, res) {
|
257 | // res.send('SERVER');
|
258 | // });
|
259 | // });
|
260 | //
|
261 | // srv.on('configured', function() {
|
262 | // var event;
|
263 | //
|
264 | // Client()
|
265 | // .on('before', function(e) {event=e.getName();})
|
266 | // .get(srv.url, function(error, response) {
|
267 | // assert.equal(event, 'before');
|
268 | // srv.close(done);
|
269 | // })
|
270 | // ;
|
271 | //
|
272 | // });
|
273 | //
|
274 | // });
|
275 | //
|
276 | // it('`before` event should be stoppable', function(done) {
|
277 | //
|
278 | // var srv = server.create(function(app) {
|
279 | // app.get('/', function(req, res) {
|
280 | // res.send('SERVER');
|
281 | // });
|
282 | // });
|
283 | //
|
284 | // srv.on('configured', function() {
|
285 | // var p1, p2;
|
286 | //
|
287 | // Client()
|
288 | // .on('before', function(e) {p1=true;e.stopPropagation();})
|
289 | // .on('before', function(e) {p2=true;})
|
290 | // .get(srv.url, function(error, response) {
|
291 | // assert(p1);
|
292 | // assert(!p2);
|
293 | // srv.close(done);
|
294 | // })
|
295 | // ;
|
296 | //
|
297 | // });
|
298 | //
|
299 | // });
|
300 | //
|
301 | // it('`before` event should be preventable');
|
302 | // it('should emit `after`', function(done) {
|
303 | //
|
304 | // var srv = server.create(function(app) {
|
305 | // app.get('/', function(req, res) {
|
306 | // res.send('SERVER');
|
307 | // });
|
308 | // });
|
309 | //
|
310 | // srv.on('configured', function() {
|
311 | // var event;
|
312 | //
|
313 | // Client()
|
314 | // .on('after', function(e) {event=e.getName();})
|
315 | // .get(srv.url, function(error, response) {
|
316 | // assert.equal(event, 'after');
|
317 | // srv.close(done);
|
318 | // })
|
319 | // ;
|
320 | //
|
321 | // });
|
322 | //
|
323 | // });
|
324 | //
|
325 | // it('`after` event should be stoppable', function(done) {
|
326 | //
|
327 | // var srv = server.create(function(app) {
|
328 | // app.get('/', function(req, res) {
|
329 | // res.send('SERVER');
|
330 | // });
|
331 | // });
|
332 | //
|
333 | // srv.on('configured', function() {
|
334 | // var p1, p2;
|
335 | //
|
336 | // Client()
|
337 | // .on('after', function(e) {p1=true;e.stopPropagation();})
|
338 | // .on('after', function(e) {p2=true;})
|
339 | // .get(srv.url, function(error, response) {
|
340 | // assert(p1);
|
341 | // assert(!p2);
|
342 | // srv.close(done);
|
343 | // })
|
344 | // ;
|
345 | //
|
346 | // });
|
347 | //
|
348 | // });
|
349 | //
|
350 | // it('should emit `sent`', function(done) {
|
351 | //
|
352 | // var srv = server.create(function(app) {
|
353 | // app.get('/', function(req, res) {
|
354 | // res.send('SERVER');
|
355 | // });
|
356 | // });
|
357 | //
|
358 | // srv.on('configured', function() {
|
359 | // var called;
|
360 | //
|
361 | // Client()
|
362 | // .on('sent', function() {called=true})
|
363 | // .get(srv.url, function(error, response) {
|
364 | // assert(called);
|
365 | // srv.close(done);
|
366 | // })
|
367 | // ;
|
368 | //
|
369 | // });
|
370 | //
|
371 | // });
|
372 | //
|
373 | // it('should emit `received`', function(done) {
|
374 | //
|
375 | // var srv = server.create(function(app) {
|
376 | // app.get('/', function(req, res) {
|
377 | // res.send('SERVER');
|
378 | // });
|
379 | // });
|
380 | //
|
381 | // srv.on('configured', function() {
|
382 | // var called;
|
383 | //
|
384 | // Client()
|
385 | // .on('received', function() {called=true})
|
386 | // .get(srv.url, function(error, response) {
|
387 | // assert(called);
|
388 | // srv.close(done);
|
389 | // })
|
390 | // ;
|
391 | //
|
392 | // });
|
393 | //
|
394 | // });
|
395 | //
|
396 | // it('should emit `error`', function(done) {
|
397 | // var called;
|
398 | //
|
399 | // Client()
|
400 | // .on('error', function() {called=true})
|
401 | // .get('http://does.not.exist', function(error, response) {
|
402 | // assert(called);
|
403 | // done();
|
404 | // })
|
405 | // ;
|
406 | //
|
407 | // });
|
408 | //
|
409 | // it('should emit `error` if a plugin fails `before`', function(done) {
|
410 | //
|
411 | // var srv = server.create(function(app) {
|
412 | // app.get('/', function(req, res) {
|
413 | // res.send('');
|
414 | // });
|
415 | // });
|
416 | //
|
417 | // srv.on('configured', function() {
|
418 | //
|
419 | // Client()
|
420 | // .use(function(client) {
|
421 | // client.on('before', function(event, next) {
|
422 | // next(new Error('Plugin error'));
|
423 | // });
|
424 | // })
|
425 | // .get(srv.url)
|
426 | // .on('error', function(err) {
|
427 | // assert.equal(err.message, 'Plugin error');
|
428 | // srv.close(done);
|
429 | // })
|
430 | // .send()
|
431 | // ;
|
432 | //
|
433 | // });
|
434 | // });
|
435 | //
|
436 | // it('should emit `error` if a plugin fails `after`', function(done) {
|
437 | //
|
438 | // var srv = server.create(function (app) {
|
439 | // app.get('/', function (req, res) {
|
440 | // res.send('');
|
441 | // });
|
442 | // });
|
443 | //
|
444 | // srv.on('configured', function () {
|
445 | //
|
446 | // Client()
|
447 | // .use(function (client) {
|
448 | // client.on('after', function (event, next) {
|
449 | // next(new Error('Plugin error'));
|
450 | // });
|
451 | // })
|
452 | // .get(srv.url)
|
453 | // .send()
|
454 | // .on('error', function (err) {
|
455 | // assert.equal(err.message, 'Plugin error');
|
456 | // srv.close(done);
|
457 | // })
|
458 | // ;
|
459 | //
|
460 | // });
|
461 | //
|
462 | // });
|
463 | //
|
464 | // it('should still have events registered on the `Response` before it is injected');
|
465 | // it('should still call methods on the `Response` before it is injected');
|
466 | //
|
467 | // it('should timeout after 1s', function(done) {
|
468 | //
|
469 | // var srv = server.create(function (app) {
|
470 | // app.get('/', function (req, res) {
|
471 | // //res.send('');
|
472 | // });
|
473 | // });
|
474 | //
|
475 | // srv.on('configured', function() {
|
476 | //
|
477 | // new Client({timeout: 1000})
|
478 | // .use(function(client) {
|
479 | // client.on('after', function(event, next) {
|
480 | // next(new Error('Plugin error'));
|
481 | // });
|
482 | // })
|
483 | // .get(srv.url, function(err) {
|
484 | // assert.equal(err.code, 'ECONNRESET');
|
485 | // srv.close(done);
|
486 | // })
|
487 | // ;
|
488 | //
|
489 | // });
|
490 | //
|
491 | // });
|
492 | //
|
493 | //});
|
494 | //
|
495 | //describe('.request()', function() {
|
496 | //
|
497 | // describe('Callback-style', function(done) {
|
498 | //
|
499 | // it('should return a `Client`', function(done) {
|
500 | //
|
501 | // var client = Client().get('http://localhost/', function(err, res) {
|
502 | // done();
|
503 | // })
|
504 | // ;
|
505 | //
|
506 | // assert(client instanceof Client);
|
507 | //
|
508 | // });
|
509 | //
|
510 | // it('should handle different length arguments');
|
511 | // it('should handle different length arguments');
|
512 | //
|
513 | // it('should call the callback on success');
|
514 | // it('should call the callback on error');
|
515 | //
|
516 | // it('should call the callback with the global context');
|
517 | //
|
518 | // it('should pass an error if it cannot connect to the server');
|
519 | //
|
520 | // });
|
521 | //
|
522 | // describe('OOP-style', function() {
|
523 | //
|
524 | // it('should return a `Request`', function() {
|
525 | // var request = Client().get('http://localhost/');
|
526 | // assert(request instanceof Request);
|
527 | // });
|
528 | //
|
529 | // it('should add a `.send()` method to the request', function() {
|
530 | // var request = Client().get('http://localhost/');
|
531 | // assert(typeof(request.send), 'function');
|
532 | // });
|
533 | //
|
534 | // });
|
535 | //
|
536 | //});
|
537 |
|
538 | });
|