|
| 1 | +var caronte = require('../'), |
| 2 | + ProxyStream = require('../lib/caronte/streams/proxy'); |
| 3 | + expect = require('expect.js'), |
| 4 | + Duplex = require('stream').Duplex, |
| 5 | + http = require('http'); |
| 6 | + |
| 7 | + |
| 8 | +describe('lib/caronte/streams/proxy.js', function () { |
| 9 | + describe('proxy stream constructor', function () { |
| 10 | + it('should be an instance of Duplex stream and get the correct options and methods', function () { |
| 11 | + var stubOptions = { |
| 12 | + key: 'value' |
| 13 | + }; |
| 14 | + var proxyStream = new ProxyStream(stubOptions); |
| 15 | + |
| 16 | + expect(proxyStream).to.be.a(Duplex); |
| 17 | + expect(proxyStream.options).to.eql({ key: 'value' }); |
| 18 | + expect(proxyStream.onPipe).to.be.a('function'); |
| 19 | + expect(proxyStream.onFinish).to.be.a('function'); |
| 20 | + expect(proxyStream._events).to.have.property('pipe'); |
| 21 | + expect(proxyStream._events).to.have.property('finish'); |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + describe('should pipe the request and finish it', function () { |
| 26 | + it('should make the request on pipe and finish it', function(done) { |
| 27 | + var result; |
| 28 | + |
| 29 | + var p = caronte.createProxyServer({ |
| 30 | + target: 'http://127.0.0.1:8080' |
| 31 | + }).listen('8081'); |
| 32 | + |
| 33 | + var s = http.createServer(function(req, res) { |
| 34 | + expect(req.headers['x-forwarded-for']).to.eql('127.0.0.1'); |
| 35 | + s.close(); |
| 36 | + p.close(); |
| 37 | + done(); |
| 38 | + }); |
| 39 | + |
| 40 | + s.listen('8080'); |
| 41 | + |
| 42 | + http.request({ |
| 43 | + hostname: '127.0.0.1', |
| 44 | + port: '8081', |
| 45 | + method: 'POST', |
| 46 | + headers: { |
| 47 | + 'x-forwarded-for': '127.0.0.1' |
| 48 | + } |
| 49 | + }, function() {}).end(); |
| 50 | + }); |
| 51 | + }); |
| 52 | +}); |
0 commit comments