[tests] drop the test of own streams, moved the usable tests

This commit is contained in:
cronopio 2013-09-16 18:59:48 -05:00
parent 1cb967b90a
commit dc9d7e5452
2 changed files with 153 additions and 46 deletions

View File

@ -1,45 +0,0 @@
var caronte = require('../'),
ForwardStream = require('../lib/caronte/streams/forward');
expect = require('expect.js'),
Writable = require('stream').Writable,
http = require('http');
describe('lib/caronte/streams/forward.js', function () {
describe('forward stream constructor', function () {
it('should be an instance of Writable stream and get the correct options and methods', function () {
var stubOptions = {
key: 'value'
};
var forwardProxy = new ForwardStream(stubOptions);
expect(forwardProxy).to.be.a(Writable);
expect(forwardProxy.options).to.eql({ key: 'value' });
expect(forwardProxy.onPipe).to.be.a('function');
expect(forwardProxy.onFinish).to.be.a('function');
expect(forwardProxy._events).to.have.property('pipe');
expect(forwardProxy._events).to.have.property('finish');
});
});
describe('should pipe the request and finish it', function () {
it('should make the request on pipe and finish it', function(done) {
var result;
var p = caronte.createProxyServer({
forward: 'http://127.0.0.1:8080'
}).listen('8081')
var s = http.createServer(function(req, res) {
expect(req.method).to.eql('GET');
s.close();
p.close();
done();
});
s.listen('8080');
http.request('http://127.0.0.1:8081', function() {}).end();
});
});
});

View File

@ -1,5 +1,6 @@
var caronte = require('../lib/caronte'),
expect = require('expect.js');
expect = require('expect.js'),
http = require('http');
describe('lib/caronte.js', function() {
describe('#createProxyServer', function() {
@ -24,4 +25,155 @@ describe('lib/caronte.js', function() {
expect(obj.listen).to.be.a(Function);
});
});
describe('#createProxyServer with forward options and using web-incoming passes', function () {
it('should pipe the request using web-incoming#stream method', function (done) {
var proxy = caronte.createProxyServer({
forward: 'http://127.0.0.1:8080'
}).listen('8081')
var source = http.createServer(function(req, res) {
expect(req.method).to.eql('GET');
expect(req.headers.host.split(':')[1]).to.eql('8081');
source.close();
proxy.close();
done();
});
source.listen('8080');
http.request('http://127.0.0.1:8081', function() {}).end();
})
});
describe('#createProxyServer using the web-incoming passes', function () {
it('should make the request on pipe and finish it', function(done) {
var proxy = caronte.createProxyServer({
target: 'http://127.0.0.1:8080'
}).listen('8081');
var source = http.createServer(function(req, res) {
expect(req.method).to.eql('POST');
expect(req.headers['x-forwarded-for']).to.eql('127.0.0.1');
expect(req.headers.host.split(':')[1]).to.eql('8081');
source.close();
proxy.close();
done();
});
source.listen('8080');
http.request({
hostname: '127.0.0.1',
port: '8081',
method: 'POST',
headers: {
'x-forwarded-for': '127.0.0.1'
}
}, function() {}).end();
});
});
describe('#createProxyServer using the web-incoming passes', function () {
it('should make the request, handle response and finish it', function(done) {
var proxy = caronte.createProxyServer({
target: 'http://127.0.0.1:8080'
}).listen('8081');
var source = http.createServer(function(req, res) {
expect(req.method).to.eql('GET');
expect(req.headers.host.split(':')[1]).to.eql('8081');
res.writeHead(200, {'Content-Type': 'text/plain'})
res.end('Hello from ' + source.address().port);
});
source.listen('8080');
http.request({
hostname: '127.0.0.1',
port: '8081',
method: 'GET',
}, function(res) {
expect(res.statusCode).to.eql(200);
res.on('data', function (data) {
expect(data.toString()).to.eql('Hello from 8080');
});
res.on('end', function () {
source.close();
proxy.close();
done();
});
}).end();
});
});
describe('#createProxyServer() method with error response', function () {
it('should make the request and emit the error event', function(done) {
var proxy = caronte.createProxyServer({
target: 'http://127.0.0.1:8080'
});
proxy.ee.on('caronte:outgoing:web:error', function (err) {
expect(err).to.be.an(Error);
expect(err.code).to.be('ECONNREFUSED');
proxyServer.close();
done();
})
var proxyServer = proxy.listen('8081');
http.request({
hostname: '127.0.0.1',
port: '8081',
method: 'GET',
}, function() {}).end();
});
});
describe('#createProxyServer using the web-incoming passes', function () {
it('should emit events correclty', function(done) {
var proxy = caronte.createProxyServer({
target: 'http://127.0.0.1:8080'
}),
proxyServer = proxy.listen('8081'),
source = http.createServer(function(req, res) {
expect(req.method).to.eql('GET');
expect(req.headers.host.split(':')[1]).to.eql('8081');
res.writeHead(200, {'Content-Type': 'text/plain'})
res.end('Hello from ' + source.address().port);
}),
events = [];
source.listen('8080');
proxy.ee.on('caronte:**', function (uno, dos, tres) {
events.push(this.event);
})
http.request({
hostname: '127.0.0.1',
port: '8081',
method: 'GET',
}, function(res) {
expect(res.statusCode).to.eql(200);
res.on('data', function (data) {
expect(data.toString()).to.eql('Hello from 8080');
});
res.on('end', function () {
expect(events).to.contain('caronte:outgoing:web:begin');
expect(events).to.contain('caronte:outgoing:web:end');
source.close();
proxyServer.close();
done();
});
}).end();
});
});
});