-
Notifications
You must be signed in to change notification settings - Fork 828
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for proxyReq - host header
- Loading branch information
Showing
9 changed files
with
87 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
|
||
}); |