UNPKG

2.86 kBJavaScriptView Raw
1/* global describe, it, before, beforeEach, after, afterEach */
2var Waif = require('../');
3var pipe = require('../pipe');
4var send = require('../send');
5var http = require('http');
6var should = require('should');
7var through = require('through2');
8
9
10describe('pipe service', function() {
11 var waif = null;
12
13 before(function() {
14 waif = Waif.createInstance();
15
16 this.service = waif('local')
17 .use(send, {msg: 'ok'})
18 .listen(3005);
19
20 this.proxy = waif('proxy')
21 .pipe(this.service.requestUrl())
22 .listen();
23
24 this.proxyParams = waif('proxyParams')
25 .pipe('/:foo/:bar', this.service.requestUrl() + ':bar/:foo')
26 .listen();
27
28 waif.start();
29 });
30
31 after(function() { waif.stop(); });
32
33
34 it('request works', function(doneFn) {
35 this.proxy('filename.jpg', function(err, resp, body) {
36 if (err || resp.statusCode !== 200) { return doneFn(resp.statusCode); }
37 should.exist(body);
38 body.should.have.property('msg', 'ok');
39 doneFn();
40 });
41 });
42
43 it('allows parameter restructuring', function(doneFn) {
44 this.proxyParams('mike/bike', function(err, resp, body) {
45 if (err || resp.statusCode !== 200) { return doneFn(resp.statusCode); }
46 should.exist(body);
47 body.should.have.property('msg', 'ok');
48 doneFn();
49 });
50 });
51});
52
53
54describe('killing streams mid-way', function() {
55 var waif = null;
56
57 var server = http.createServer();
58 var instance;
59
60 // we just close the request after answering it.
61 server.on('request', function(req, res, next) {
62 if (req.url === '/response') {
63 return res.socket.end();
64 }
65 if (req.url === '/server') {
66 server.close();
67 }
68 if (req.url === '/request') {
69 return req.socket.end();
70 }
71 });
72
73 before(function(done) {
74 waif = Waif.createInstance();
75
76 instance = server.listen(0, '0.0.0.0', function(err) {
77 waif('fails')
78 .pipe('http://localhost:'+instance.address().port)
79 .listen(0);
80
81 waif.start();
82
83 done();
84 });
85 });
86
87 after(function() {
88 waif.stop();
89 });
90
91 it('killing a response', function(done) {
92 waif('fails')('response', function(err, resp, body) {
93 should.not.exist(err);
94 resp.statusCode.should.not.eql(200);
95 body.should.have.property('code', 'ECONNRESET');
96 done();
97 });
98 });
99
100 it('killing a request', function(done) {
101 waif('fails')('response', function(err, resp, body) {
102 should.not.exist(err);
103 resp.statusCode.should.not.eql(200);
104 body.should.have.property('code', 'ECONNRESET');
105 done();
106 });
107 });
108
109
110 it.skip('killing the server', function(done) {
111 waif('fails')('server', function(err, resp, body) {
112 should.not.exist(err);
113 resp.statusCode.should.not.eql(200);
114 body.should.have.property('code', 'ECONNRESET');
115 done();
116 });
117 });
118});