Skip to content

Commit

Permalink
Added tests for proxyReq - host header
Browse files Browse the repository at this point in the history
  • Loading branch information
chimurai committed Mar 15, 2015
1 parent c82dac0 commit 8e9d027
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 40 deletions.
9 changes: 1 addition & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var httpProxyMiddleware = function (context, opts) {

console.log('[http-proxy-middleware] Proxy created:', context, proxyOptions.target);

proxy.on('proxyReq', proxyReqHost);
proxy.on('proxyReq', utils.proxyReqHost);

return fnProxyMiddleWare;

Expand All @@ -20,13 +20,6 @@ var httpProxyMiddleware = function (context, opts) {
}
}

function proxyReqHost (proxyReq, req, res, options) {
var host = options.target.host;
if (host) {
proxyReq.setHeader('host', host);
}
}

};

module.exports = httpProxyMiddleware;
10 changes: 2 additions & 8 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
var url = require('url');

function hasContext (context, uri) {
var urlPath = url.parse(uri).path;
return urlPath.indexOf(context) === 0;
}

module.exports = {
hasContext: hasContext
hasContext: require('./utils/has-context'),
proxyReqHost: require('./utils/proxy-req-host')
}
6 changes: 6 additions & 0 deletions lib/utils/has-context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var url = require('url');

module.exports = function (context, uri) {
var urlPath = url.parse(uri).path;
return urlPath.indexOf(context) === 0;
};
6 changes: 6 additions & 0 deletions lib/utils/proxy-req-host.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = function (proxyReq, req, res, options) {
var host = options.target.host;
if (host) {
proxyReq.setHeader('host', host);
}
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
},
"homepage": "https://github.com/chimurai/http-proxy-middleware",
"devDependencies": {
"mocha": "^2.2.1",
"should": "^5.2.0"
"chai": "^2.1.1",
"mocha": "^2.2.1"
},
"dependencies": {
"http-proxy": "^1.9.0",
Expand Down
22 changes: 0 additions & 22 deletions test/util-test.js

This file was deleted.

20 changes: 20 additions & 0 deletions test/utils-has-context-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var expect = require('chai').expect;
var hasContext = require('../lib/utils/has-context');

describe('utils#hasContext(context, url)', function () {

it('should return true when the context is present in url', function () {
var result = hasContext('/api', 'http://localhost/api/foo/bar');
expect(result).to.be.true;
});

it('should return false when the context is not present in url', function () {
var result = hasContext('/abc', 'http://localhost/api/foo/bar');
expect(result).to.be.false;
});

it('should return false when the context is present half way in url', function () {
var result = hasContext('/foo', 'http://localhost/api/foo/bar');
expect(result).to.be.false;
});
});
36 changes: 36 additions & 0 deletions test/utils-proxy-req-host.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var expect = require('chai').expect;
var proxyReqHost = require('../lib/utils/proxy-req-host');

// Simulate http-proxy's proxyReq
var ProxyReq = function () {
var headers = {};

return {
getHeader : function (key) {
return headers[key];
},
setHeader : function (key, val) {
headers[key] = val;
}
}
};

var proxyReqOptions = {
target : {
host : 'localhost.dev'
}
};

describe('utils#proxyReqHost(proxyReq, req, res, options)', function () {

it('should set the header: host to match the target host', function () {

var proxyReq = new ProxyReq();
proxyReqHost(proxyReq, {}, {}, proxyReqOptions);

var result = proxyReq.getHeader('host');

expect(result).to.equal('localhost.dev');
});

});
14 changes: 14 additions & 0 deletions test/utils-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var expect = require('chai').expect;
var utils = require('../lib/utils');

describe('utils', function () {

it('should have function: hasContext', function () {
expect(utils.hasContext).to.be.a('function');
});

it('should have function: proxyReqHost', function () {
expect(utils.proxyReqHost).to.be.a('function');
});

});

0 comments on commit 8e9d027

Please sign in to comment.