Skip to content
Permalink
Browse files

Added tests for proxyReq - host header

  • Loading branch information
chimurai committed Mar 15, 2015
1 parent c82dac0 commit 8e9d027aa4497358c079dfb3777c8d2950ea1c2b
Showing with 87 additions and 40 deletions.
  1. +1 −8 index.js
  2. +2 −8 lib/utils.js
  3. +6 −0 lib/utils/has-context.js
  4. +6 −0 lib/utils/proxy-req-host.js
  5. +2 −2 package.json
  6. +0 −22 test/util-test.js
  7. +20 −0 test/utils-has-context-test.js
  8. +36 −0 test/utils-proxy-req-host.js
  9. +14 −0 test/utils-test.js
@@ -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;

@@ -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;
@@ -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')
}
@@ -0,0 +1,6 @@
var url = require('url');

module.exports = function (context, uri) {
var urlPath = url.parse(uri).path;
return urlPath.indexOf(context) === 0;
};
@@ -0,0 +1,6 @@
module.exports = function (proxyReq, req, res, options) {
var host = options.target.host;
if (host) {
proxyReq.setHeader('host', host);
}
};
@@ -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",

This file was deleted.

@@ -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;
});
});
@@ -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');
});

});
@@ -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.
You can’t perform that action at this time.