Skip to content

Commit

Permalink
[tests] fixing tests, fixed some typos and changed how passes are stored
Browse files Browse the repository at this point in the history
  • Loading branch information
cronopio committed Oct 9, 2013
1 parent b333e63 commit a704213
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
27 changes: 13 additions & 14 deletions lib/http-proxy/index.js
Expand Up @@ -2,6 +2,8 @@ var httpProxy = exports,
extend = require('util')._extend,
parse_url = require('url').parse,
EE3 = require('eventemitter3').EventEmitter,
http = require('http'),
https = require('https'),
web = require('./passes/web-incoming'),
ws = require('./passes/ws-incoming');

Expand All @@ -24,16 +26,9 @@ httpProxy.Server = ProxyServer;
*/

function createRightProxy(type) {
var passes = (type === 'ws') ? ws : web;

return function(options) {

passes = Object.keys(passes).map(function(pass) {
return passes[pass];
});

return function(req, res /*, [head], [opts] */) {
var passes = this.passes || passes,
var passes = (type === 'ws') ? this.wsPasses : this.webPasses,
args = [].slice.call(arguments),
cntr = args.length - 1,
head, cbl;
Expand Down Expand Up @@ -78,7 +73,7 @@ function createRightProxy(type) {
* refer to the connection socket
* pass(req, socket, options, head)
*/
if(passes[i](req, res, cbl ? this : false, head, cbl)) { // passes can return a truthy value to halt the loop
if(passes[i](req, res, cbl ? false : this, head, cbl)) { // passes can return a truthy value to halt the loop
break;
}
}
Expand All @@ -94,20 +89,24 @@ function ProxyServer(options) {
this.ws = createRightProxy('ws')(options);
this.options = options;

this.passes = Object.keys(passes).map(function(pass) {
return passes[pass];
this.webPasses = Object.keys(web).map(function(pass) {
return web[pass];
});

this.wsPasses = Object.keys(ws).map(function(pass) {
return ws[pass];
});
}

ProxyServer.prototype.listen = function(port) {
var self = this,
closure = function(req, res) { self.web(req, res); };

this._server = options.ssl ?
this._server = this.options.ssl ?
https.createServer(this.options.ssl, closure) :
http.createServer(closure);

if(options.ws) {
if(this.options.ws) {
this._server.on('upgrade', function(req, socket, head) { self.ws(req, socket, head); });
}

Expand Down Expand Up @@ -137,4 +136,4 @@ ProxyServer.prototype.after = function(passName, callback) {
this.passes.splice(i++, 0, callback);
};

require('util').inherits(ProxyServer, EE3);
//require('util').inherits(ProxyServer, EE3);
14 changes: 7 additions & 7 deletions test/lib-http-proxy-test.js
Expand Up @@ -40,7 +40,7 @@ describe('lib/http-proxy.js', function() {
expect(req.method).to.eql('GET');
expect(req.headers.host.split(':')[1]).to.eql('8081');
source.close();
proxy.close();
proxy._server.close();
done();
});

Expand All @@ -61,7 +61,7 @@ describe('lib/http-proxy.js', function() {
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();
proxy._server.close();
done();
});

Expand Down Expand Up @@ -106,7 +106,7 @@ describe('lib/http-proxy.js', function() {

res.on('end', function () {
source.close();
proxy.close();
proxy._server.close();
done();
});
}).end();
Expand All @@ -122,7 +122,7 @@ describe('lib/http-proxy.js', function() {
proxy.ee.on('http-proxy:outgoing:web:error', function (err) {
expect(err).to.be.an(Error);
expect(err.code).to.be('ECONNREFUSED');
proxyServer.close();
proxyServer._server.close();
done();
})

Expand Down Expand Up @@ -174,7 +174,7 @@ describe('lib/http-proxy.js', function() {
expect(events).to.contain('http-proxy:outgoing:web:begin');
expect(events).to.contain('http-proxy:outgoing:web:end');
source.close();
proxyServer.close();
proxyServer._server.close();
done();
});
}).end();
Expand All @@ -198,7 +198,7 @@ describe('lib/http-proxy.js', function() {
client.on('message', function (msg) {
expect(msg).to.be('Hello over websockets');
client.close();
proxyServer.close();
proxyServer._server.close();
destiny.close();
done();
});
Expand Down Expand Up @@ -229,7 +229,7 @@ describe('lib/http-proxy.js', function() {

client.on('outgoing', function (data) {
expect(data).to.be('Hello over websockets');
proxyServer.close();
proxyServer._server.close();
destiny.server.close();
done();
});
Expand Down

0 comments on commit a704213

Please sign in to comment.