Skip to content

Commit

Permalink
[tests] drop the test of own streams, moved the usable tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cronopio committed Sep 16, 2013
1 parent 1cb967b commit dc9d7e5
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 46 deletions.
45 changes: 0 additions & 45 deletions test/lib-caronte-streams-forward-test.js

This file was deleted.

154 changes: 153 additions & 1 deletion test/lib-caronte-test.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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: 'https://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('https://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: 'https://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: 'https://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: 'https://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: 'https://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();
});
});
});

0 comments on commit dc9d7e5

Please sign in to comment.