express-http-proxy
Advanced tools
Comparing version 1.2.0 to 1.3.0
27
index.js
@@ -7,3 +7,2 @@ 'use strict'; | ||
var ScopeContainer = require('./lib/scopeContainer'); | ||
@@ -19,3 +18,4 @@ var assert = require('assert'); | ||
var decorateUserResHeaders = require('./app/steps/decorateUserResHeaders'); | ||
var handleProxyErrors = require('./app/steps/handleProxyErrors'); | ||
var filterUserRequest = require('./app/steps/filterUserRequest'); | ||
var handleProxyErrors = require('./app/steps/handleProxyErrors'); | ||
var maybeSkipToNextHandler = require('./app/steps/maybeSkipToNextHandler'); | ||
@@ -37,5 +37,7 @@ var prepareProxyReq = require('./app/steps/prepareProxyReq'); | ||
// false, null, undefined, etc. | ||
if (!container.options.filter(req, res)) { return next(); } | ||
buildProxyReq(container) | ||
//if (!container.options.filter(req, res)) { return next(); } | ||
filterUserRequest(container) | ||
.then(buildProxyReq) | ||
.then(resolveProxyHost) | ||
@@ -52,7 +54,14 @@ .then(decorateProxyReqOpts) | ||
.then(sendUserRes) | ||
.catch(function(err) { | ||
var resolver = (container.options.proxyErrorHandler) ? | ||
container.options.proxyErrorHandler : | ||
handleProxyErrors; | ||
resolver(err, res, next); | ||
.catch(function (err) { | ||
// I sometimes reject without an error to shortcircuit the remaining | ||
// steps and return control to the host application. | ||
if (err) { | ||
var resolver = (container.options.proxyErrorHandler) ? | ||
container.options.proxyErrorHandler : | ||
handleProxyErrors; | ||
resolver(err, res, next); | ||
} else { | ||
next(); | ||
} | ||
}); | ||
@@ -59,0 +68,0 @@ }; |
'use strict'; | ||
function chunkLength(chunks) { | ||
return chunks.reduce(function(len, buf) { | ||
return chunks.reduce(function (len, buf) { | ||
return len + buf.length; | ||
@@ -6,0 +6,0 @@ }, 0); |
'use strict'; | ||
module.exports = function(val) { | ||
module.exports = function (val) { | ||
return (typeof val === 'undefined' || val === '' || val === null); | ||
}; |
@@ -5,3 +5,3 @@ 'use strict'; | ||
app.use('/status/:status', function(req, res) { | ||
app.use('/status/:status', function (req, res) { | ||
res.status(Number(req.params.status)); | ||
@@ -8,0 +8,0 @@ res.send(); |
@@ -70,2 +70,3 @@ 'use strict'; | ||
// prepare proxyRequest | ||
var reqOpt = { | ||
@@ -86,2 +87,3 @@ headers: reqHeaders(req, options), | ||
// extract to bodyContent object | ||
function bodyContent(req, res, options) { | ||
@@ -95,2 +97,3 @@ var parseReqBody = isUnset(options.parseReqBody) ? true : options.parseReqBody; | ||
// Returns a promise if no callback specified and global Promise exists. | ||
return getRawBody(req, { | ||
@@ -97,0 +100,0 @@ length: req.headers['content-length'], |
@@ -8,10 +8,12 @@ 'use strict'; | ||
function resolveBodyEncoding(reqBodyEncoding) { | ||
/* For reqBodyEncoding, these is a meaningful difference between null and | ||
/* For reqBodyEncoding, these is a meaningful difference between null and | ||
* undefined. null should be passed forward as the value of reqBodyEncoding, | ||
* and undefined should result in utf-8. | ||
*/ | ||
return reqBodyEncoding !== undefined ? reqBodyEncoding: 'utf-8'; | ||
return reqBodyEncoding !== undefined ? reqBodyEncoding : 'utf-8'; | ||
} | ||
// parse client arguments | ||
function resolveOptions(options) { | ||
@@ -23,3 +25,4 @@ options = options || {}; | ||
throw new Error( | ||
'decorateRequest is REMOVED; use proxyReqOptDecorator and proxyReqBodyDecorator instead. see README.md' | ||
'decorateRequest is REMOVED; use proxyReqOptDecorator and' + | ||
'proxyReqBodyDecorator instead. see README.md' | ||
); | ||
@@ -29,7 +32,13 @@ } | ||
if (options.forwardPath || options.forwardPathAsync) { | ||
console.warn('forwardPath and forwardPathAsync are DEPRECATED and should be replaced with proxyReqPathResolver'); | ||
console.warn( | ||
'forwardPath and forwardPathAsync are DEPRECATED' + | ||
'and should be replaced with proxyReqPathResolver' | ||
); | ||
} | ||
if (options.intercept) { | ||
console.warn('DEPRECATED: intercept. Use decorateUseRes instead. Please see README for more information.'); | ||
console.warn( | ||
'DEPRECATED: intercept. Use decorateUseRes instead.' + | ||
' Please see README for more information.' | ||
); | ||
} | ||
@@ -39,3 +48,5 @@ | ||
limit: options.limit || '1mb', | ||
proxyReqPathResolver: options.proxyReqPathResolver || options.forwardPathAsync || options.forwardPath, | ||
proxyReqPathResolver: options.proxyReqPathResolver | ||
|| options.forwardPathAsync | ||
|| options.forwardPath, | ||
proxyReqOptDecorator: options.proxyReqOptDecorator, | ||
@@ -46,7 +57,8 @@ proxyReqBodyDecorator: options.proxyReqBodyDecorator, | ||
proxyErrorHandler: options.proxyErrorHandler, | ||
filter: options.filter || defaultFilter, | ||
filter: options.filter, | ||
// For backwards compatability, we default to legacy behavior for newly added settings. | ||
parseReqBody: isUnset(options.parseReqBody) ? true : options.parseReqBody, | ||
preserveHostHdr: options.preserveHostHdr, | ||
memoizeHost: isUnset(options.memoizeHost) ? true: options.memoizeHost, | ||
memoizeHost: isUnset(options.memoizeHost) ? true : options.memoizeHost, | ||
reqBodyEncoding: resolveBodyEncoding(options.reqBodyEncoding), | ||
@@ -63,2 +75,3 @@ skipToNextHandlerFilter: options.skipToNextHandlerFilter, | ||
// automatically opt into stream mode if no response modifiers are specified | ||
resolved.stream = !resolved.skipToNextHandlerFilter && | ||
@@ -72,7 +85,2 @@ !resolved.userResDecorator && | ||
function defaultFilter() { | ||
// No-op version of filter. Allows everything! | ||
return true; | ||
} | ||
module.exports = resolveOptions; |
{ | ||
"name": "express-http-proxy", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "http proxy middleware for express", | ||
"engines": { | ||
"node": ">=4.0.0" | ||
"node": ">=6.0.0" | ||
}, | ||
"engineStrict": true, | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "npm -s run mocha && npm run -s lint && npm run -s jscs", | ||
"test": "npm -s run mocha && npm run -s lint", | ||
"test:debug": "mocha debug -R spec test --recursive", | ||
"mocha": "mocha -R spec test --recursive", | ||
"lint": "jshint index.js test/*.js test/**/*js lib/*js app/**/*js ", | ||
"jscs": "jscs index.js test/*.js test/**/*js lib/*js app/**/*js" | ||
"lint": "eslint index.js **/*js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/villadora/express-http-proxy.git" | ||
"url": "git://github.com/nkrimm/express-http-proxy.git" | ||
}, | ||
@@ -25,8 +23,8 @@ "keywords": [ | ||
"author": { | ||
"name": "villadora", | ||
"email": "jky239@gmail.com" | ||
"name": "Nik Krimm", | ||
"url": "https://github.com/monkpow" | ||
}, | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/villadora/express-http-proxy/issues" | ||
"url": "https://github.com/nkrimm/express-http-proxy/issues" | ||
}, | ||
@@ -37,2 +35,3 @@ "devDependencies": { | ||
"cookie-parser": "^1.4.3", | ||
"eslint": "^4.19.1", | ||
"express": "^4.15.4", | ||
@@ -66,10 +65,10 @@ "jscs": "^3.0.7", | ||
{ | ||
"name": "Wei Gao", | ||
"name": "villadora", | ||
"email": "jky239@gmail.com" | ||
}, | ||
{ | ||
"name": "Nik Krimm", | ||
"url": "https://github.com/monkpow" | ||
"name": "Wei Gao", | ||
"email": "jky239@gmail.com" | ||
} | ||
] | ||
} |
@@ -83,3 +83,3 @@ # express-http-proxy [![NPM version](https://badge.fury.io/js/express-http-proxy.svg)](http://badge.fury.io/js/express-http-proxy) [![Build Status](https://travis-ci.org/villadora/express-http-proxy.svg?branch=master)](https://travis-ci.org/villadora/express-http-proxy) [![Dependency Status](https://gemnasium.com/villadora/express-http-proxy.svg)](https://gemnasium.com/villadora/express-http-proxy) | ||
```http://smoogle.com/search/path?q=123```; the path is | ||
```/search/path?q=123```. | ||
```/search/path?q=123```. Authors using this resolver must also handle the query parameter portion of the path. | ||
@@ -91,9 +91,11 @@ Provide a proxyReqPathResolver function if you'd like to | ||
```js | ||
app.use('/proxy', proxy('localhost:12345', { | ||
proxyReqPathResolver: function(req) { | ||
return require('url').parse(req.url).path; | ||
} | ||
})); | ||
app.use(proxy('localhost:12345', { | ||
proxyReqPathResolver: function (req) { | ||
var parts = req.url.split('?'); | ||
var queryString = parts[1]; | ||
var updatedPath = parts[0].replace(/test/, 'tent'); | ||
return updatedPath + (queryString ? '?' + queryString : ''); | ||
} | ||
})); | ||
``` | ||
Promise form | ||
@@ -106,4 +108,6 @@ | ||
setTimeout(function () { // simulate async | ||
// in this case I expect a request to /proxy => localhost:12345/a/different/path | ||
var resolvedPathValue = "/a/different/path"; | ||
var parts = req.url.split('?'); | ||
var queryString = parts[1]; | ||
var updatedPath = parts[0].replace(/test/, 'tent'); | ||
var resolvedPathValue = updatedPath + (queryString ? '?' + queryString : ''); | ||
resolve(resolvedPathValue); | ||
@@ -110,0 +114,0 @@ }, 200); |
@@ -11,10 +11,10 @@ 'use strict'; | ||
describe('body encoding', function() { | ||
describe('body encoding', function () { | ||
var server; | ||
before(function() { | ||
before(function () { | ||
server = startProxyTarget(8109, 1000); | ||
}); | ||
after(function() { | ||
after(function () { | ||
server.close(); | ||
@@ -35,3 +35,3 @@ }); | ||
it('allow raw data', function(done) { | ||
it('allow raw data', function (done) { | ||
var filename = os.tmpdir() + '/express-http-proxy-test-' + (new Date()).getTime() + '-png-transparent.png'; | ||
@@ -42,3 +42,3 @@ var app = express(); | ||
reqBodyEncoding: null, | ||
proxyReqBodyDecorator: function(bodyContent) { | ||
proxyReqBodyDecorator: function (bodyContent) { | ||
assert((new Buffer(bodyContent).toString('hex')).indexOf(pngData.toString('hex')) >= 0, | ||
@@ -50,3 +50,3 @@ 'body should contain same data'); | ||
fs.writeFile(filename, pngData, function(err) { | ||
fs.writeFile(filename, pngData, function (err) { | ||
if (err) { throw err; } | ||
@@ -56,3 +56,3 @@ request(app) | ||
.attach('image', filename) | ||
.end(function(err) { | ||
.end(function (err) { | ||
fs.unlinkSync(filename); | ||
@@ -65,2 +65,3 @@ // This test is both broken and I think unnecessary. | ||
//assert(response.indexOf(pngData.toString('base64')) >= 0, 'response should include original raw data'); | ||
done(err); | ||
@@ -72,4 +73,4 @@ }); | ||
describe('when user sets parseReqBody', function() { | ||
it('should not parse body', function(done) { | ||
describe('when user sets parseReqBody', function () { | ||
it('should not parse body', function (done) { | ||
var filename = os.tmpdir() + '/express-http-proxy-test-' + (new Date()).getTime() + '-png-transparent.png'; | ||
@@ -79,3 +80,3 @@ var app = express(); | ||
parseReqBody: false, | ||
proxyReqBodyDecorator: function(bodyContent) { | ||
proxyReqBodyDecorator: function (bodyContent) { | ||
assert(!bodyContent, 'body content should not be parsed.'); | ||
@@ -86,3 +87,3 @@ return bodyContent; | ||
fs.writeFile(filename, pngData, function(err) { | ||
fs.writeFile(filename, pngData, function (err) { | ||
if (err) { throw err; } | ||
@@ -92,3 +93,3 @@ request(app) | ||
.attach('image', filename) | ||
.end(function(err) { | ||
.end(function (err) { | ||
fs.unlinkSync(filename); | ||
@@ -101,2 +102,3 @@ // This test is both broken and I think unnecessary. | ||
// assert(response.indexOf(pngData.toString('base64')) >= 0, 'response should include original raw data'); | ||
done(err); | ||
@@ -106,3 +108,3 @@ }); | ||
}); | ||
it('should not fail on large limit', function(done) { | ||
it('should not fail on large limit', function (done) { | ||
var filename = os.tmpdir() + '/express-http-proxy-test-' + (new Date()).getTime() + '-png-transparent.png'; | ||
@@ -114,3 +116,3 @@ var app = express(); | ||
})); | ||
fs.writeFile(filename, pngData, function(err) { | ||
fs.writeFile(filename, pngData, function (err) { | ||
if (err) { throw err; } | ||
@@ -120,3 +122,3 @@ request(app) | ||
.attach('image', filename) | ||
.end(function(err) { | ||
.end(function (err) { | ||
fs.unlinkSync(filename); | ||
@@ -130,2 +132,3 @@ assert(err === null); | ||
//assert(response.indexOf(pngData.toString('base64')) >= 0, 'response should include original raw data'); | ||
done(err); | ||
@@ -135,3 +138,3 @@ }); | ||
}); | ||
it('should fail with an error when exceeding limit', function(done) { | ||
it('should fail with an error when exceeding limit', function (done) { | ||
var app = express(); | ||
@@ -142,3 +145,3 @@ app.use(proxy('localhost:8109', { | ||
// silence jshint warning about unused vars - express error handler *needs* 4 args | ||
app.use(function(err, req, res, next) {// jshint ignore:line | ||
app.use(function (err, req, res, next) { // eslint-disable-line no-unused-vars | ||
res.json(err); | ||
@@ -149,3 +152,3 @@ }); | ||
.send({ some: 'json' }) | ||
.end(function(err, response) { | ||
.end(function (err, response) { | ||
assert(response.body.message === 'request entity too large'); | ||
@@ -158,4 +161,4 @@ done(); | ||
describe('when user sets reqBodyEncoding', function() { | ||
it('should set the accepts-charset header', function(done) { | ||
describe('when user sets reqBodyEncoding', function () { | ||
it('should set the accepts-charset header', function (done) { | ||
var app = express(); | ||
@@ -167,3 +170,3 @@ app.use(proxy('httpbin.org', { | ||
.get('/headers') | ||
.end(function(err, res) { | ||
.end(function (err, res) { | ||
if (err) { throw err; } | ||
@@ -170,0 +173,0 @@ assert.equal(res.body.headers['Accept-Charset'], 'utf-16'); |
@@ -8,13 +8,15 @@ 'use strict'; | ||
describe('when server responds with an error', function() { | ||
describe('when server responds with an error', function () { | ||
this.timeout(10000); | ||
var app, slowTarget, serverReference; | ||
var app; | ||
var slowTarget; | ||
var serverReference; | ||
beforeEach(function() { | ||
beforeEach(function () { | ||
app = express(); | ||
}); | ||
afterEach(function() { | ||
afterEach(function () { | ||
serverReference.close(); | ||
@@ -29,7 +31,7 @@ }); | ||
STATUS_CODES.forEach(function(statusCode) { | ||
STATUS_CODES.forEach(function (statusCode) { | ||
it('express-http-proxy responds with ' + statusCode.text + | ||
'when proxy server responds ' + statusCode.code, function(done) { | ||
'when proxy server responds ' + statusCode.code, function (done) { | ||
slowTarget = express(); | ||
slowTarget.use(function(req, res) { res.sendStatus(statusCode.code); }); | ||
slowTarget.use(function (req, res) { res.sendStatus(statusCode.code); }); | ||
serverReference = slowTarget.listen(12345); | ||
@@ -46,3 +48,3 @@ | ||
.expect(statusCode.code) | ||
.end(function(err, res) { | ||
.end(function (err, res) { | ||
assert(err === null); | ||
@@ -49,0 +51,0 @@ assert(res.error); |
@@ -12,4 +12,4 @@ 'use strict'; | ||
path: '/cookieTest', | ||
fn: function(req, res) { | ||
Object.keys(req.cookies).forEach(function(key) { | ||
fn: function (req, res) { | ||
Object.keys(req.cookies).forEach(function (key) { | ||
res.cookie(key, req.cookies[key]); | ||
@@ -21,3 +21,3 @@ }); | ||
describe('proxies cookie', function() { | ||
describe('proxies cookie', function () { | ||
this.timeout(10000); | ||
@@ -28,3 +28,3 @@ | ||
beforeEach(function() { | ||
beforeEach(function () { | ||
proxyServer = proxyTarget(12346, 100, proxyRouteFn); | ||
@@ -35,12 +35,12 @@ app = express(); | ||
afterEach(function() { | ||
afterEach(function () { | ||
proxyServer.close(); | ||
}); | ||
it('set cookie', function(done) { | ||
it('set cookie', function (done) { | ||
request(app) | ||
.get('/cookieTest') | ||
.set('Cookie', 'myApp-token=12345667') | ||
.end(function(err, res) { | ||
var cookiesMatch = res.headers['set-cookie'].filter(function(item) { | ||
.end(function (err, res) { | ||
var cookiesMatch = res.headers['set-cookie'].filter(function (item) { | ||
return item.match(/myApp-token=12345667/); | ||
@@ -47,0 +47,0 @@ }); |
@@ -8,27 +8,28 @@ 'use strict'; | ||
describe('when userResHeaderDecorator is defined', function() { | ||
describe('when userResHeaderDecorator is defined', function () { | ||
this.timeout(10000); | ||
var app, serverReference; | ||
var app; | ||
var serverReference; | ||
afterEach(function() { | ||
afterEach(function () { | ||
serverReference.close(); | ||
}); | ||
beforeEach(function() { | ||
beforeEach(function () { | ||
app = express(); | ||
var pTarget = express(); | ||
pTarget.use(function(req, res) { res.json(req.headers); }); | ||
pTarget.use(function (req, res) { res.json(req.headers); }); | ||
serverReference = pTarget.listen(12345); | ||
}); | ||
afterEach(function() { | ||
afterEach(function () { | ||
serverReference.close(); | ||
}); | ||
it('provides an interface for updating headers', function(done) { | ||
it('provides an interface for updating headers', function (done) { | ||
app.use('/proxy', proxy('http://127.0.0.1:12345', { | ||
userResHeaderDecorator: function(headers /*, userReq, userRes, proxyReq, proxyRes */) { | ||
userResHeaderDecorator: function (headers /*, userReq, userRes, proxyReq, proxyRes */) { | ||
headers.boltedonheader = 'franky'; | ||
@@ -39,3 +40,3 @@ return headers; | ||
app.use(function(req, res) { | ||
app.use(function (req, res) { | ||
res.sendStatus(200); | ||
@@ -46,3 +47,3 @@ }); | ||
.get('/proxy') | ||
.expect(function(res) { | ||
.expect(function (res) { | ||
assert(res.headers.boltedonheader === 'franky'); | ||
@@ -49,0 +50,0 @@ }) |
@@ -11,3 +11,3 @@ 'use strict'; | ||
path: '/:errorCode', | ||
fn: function(req, res) { | ||
fn: function (req, res) { | ||
var errorCode = req.params.errorCode; | ||
@@ -21,7 +21,7 @@ if (errorCode === 'timeout') { | ||
describe('error handling can be over-ridden by user', function() { | ||
describe('error handling can be over-ridden by user', function () { | ||
var app = express(); | ||
var proxyServer; | ||
beforeEach(function() { | ||
beforeEach(function () { | ||
proxyServer = proxyTarget(12346, 100, proxyRouteFn); | ||
@@ -31,10 +31,10 @@ app = express(); | ||
afterEach(function() { | ||
afterEach(function () { | ||
proxyServer.close(); | ||
}); | ||
describe('when user provides a null function', function() { | ||
describe('when user provides a null function', function () { | ||
describe('when author sets a timeout that fires', function() { | ||
it('passes 504 directly to client', function(done) { | ||
describe('when author sets a timeout that fires', function () { | ||
it('passes 504 directly to client', function (done) { | ||
app.use(proxy('localhost:12346', { | ||
@@ -52,3 +52,3 @@ timeout: 1, | ||
it('passes status code (e.g. 504) directly to the client', function(done) { | ||
it('passes status code (e.g. 504) directly to the client', function (done) { | ||
app.use(proxy('localhost:12346')); | ||
@@ -58,3 +58,3 @@ request(app) | ||
.expect(504) | ||
.expect(function(res) { | ||
.expect(function (res) { | ||
assert(res.text === 'test case error'); | ||
@@ -66,3 +66,3 @@ return res; | ||
it('passes status code (e.g. 500) back to the client', function(done) { | ||
it('passes status code (e.g. 500) back to the client', function (done) { | ||
app.use(proxy('localhost:12346')); | ||
@@ -72,3 +72,3 @@ request(app) | ||
.expect(500) | ||
.end(function(err, res) { | ||
.end(function (err, res) { | ||
assert(res.text === 'test case error'); | ||
@@ -80,11 +80,11 @@ done(); | ||
describe('when user provides a handler function', function() { | ||
describe('when user provides a handler function', function () { | ||
var intentionallyWeirdStatusCode = 399; | ||
var intentionallyQuirkyStatusMessage = 'monkey skunky'; | ||
describe('when author sets a timeout that fires', function() { | ||
it('allows author to skip handling and handle in application step', function(done) { | ||
describe('when author sets a timeout that fires', function () { | ||
it('allows author to skip handling and handle in application step', function (done) { | ||
app.use(proxy('localhost:12346', { | ||
timeout: 1, | ||
proxyErrorHandler: function(err, res, next) { | ||
proxyErrorHandler: function (err, res, next) { | ||
next(err); | ||
@@ -94,3 +94,3 @@ } | ||
app.use(function(err, req, res, next) { // jshint ignore:line | ||
app.use(function (err, req, res, next) { // eslint-disable-line no-unused-vars | ||
if (err.code === 'ECONNRESET') { | ||
@@ -103,3 +103,3 @@ res.status(intentionallyWeirdStatusCode).send(intentionallyQuirkyStatusMessage); | ||
.get('/200') | ||
.expect(function(res) { | ||
.expect(function (res) { | ||
assert(res.text === intentionallyQuirkyStatusMessage); | ||
@@ -113,6 +113,6 @@ return res; | ||
it('allows authors to sub in their own handling', function(done) { | ||
it('allows authors to sub in their own handling', function (done) { | ||
app.use(proxy('localhost:12346', { | ||
timeout: 1, | ||
proxyErrorHandler: function(err, res, next) { | ||
proxyErrorHandler: function (err, res, next) { | ||
switch (err && err.code) { | ||
@@ -123,3 +123,3 @@ case 'ECONNRESET': { return res.status(405).send('504 became 405'); } | ||
} | ||
}})); | ||
} })); | ||
@@ -129,3 +129,3 @@ request(app) | ||
.expect(405) | ||
.expect(function(res) { | ||
.expect(function (res) { | ||
assert(res.text === '504 became 405'); | ||
@@ -137,3 +137,3 @@ return res; | ||
it('passes status code (e.g. 500) back to the client', function(done) { | ||
it('passes status code (e.g. 500) back to the client', function (done) { | ||
app.use(proxy('localhost:12346')); | ||
@@ -143,3 +143,3 @@ request(app) | ||
.expect(500) | ||
.end(function(err, res) { | ||
.end(function (err, res) { | ||
assert(res.text === 'test case error'); | ||
@@ -146,0 +146,0 @@ done(); |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var assert = require('assert'); | ||
@@ -6,4 +8,3 @@ var express = require('express'); | ||
describe('proxies headers', function() { | ||
'use strict'; | ||
describe('proxies headers', function () { | ||
this.timeout(10000); | ||
@@ -13,3 +14,3 @@ | ||
beforeEach(function() { | ||
beforeEach(function () { | ||
http = express(); | ||
@@ -23,7 +24,7 @@ http.use(proxy('http://httpbin.org', { | ||
it('passed as options', function(done) { | ||
it('passed as options', function (done) { | ||
request(http) | ||
.get('/headers') | ||
.expect(200) | ||
.end(function(err, res) { | ||
.end(function (err, res) { | ||
if (err) { return done(err); } | ||
@@ -35,3 +36,3 @@ assert(res.body.headers['X-Current-President'] === 'taft'); | ||
it('passed as on request', function(done) { | ||
it('passed as on request', function (done) { | ||
request(http) | ||
@@ -41,3 +42,3 @@ .get('/headers') | ||
.expect(200) | ||
.end(function(err, res) { | ||
.end(function (err, res) { | ||
if (err) { return done(err); } | ||
@@ -44,0 +45,0 @@ assert(res.body.headers['X-Powerererer']); |
@@ -7,3 +7,3 @@ 'use strict'; | ||
describe('host can be a dynamic function', function() { | ||
describe('host can be a dynamic function', function () { | ||
@@ -13,40 +13,41 @@ this.timeout(10000); | ||
var app = express(); | ||
describe('and memoization can be disabled', function() { | ||
var firstProxyApp = express(); | ||
var secondProxyApp = express(); | ||
// TODO: This seems like a bug factory. We will have intermittent port conflicts, yeah? | ||
function randomNumberInPortRange() { | ||
return Math.floor(Math.random() * 48000) + 1024; | ||
} | ||
var firstPort = randomNumberInPortRange(); | ||
var secondPort = randomNumberInPortRange(); | ||
describe('and memoization can be disabled', function () { | ||
var firstProxyApp = express(); | ||
var secondProxyApp = express(); | ||
// TODO: This seems like a bug factory. We will have intermittent port conflicts, yeah? | ||
var hostFn = function(req) { | ||
return 'localhost:' + req.params.port; | ||
}; | ||
function randomNumberInPortRange() { | ||
return Math.floor(Math.random() * 48000) + 1024; | ||
} | ||
var firstPort = randomNumberInPortRange(); | ||
var secondPort = randomNumberInPortRange(); | ||
app.use('/proxy/:port', proxy(hostFn, { memoizeHost: false })); | ||
var hostFn = function (req) { | ||
return 'localhost:' + req.params.port; | ||
}; | ||
firstProxyApp | ||
.get('/', function(req, res) { res.sendStatus(204); }) | ||
.listen(firstPort); | ||
app.use('/proxy/:port', proxy(hostFn, { memoizeHost: false })); | ||
secondProxyApp | ||
.get('/', function(req, res) { res.sendStatus(200); }) | ||
.listen(secondPort); | ||
firstProxyApp | ||
.get('/', function (req, res) { res.sendStatus(204); }) | ||
.listen(firstPort); | ||
it('when not memoized, host resolves to a second value on the seecond call', function(done) { | ||
request(app) | ||
.get('/proxy/' + firstPort) | ||
.expect(204) | ||
.end(function(err) { | ||
if (err) { | ||
return done(err); | ||
} | ||
request(app) | ||
.get('/proxy/' + secondPort) | ||
.expect(200, done); | ||
}); | ||
}); | ||
secondProxyApp | ||
.get('/', function (req, res) { res.sendStatus(200); }) | ||
.listen(secondPort); | ||
it('when not memoized, host resolves to a second value on the seecond call', function (done) { | ||
request(app) | ||
.get('/proxy/' + firstPort) | ||
.expect(204) | ||
.end(function (err) { | ||
if (err) { | ||
return done(err); | ||
} | ||
request(app) | ||
.get('/proxy/' + secondPort) | ||
.expect(200, done); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var assert = require('assert'); | ||
@@ -6,4 +8,3 @@ var express = require('express'); | ||
describe('proxies https', function() { | ||
'use strict'; | ||
describe('proxies https', function () { | ||
@@ -14,3 +15,3 @@ this.timeout(10000); | ||
beforeEach(function() { | ||
beforeEach(function () { | ||
app = express(); | ||
@@ -22,3 +23,3 @@ }); | ||
.get('/get?show_env=1') | ||
.end(function(err, res) { | ||
.end(function (err, res) { | ||
if (err) { return done(err); } | ||
@@ -31,5 +32,5 @@ assert(res.body.headers['X-Forwarded-Port'] === '443', 'Expects forwarded 443 Port'); | ||
describe('when host is a String', function() { | ||
describe('and includes "https" as protocol', function() { | ||
it('proxys via https', function(done) { | ||
describe('when host is a String', function () { | ||
describe('and includes "https" as protocol', function () { | ||
it('proxys via https', function (done) { | ||
app.use(proxy('https://httpbin.org')); | ||
@@ -39,5 +40,5 @@ assertSecureRequest(app, done); | ||
}); | ||
describe('option https is set to "true"', function() { | ||
it('proxys via https', function(done) { | ||
app.use(proxy('http://httpbin.org', {https: true})); | ||
describe('option https is set to "true"', function () { | ||
it('proxys via https', function (done) { | ||
app.use(proxy('http://httpbin.org', { https: true })); | ||
assertSecureRequest(app, done); | ||
@@ -48,12 +49,12 @@ }); | ||
describe('when host is a Function', function() { | ||
describe('returned value includes "https" as protocol', function() { | ||
it('proxys via https', function(done) { | ||
app.use(proxy(function() { return 'https://httpbin.org'; })); | ||
describe('when host is a Function', function () { | ||
describe('returned value includes "https" as protocol', function () { | ||
it('proxys via https', function (done) { | ||
app.use(proxy(function () { return 'https://httpbin.org'; })); | ||
assertSecureRequest(app, done); | ||
}); | ||
}); | ||
describe('option https is set to "true"', function() { | ||
it('proxys via https', function(done) { | ||
app.use(proxy(function() { return 'http://httpbin.org'; }, {https: true})); | ||
describe('option https is set to "true"', function () { | ||
it('proxys via https', function (done) { | ||
app.use(proxy(function () { return 'http://httpbin.org'; }, { https: true })); | ||
assertSecureRequest(app, done); | ||
@@ -60,0 +61,0 @@ }); |
@@ -7,16 +7,18 @@ 'use strict'; | ||
describe('when skipToNextHandlerFilter is defined', function() { | ||
describe('when skipToNextHandlerFilter is defined', function () { | ||
this.timeout(10000); | ||
var app, slowTarget, serverReference; | ||
var app; | ||
var slowTarget; | ||
var serverReference; | ||
beforeEach(function() { | ||
beforeEach(function () { | ||
app = express(); | ||
slowTarget = express(); | ||
slowTarget.use(function(req, res) { res.sendStatus(404); }); | ||
slowTarget.use(function (req, res) { res.sendStatus(404); }); | ||
serverReference = slowTarget.listen(12345); | ||
}); | ||
afterEach(function() { | ||
afterEach(function () { | ||
serverReference.close(); | ||
@@ -30,8 +32,8 @@ }); | ||
OUTCOMES.forEach(function(outcome) { | ||
describe('and returns ' + outcome.shouldSkip, function() { | ||
it('express-http-proxy' + (outcome.shouldSkip ? ' skips ' : ' doesnt skip ') + 'to next()', function(done) { | ||
OUTCOMES.forEach(function (outcome) { | ||
describe('and returns ' + outcome.shouldSkip, function () { | ||
it('express-http-proxy' + (outcome.shouldSkip ? ' skips ' : ' doesnt skip ') + 'to next()', function (done) { | ||
app.use('/proxy', proxy('http://127.0.0.1:12345', { | ||
skipToNextHandlerFilter: function(/*res*/) { | ||
skipToNextHandlerFilter: function (/*res*/) { | ||
return outcome.shouldSkip; | ||
@@ -41,3 +43,3 @@ } | ||
app.use(function(req, res) { | ||
app.use(function (req, res) { | ||
res.sendStatus(200); | ||
@@ -44,0 +46,0 @@ }); |
@@ -14,3 +14,3 @@ 'use strict'; | ||
path: '/poster', | ||
fn: function(req, res) { | ||
fn: function (req, res) { | ||
res.send(req.body); | ||
@@ -20,18 +20,19 @@ } | ||
describe('middleware compatibility', function() { | ||
describe('middleware compatibility', function () { | ||
var proxyServer; | ||
beforeEach(function() { | ||
beforeEach(function () { | ||
proxyServer = proxyTarget(12346, 100, proxyRouteFn); | ||
}); | ||
afterEach(function() { | ||
afterEach(function () { | ||
proxyServer.close(); | ||
}); | ||
it('should use req.body if defined', function(done) { | ||
it('should use req.body if defined', function (done) { | ||
var app = express(); | ||
// Simulate another middleware that puts req stream into the body | ||
app.use(function(req, res, next) { | ||
app.use(function (req, res, next) { | ||
var received = []; | ||
@@ -51,3 +52,3 @@ req.on('data', function onData(chunk) { | ||
app.use(proxy('localhost:12346', { | ||
intercept: function(rsp, data, req) { | ||
intercept: function (rsp, data, req) { | ||
assert(req.body); | ||
@@ -62,4 +63,4 @@ assert.equal(req.body.foo, 1); | ||
.post('/poster') | ||
.send({ 'mypost': 'hello'}) | ||
.expect(function(res) { | ||
.send({ mypost: 'hello' }) | ||
.expect(function (res) { | ||
assert.equal(res.body.foo, 1); | ||
@@ -71,3 +72,3 @@ assert.equal(res.body.mypost, 'hello'); | ||
it('should stringify req.body when it is a json body so it is written to proxy request', function(done) { | ||
it('should stringify req.body when it is a json body so it is written to proxy request', function (done) { | ||
var app = express(); | ||
@@ -85,3 +86,3 @@ app.use(bodyParser.json()); | ||
}) | ||
.expect(function(res) { | ||
.expect(function (res) { | ||
assert.equal(res.body.doorknob, 'wrect'); | ||
@@ -93,3 +94,3 @@ assert.equal(res.body.mypost, 'hello'); | ||
it('should convert req.body to a Buffer when reqAsBuffer is set', function(done) { | ||
it('should convert req.body to a Buffer when reqAsBuffer is set', function (done) { | ||
var app = express(); | ||
@@ -109,3 +110,3 @@ app.use(bodyParser.json()); | ||
}) | ||
.expect(function(res) { | ||
.expect(function (res) { | ||
assert.equal(res.body.doorknob, 'wrect'); | ||
@@ -112,0 +113,0 @@ assert.equal(res.body.mypost, 'hello'); |
@@ -12,3 +12,3 @@ 'use strict'; | ||
path: '/test', | ||
fn: function(req, res) { | ||
fn: function (req, res) { | ||
res.send(req.url); | ||
@@ -18,3 +18,3 @@ } | ||
describe('proxies query parameters', function() { | ||
describe('proxies query parameters', function () { | ||
this.timeout(10000); | ||
@@ -25,3 +25,3 @@ | ||
beforeEach(function() { | ||
beforeEach(function () { | ||
proxyServer = proxyTarget(12346, 100, proxyRouteFn); | ||
@@ -32,11 +32,11 @@ app = express(); | ||
afterEach(function() { | ||
afterEach(function () { | ||
proxyServer.close(); | ||
}); | ||
it('set cookie', function(done) { | ||
it('repeats query params to proxy server', function (done) { | ||
request(app) | ||
.get('/test?a=1&b=2&c=3') | ||
.end(function(err, res) { | ||
assert(res.text === '/test?a=1&b=2&c=3'); | ||
.end(function (err, res) { | ||
assert.equal(res.text, '/test?a=1&b=2&c=3'); | ||
done(err); | ||
@@ -43,0 +43,0 @@ }); |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var express = require('express'); | ||
@@ -7,4 +9,3 @@ var request = require('supertest'); | ||
describe('uses remote path', function() { | ||
'use strict'; | ||
describe('uses remote path', function () { | ||
@@ -18,8 +19,8 @@ this.timeout(10000); | ||
afterEach(function() { | ||
afterEach(function () { | ||
server.close(); | ||
}); | ||
proxyRoutes.forEach(function(path) { | ||
it('uses path component from inbound request', function(done) { | ||
proxyRoutes.forEach(function (path) { | ||
it('uses path component from inbound request', function (done) { | ||
@@ -31,4 +32,4 @@ var modifiedPath = path.replace(new RegExp(proxyKeyPath), ''); | ||
path: modifiedPath, | ||
fn: function(req, res) { | ||
res.json({path: path, modifiedPath: modifiedPath}); | ||
fn: function (req, res) { | ||
res.json({ path: path, modifiedPath: modifiedPath }); | ||
} | ||
@@ -44,3 +45,3 @@ }; | ||
.expect(200) | ||
.end(function(err, response) { | ||
.end(function (err, response) { | ||
if (err) { | ||
@@ -59,4 +60,3 @@ return done(err); | ||
describe('host can be a dynamic function', function() { | ||
'use strict'; | ||
describe('host can be a dynamic function', function () { | ||
@@ -71,3 +71,3 @@ this.timeout(10000); | ||
app.use('/proxy/:port', proxy(function(req) { | ||
app.use('/proxy/:port', proxy(function (req) { | ||
return 'localhost:' + req.params.port; | ||
@@ -78,3 +78,3 @@ }, { | ||
firstProxyApp.use('/', function(req, res) { | ||
firstProxyApp.use('/', function (req, res) { | ||
res.sendStatus(204); | ||
@@ -84,3 +84,3 @@ }); | ||
secondProxyApp.use('/', function(req, res) { | ||
secondProxyApp.use('/', function (req, res) { | ||
res.sendStatus(200); | ||
@@ -90,7 +90,7 @@ }); | ||
it('can proxy with session value', function(done) { | ||
it('can proxy with session value', function (done) { | ||
request(app) | ||
.get('/proxy/' + firstPort) | ||
.expect(204) | ||
.end(function(err) { | ||
.end(function (err) { | ||
if (err) { | ||
@@ -100,6 +100,6 @@ return done(err); | ||
request(app) | ||
.get('/proxy/' + secondPort) | ||
.expect(200, done); | ||
.get('/proxy/' + secondPort) | ||
.expect(200, done); | ||
}); | ||
}); | ||
}); |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var assert = require('assert'); | ||
@@ -7,6 +9,5 @@ var express = require('express'); | ||
function proxyTarget(port) { | ||
'use strict'; | ||
var other = express(); | ||
other.get('/', function(req, res) { | ||
other.get('/', function (req, res) { | ||
res.send('Success'); | ||
@@ -17,7 +18,7 @@ }); | ||
describe('proxies to requested port', function() { | ||
'use strict'; | ||
describe('proxies to requested port', function () { | ||
var other; | ||
var http; | ||
var other, http; | ||
beforeEach(function() { | ||
beforeEach(function () { | ||
http = express(); | ||
@@ -27,3 +28,3 @@ other = proxyTarget(56001); | ||
afterEach(function() { | ||
afterEach(function () { | ||
other.close(); | ||
@@ -37,3 +38,3 @@ }); | ||
.expect(200) | ||
.end(function(err, res) { | ||
.end(function (err, res) { | ||
if (err) { return done(err); } | ||
@@ -45,4 +46,4 @@ assert(res.text === 'Success'); | ||
describe('when host is a String', function() { | ||
it('when passed as an option', function(done) { | ||
describe('when host is a String', function () { | ||
it('when passed as an option', function (done) { | ||
@@ -56,3 +57,3 @@ http.use(proxy('http://localhost', { | ||
it('when passed on the host string', function(done) { | ||
it('when passed on the host string', function (done) { | ||
@@ -66,9 +67,9 @@ http.use(proxy('http://localhost:56001')); | ||
describe('when host is a function', function() { | ||
describe('when host is a function', function () { | ||
it('and port is on the String returned', function(done) { | ||
it('and port is on the String returned', function (done) { | ||
http.use(proxy( | ||
function() { return 'http://localhost:56001'; } | ||
function () { return 'http://localhost:56001'; } | ||
)); | ||
@@ -79,6 +80,6 @@ | ||
it('and port passed as an option', function(done) { | ||
it('and port passed as an option', function (done) { | ||
http.use(proxy( | ||
function() { return 'http://localhost'; }, | ||
function () { return 'http://localhost'; }, | ||
{ port: 56001 } | ||
@@ -85,0 +86,0 @@ )); |
@@ -18,4 +18,4 @@ 'use strict'; | ||
pTarget.use(bodyParser.json()); | ||
pTarget.use(bodyParser.urlencoded({extended: true})); | ||
pTarget.use(function(req, res) { | ||
pTarget.use(bodyParser.urlencoded({ extended: true })); | ||
pTarget.use(function (req, res) { | ||
assert(req.body.name === 'tobi'); //, 'Assert that the value posted to the local server is passed to the proxy'); | ||
@@ -27,9 +27,10 @@ res.json(req.body); | ||
describe('when proxy request is a POST', function() { | ||
describe('when proxy request is a POST', function () { | ||
this.timeout(10000); | ||
var localServer, proxyServer; | ||
var localServer; | ||
var proxyServer; | ||
beforeEach(function() { | ||
beforeEach(function () { | ||
localServer = createLocalApplicationServer(); | ||
@@ -39,3 +40,3 @@ proxyServer = createProxyApplicationServer(); | ||
afterEach(function() { | ||
afterEach(function () { | ||
proxyServer.close(); | ||
@@ -49,14 +50,14 @@ }); | ||
testCases.forEach(function(test) { | ||
it('should deliver the post body when ' + test.name, function(done) { | ||
testCases.forEach(function (test) { | ||
it('should deliver the post body when ' + test.name, function (done) { | ||
localServer.use('/proxy', proxy('http://127.0.0.1:12345')); | ||
localServer.use(function(req, res) { res.sendStatus(200); }); | ||
localServer.use(function(err, req, res, next) { throw new Error(err, req, res, next); }); | ||
localServer.use(function (req, res) { res.sendStatus(200); }); | ||
localServer.use(function (err, req, res, next) { throw new Error(err, req, res, next); }); | ||
request(localServer) | ||
.post('/proxy') | ||
.send({ 'name': 'tobi' }) | ||
.send({ name: 'tobi' }) | ||
.set('Content-Type', test.encoding) | ||
.expect(function(res) { | ||
.expect(function (res) { | ||
assert(res.body.name === 'tobi'); | ||
@@ -63,0 +64,0 @@ }) |
@@ -12,3 +12,3 @@ 'use strict'; | ||
path: '/hostHdrTest', | ||
fn: function(req, res) { | ||
fn: function (req, res) { | ||
res.send(req.headers.host); | ||
@@ -18,3 +18,3 @@ } | ||
describe('preserves host header only when requested', function() { | ||
describe('preserves host header only when requested', function () { | ||
@@ -26,4 +26,4 @@ this.timeout(10000); | ||
describe('when preserveHostHdr is true', function() { | ||
before(function() { | ||
describe('when preserveHostHdr is true', function () { | ||
before(function () { | ||
proxyServer = proxyTarget(12346, 100, proxyRouteFn); | ||
@@ -36,11 +36,11 @@ app = express(); | ||
after(function() { | ||
after(function () { | ||
proxyServer.close(); | ||
}); | ||
it('host is passed forward', function(done) { | ||
it('host is passed forward', function (done) { | ||
request(app) | ||
.get('/hostHdrTest') | ||
.set('host', 'hamburger-helper') | ||
.end(function(err, res) { | ||
.end(function (err, res) { | ||
assert(res.text === 'hamburger-helper'); | ||
@@ -52,4 +52,4 @@ done(); | ||
describe('when preserveHostHdr is absent or false', function() { | ||
before(function() { | ||
describe('when preserveHostHdr is absent or false', function () { | ||
before(function () { | ||
proxyServer = proxyTarget(12346, 100, proxyRouteFn); | ||
@@ -60,11 +60,11 @@ app = express(); | ||
after(function() { | ||
after(function () { | ||
proxyServer.close(); | ||
}); | ||
it('host is not passed forward', function(done) { | ||
it('host is not passed forward', function (done) { | ||
request(app) | ||
.get('/hostHdrTest') | ||
.set('host', 'hamburger-helper') | ||
.end(function(err, res) { | ||
.end(function (err, res) { | ||
assert(res.text !== 'hamburger-helper'); | ||
@@ -71,0 +71,0 @@ done(); |
@@ -12,3 +12,3 @@ 'use strict'; | ||
describe('resolveProxyReqPath', function() { | ||
describe('resolveProxyReqPath', function () { | ||
var server; | ||
@@ -18,7 +18,7 @@ | ||
before(function() { | ||
before(function () { | ||
var handlers = [{ | ||
method: 'get', | ||
path: '/working', | ||
fn: function(req, res) { | ||
fn: function (req, res) { | ||
res.sendStatus(200); | ||
@@ -31,12 +31,12 @@ } | ||
after(function() { | ||
after(function () { | ||
server.close(); | ||
}); | ||
aliases.forEach(function(alias) { | ||
describe('when author uses option ' + alias, function() { | ||
it('the proxy request path is the result of the function', function(done) { | ||
aliases.forEach(function (alias) { | ||
describe('when author uses option ' + alias, function () { | ||
it('the proxy request path is the result of the function', function (done) { | ||
var app = express(); | ||
var opts = {}; | ||
opts[alias] = function() { return '/working'; }; | ||
opts[alias] = function () { return '/working'; }; | ||
app.use(proxy('localhost:12345', opts)); | ||
@@ -50,6 +50,6 @@ | ||
it('the ' + alias + ' method has access to request object', function(done) { | ||
it('the ' + alias + ' method has access to request object', function (done) { | ||
var app = express(); | ||
app.use(proxy('localhost:12345', { | ||
forwardPath: function(req) { | ||
forwardPath: function (req) { | ||
assert.ok(req instanceof http.IncomingMessage); | ||
@@ -56,0 +56,0 @@ return '/working'; |
@@ -7,8 +7,11 @@ 'use strict'; | ||
var expect = require('chai').expect; | ||
var express = require('express'); | ||
var request = require('supertest'); | ||
var proxy = require('../'); | ||
describe('resolveProxyReqPath', function() { | ||
describe('resolveProxyReqPath', function () { | ||
var container; | ||
beforeEach(function() { | ||
beforeEach(function () { | ||
container = new ScopeContainer(); | ||
@@ -28,3 +31,3 @@ }); | ||
resolverType: 'a syncronous function', | ||
resolverFn: function() { return 'the craziest thing'; }, | ||
resolverFn: function () { return 'the craziest thing'; }, | ||
data: [ | ||
@@ -37,4 +40,4 @@ { url: 'http://localhost:12345', parsed: 'the craziest thing' }, | ||
resolverType: 'a Promise', | ||
resolverFn: function() { | ||
return new Promise(function(resolve) { | ||
resolverFn: function () { | ||
return new Promise(function (resolve) { | ||
resolve('the craziest think'); | ||
@@ -50,9 +53,9 @@ }); | ||
describe('when proxyReqPathResolver', function() { | ||
describe('when proxyReqPathResolver', function () { | ||
tests.forEach(function(test) { | ||
describe('is ' + test.resolverType, function() { | ||
describe('it returns a promise which resolves a container with expected url', function() { | ||
test.data.forEach(function(data) { | ||
it(data.url, function(done) { | ||
tests.forEach(function (test) { | ||
describe('is ' + test.resolverType, function () { | ||
describe('it returns a promise which resolves a container with expected url', function () { | ||
test.data.forEach(function (data) { | ||
it(data.url, function (done) { | ||
container.user.req = { url: data.url }; | ||
@@ -64,3 +67,3 @@ container.options.proxyReqPathResolver = test.resolverFn; | ||
r.then(function(container) { | ||
r.then(function (container) { | ||
var response; | ||
@@ -85,2 +88,35 @@ try { | ||
}); | ||
describe('testing example code in docs', function () { | ||
it('works as advertised', function (done) { | ||
var proxyTarget = require('../test/support/proxyTarget'); | ||
var proxyRouteFn = [{ | ||
method: 'get', | ||
path: '/tent', | ||
fn: function (req, res) { | ||
res.send(req.url); | ||
} | ||
}]; | ||
var proxyServer = proxyTarget(12345, 100, proxyRouteFn); | ||
var app = express(); | ||
app.use(proxy('localhost:12345', { | ||
proxyReqPathResolver: function (req) { | ||
var parts = req.url.split('?'); | ||
var queryString = parts[1]; | ||
var updatedPath = parts[0].replace(/test/, 'tent'); | ||
return updatedPath + (queryString ? '?' + queryString : ''); | ||
} | ||
})); | ||
request(app) | ||
.get('/test?a=1&b=2&c=3') | ||
.end(function (err, res) { | ||
assert.equal(res.text, '/tent?a=1&b=2&c=3'); | ||
proxyServer.close(); | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var assert = require('assert'); | ||
@@ -6,4 +8,3 @@ var express = require('express'); | ||
describe('preserveReqSession', function() { | ||
'use strict'; | ||
describe('preserveReqSession', function () { | ||
@@ -14,3 +15,3 @@ this.timeout(10000); | ||
beforeEach(function() { | ||
beforeEach(function () { | ||
app = express(); | ||
@@ -20,5 +21,5 @@ app.use(proxy('httpbin.org')); | ||
it('preserveReqSession', function(done) { | ||
it('preserveReqSession', function (done) { | ||
var app = express(); | ||
app.use(function(req, res, next) { | ||
app.use(function (req, res, next) { | ||
req.session = 'hola'; | ||
@@ -29,3 +30,3 @@ next(); | ||
preserveReqSession: true, | ||
proxyReqOptDecorator: function(reqOpts) { | ||
proxyReqOptDecorator: function (reqOpts) { | ||
assert(reqOpts.session, 'hola'); | ||
@@ -38,3 +39,3 @@ return reqOpts; | ||
.get('/user-agent') | ||
.end(function(err) { | ||
.end(function (err) { | ||
if (err) { return done(err); } | ||
@@ -41,0 +42,0 @@ done(); |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var express = require('express'); | ||
@@ -6,5 +8,3 @@ var request = require('supertest'); | ||
describe('proxies status code', function() { | ||
'use strict'; | ||
describe('proxies status code', function () { | ||
var proxyServer = express(); | ||
@@ -17,12 +17,12 @@ var port = 21239; | ||
beforeEach(function() { | ||
beforeEach(function () { | ||
server = mockEndpoint.listen(21239); | ||
}); | ||
afterEach(function() { | ||
afterEach(function () { | ||
server.close(); | ||
}); | ||
[304, 404, 200, 401, 500].forEach(function(status) { | ||
it('on ' + status, function(done) { | ||
[304, 404, 200, 401, 500].forEach(function (status) { | ||
it('on ' + status, function (done) { | ||
request(proxyServer) | ||
@@ -29,0 +29,0 @@ .get('/status/' + status) |
@@ -13,8 +13,8 @@ 'use strict'; | ||
path: '/stream', | ||
fn: function(req, res) { | ||
fn: function (req, res) { | ||
res.write('0'); | ||
setTimeout(function() { res.write('1'); }, 100); | ||
setTimeout(function() { res.write('2'); }, 200); | ||
setTimeout(function() { res.write('3'); }, 300); | ||
setTimeout(function() { res.end(); }, 500); | ||
setTimeout(function () { res.write('1'); }, 100); | ||
setTimeout(function () { res.write('2'); }, 200); | ||
setTimeout(function () { res.write('3'); }, 300); | ||
setTimeout(function () { res.end(); }, 500); | ||
} | ||
@@ -27,10 +27,10 @@ }]; | ||
function simulateUserRequest() { | ||
return new Promise(function(resolve, reject) { | ||
var req = http.request({ hostname: 'localhost', port: 8308, path: '/stream' }, function(res) { | ||
return new Promise(function (resolve, reject) { | ||
var req = http.request({ hostname: 'localhost', port: 8308, path: '/stream' }, function (res) { | ||
var chunks = []; | ||
res.on('data', function(chunk) { chunks.push(chunk.toString()); }); | ||
res.on('end', function() { resolve(chunks); }); | ||
res.on('data', function (chunk) { chunks.push(chunk.toString()); }); | ||
res.on('end', function () { resolve(chunks); }); | ||
}); | ||
req.on('error', function(e) { | ||
req.on('error', function (e) { | ||
reject('problem with request:', e.message); | ||
@@ -49,12 +49,13 @@ }); | ||
describe('streams', function() { | ||
describe('streams / piped requests', function () { | ||
this.timeout(3000); | ||
var server, targetServer; | ||
var server; | ||
var targetServer; | ||
beforeEach(function() { | ||
beforeEach(function () { | ||
targetServer = chunkingProxyServer(); | ||
}); | ||
afterEach(function() { | ||
afterEach(function () { | ||
server.close(); | ||
@@ -64,30 +65,61 @@ targetServer.close(); | ||
describe('when streaming options are truthy', function() { | ||
it('chunks are received without any buffering, e.g. before request end', function(done) { | ||
describe('when streaming options are truthy', function () { | ||
var TEST_CASES = [{ | ||
name: 'vanilla, no options defined', | ||
options: {} | ||
}, { | ||
name: 'proxyReqOptDecorator is defined', | ||
options: { proxyReqOptDecorator: function (reqBuilder) { return reqBuilder; } } | ||
}, { | ||
//// Keep around this case for manually testing that this for sure fails for a few cycles. 2018 NMK | ||
//name: 'proxyReqOptDecorator never returns', | ||
//options: { proxyReqOptDecorator: function () { return new Promise(function () {}); } } | ||
//}, { | ||
server = startLocalServer(); | ||
name: 'proxyReqOptDecorator is a Promise', | ||
options: { proxyReqOptDecorator: function (reqBuilder) { return Promise.resolve(reqBuilder); } } | ||
}]; | ||
simulateUserRequest() | ||
.then(function(res) { | ||
// Assume that if I'm getting a chunked response, it will be an array of length > 1; | ||
assert(res instanceof Array && res.length === 4); | ||
done(); | ||
}) | ||
.catch(done); | ||
TEST_CASES.forEach(function (testCase) { | ||
describe(testCase.name, function () { | ||
it('chunks are received without any buffering, e.g. before request end', function (done) { | ||
server = startLocalServer(testCase.options); | ||
simulateUserRequest() | ||
.then(function (res) { | ||
// Assume that if I'm getting a chunked response, it will be an array of length > 1; | ||
assert(res instanceof Array, 'res is an Array'); | ||
assert.equal(res.length, 4); | ||
done(); | ||
}) | ||
.catch(done); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('when streaming options are falsey', function() { | ||
it('response arrives in one large chunk', function(done) { | ||
server = startLocalServer({ skipToNextHandlerFilter: function() { return false; } }); | ||
describe('when streaming options are falsey', function () { | ||
var TEST_CASES = [{ | ||
name: 'skipToNextHandler is defined', | ||
options: { skipToNextHandlerFilter: function () { return false; } } | ||
}]; | ||
simulateUserRequest() | ||
.then(function(res) { | ||
// Assume that if I'm getting a un-chunked response, it will be an array of length = 1; | ||
assert(res instanceof Array && res.length === 1); | ||
done(); | ||
}) | ||
.catch(done); | ||
TEST_CASES.forEach(function (testCase) { | ||
describe(testCase.name, function () { | ||
it('response arrives in one large chunk', function (done) { | ||
server = startLocalServer(testCase.options); | ||
simulateUserRequest() | ||
.then(function (res) { | ||
// Assume that if I'm getting a un-chunked response, it will be an array of length = 1; | ||
assert(res instanceof Array); | ||
assert.equal(res.length, 1); | ||
done(); | ||
}) | ||
.catch(done); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var express = require('express'); | ||
@@ -6,7 +8,8 @@ var request = require('supertest'); | ||
describe('honors timeout option', function() { | ||
'use strict'; | ||
describe('honors timeout option', function () { | ||
var other, http; | ||
beforeEach(function() { | ||
var other; | ||
var http; | ||
beforeEach(function () { | ||
http = express(); | ||
@@ -16,7 +19,7 @@ other = proxyTarget(56001, 1000, [{ | ||
path: '/', | ||
fn: function(req, res) { res.sendStatus(200); } | ||
fn: function (req, res) { res.sendStatus(200); } | ||
}]); | ||
}); | ||
afterEach(function() { | ||
afterEach(function () { | ||
other.close(); | ||
@@ -40,4 +43,4 @@ }); | ||
describe('when timeout option is set lower than server response time', function() { | ||
it('should fail with CONNECTION TIMEOUT', function(done) { | ||
describe('when timeout option is set lower than server response time', function () { | ||
it('should fail with CONNECTION TIMEOUT', function (done) { | ||
@@ -52,4 +55,4 @@ http.use(proxy('http://localhost:56001', { | ||
describe('when timeout option is set higher than server response time', function() { | ||
it('should succeed', function(done) { | ||
describe('when timeout option is set higher than server response time', function () { | ||
it('should succeed', function (done) { | ||
@@ -56,0 +59,0 @@ http.use(proxy('http://localhost:56001', { |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var assert = require('assert'); | ||
@@ -6,8 +8,7 @@ var express = require('express'); | ||
describe('url parsing', function() { | ||
'use strict'; | ||
describe('url parsing', function () { | ||
this.timeout(10000); | ||
it('can parse a url with a port', function(done) { | ||
it('can parse a url with a port', function (done) { | ||
var app = express(); | ||
@@ -17,3 +18,3 @@ app.use(proxy('http://httpbin.org:80')); | ||
.get('/') | ||
.end(function(err) { | ||
.end(function (err) { | ||
if (err) { return done(err); } | ||
@@ -25,3 +26,3 @@ assert(true); | ||
it('does not throw `Uncaught RangeError` if you have both a port and a trailing slash', function(done) { | ||
it('does not throw `Uncaught RangeError` if you have both a port and a trailing slash', function (done) { | ||
var app = express(); | ||
@@ -31,3 +32,3 @@ app.use(proxy('http://httpbin.org:80/')); | ||
.get('/') | ||
.end(function(err) { | ||
.end(function (err) { | ||
if (err) { return done(err); } | ||
@@ -41,2 +42,1 @@ assert(true); | ||
@@ -8,23 +8,25 @@ 'use strict'; | ||
describe('userResDecorator', function() { | ||
describe('userResDecorator', function () { | ||
describe('when handling a 304', function() { | ||
describe('when handling a 304', function () { | ||
this.timeout(10000); | ||
var app, slowTarget, serverReference; | ||
var app; | ||
var slowTarget; | ||
var serverReference; | ||
beforeEach(function() { | ||
beforeEach(function () { | ||
app = express(); | ||
slowTarget = express(); | ||
slowTarget.use(function(req, res) { res.sendStatus(304); }); | ||
slowTarget.use(function (req, res) { res.sendStatus(304); }); | ||
serverReference = slowTarget.listen(12345); | ||
}); | ||
afterEach(function() { | ||
afterEach(function () { | ||
serverReference.close(); | ||
}); | ||
it('skips any handling', function(done) { | ||
it('skips any handling', function (done) { | ||
app.use('/proxy', proxy('http://127.0.0.1:12345', { | ||
userResDecorator: function(/*res*/) { | ||
userResDecorator: function (/*res*/) { | ||
throw new Error('expected to never get here because this step should be skipped for 304'); | ||
@@ -41,6 +43,6 @@ } | ||
it('has access to original response', function(done) { | ||
it('has access to original response', function (done) { | ||
var app = express(); | ||
app.use(proxy('httpbin.org', { | ||
userResDecorator: function(proxyRes, proxyResData) { | ||
userResDecorator: function (proxyRes, proxyResData) { | ||
assert(proxyRes.connection); | ||
@@ -57,9 +59,9 @@ assert(proxyRes.socket); | ||
it('works with promises', function(done) { | ||
it('works with promises', function (done) { | ||
var app = express(); | ||
app.use(proxy('httpbin.org', { | ||
userResDecorator: function(proxyRes, proxyResData) { | ||
return new Promise(function(resolve) { | ||
userResDecorator: function (proxyRes, proxyResData) { | ||
return new Promise(function (resolve) { | ||
proxyResData.funkyMessage = 'oi io oo ii'; | ||
setTimeout(function() { | ||
setTimeout(function () { | ||
resolve(proxyResData); | ||
@@ -72,16 +74,16 @@ }, 200); | ||
request(app) | ||
.get('/ip') | ||
.end(function(err, res) { | ||
if (err) { return done(err); } | ||
.get('/ip') | ||
.end(function (err, res) { | ||
if (err) { return done(err); } | ||
assert(res.body.funkyMessage = 'oi io oo ii'); | ||
done(); | ||
}); | ||
assert(res.body.funkyMessage = 'oi io oo ii'); | ||
done(); | ||
}); | ||
}); | ||
it('can modify the response data', function(done) { | ||
it('can modify the response data', function (done) { | ||
var app = express(); | ||
app.use(proxy('httpbin.org', { | ||
userResDecorator: function(proxyRes, proxyResData) { | ||
userResDecorator: function (proxyRes, proxyResData) { | ||
proxyResData = JSON.parse(proxyResData.toString('utf8')); | ||
@@ -94,16 +96,16 @@ proxyResData.intercepted = true; | ||
request(app) | ||
.get('/ip') | ||
.end(function(err, res) { | ||
if (err) { return done(err); } | ||
.get('/ip') | ||
.end(function (err, res) { | ||
if (err) { return done(err); } | ||
assert(res.body.intercepted); | ||
done(); | ||
}); | ||
assert(res.body.intercepted); | ||
done(); | ||
}); | ||
}); | ||
it('can modify the response headers, [deviant case, supported by pass-by-reference atm]', function(done) { | ||
it('can modify the response headers, [deviant case, supported by pass-by-reference atm]', function (done) { | ||
var app = express(); | ||
app.use(proxy('httpbin.org', { | ||
userResDecorator: function(rsp, data, req, res) { | ||
userResDecorator: function (rsp, data, req, res) { | ||
res.set('x-wombat-alliance', 'mammels'); | ||
@@ -116,15 +118,15 @@ res.set('content-type', 'wiki/wiki'); | ||
request(app) | ||
.get('/ip') | ||
.end(function(err, res) { | ||
if (err) { return done(err); } | ||
assert(res.headers['content-type'] === 'wiki/wiki'); | ||
assert(res.headers['x-wombat-alliance'] === 'mammels'); | ||
done(); | ||
}); | ||
.get('/ip') | ||
.end(function (err, res) { | ||
if (err) { return done(err); } | ||
assert(res.headers['content-type'] === 'wiki/wiki'); | ||
assert(res.headers['x-wombat-alliance'] === 'mammels'); | ||
done(); | ||
}); | ||
}); | ||
it('can mutuate an html response', function(done) { | ||
it('can mutuate an html response', function (done) { | ||
var app = express(); | ||
app.use(proxy('httpbin.org', { | ||
userResDecorator: function(rsp, data) { | ||
userResDecorator: function (rsp, data) { | ||
data = data.toString().replace('Oh', '<strong>Hey</strong>'); | ||
@@ -137,15 +139,15 @@ assert(data !== ''); | ||
request(app) | ||
.get('/html') | ||
.end(function(err, res) { | ||
if (err) { return done(err); } | ||
assert(res.text.indexOf('<strong>Hey</strong>') > -1); | ||
done(); | ||
}); | ||
.get('/html') | ||
.end(function (err, res) { | ||
if (err) { return done(err); } | ||
assert(res.text.indexOf('<strong>Hey</strong>') > -1); | ||
done(); | ||
}); | ||
}); | ||
it('can change the location of a redirect', function(done) { | ||
it('can change the location of a redirect', function (done) { | ||
function redirectingServer(port, origin) { | ||
var app = express(); | ||
app.get('/', function(req, res) { | ||
app.get('/', function (req, res) { | ||
res.status(302); | ||
@@ -167,3 +169,3 @@ res.location(origin + '/proxied/redirect/url'); | ||
proxyApp.use(proxy(redirectingServerOrigin, { | ||
userResDecorator: function(rsp, data, req, res) { | ||
userResDecorator: function (rsp, data, req, res) { | ||
var proxyReturnedLocation = res._headers.location; | ||
@@ -176,10 +178,10 @@ res.location(proxyReturnedLocation.replace(redirectingServerPort, preferredPort)); | ||
request(proxyApp) | ||
.get('/') | ||
.expect(function(res) { | ||
res.headers.location.match(/localhost:3000/); | ||
}) | ||
.end(function() { | ||
server.close(); | ||
done(); | ||
}); | ||
.get('/') | ||
.expect(function (res) { | ||
res.headers.location.match(/localhost:3000/); | ||
}) | ||
.end(function () { | ||
server.close(); | ||
done(); | ||
}); | ||
}); | ||
@@ -189,3 +191,4 @@ }); | ||
describe('test userResDecorator on html response from github',function() { | ||
describe('test userResDecorator on html response from github', function () { | ||
/* | ||
@@ -199,8 +202,8 @@ Github provided a unique situation where the encoding was different than | ||
it('is able to read and manipulate the response', function(done) { | ||
it('is able to read and manipulate the response', function (done) { | ||
this.timeout(15000); // give it some extra time to get response | ||
var app = express(); | ||
app.use(proxy('https://github.com/villadora/express-http-proxy', { | ||
userResDecorator: function(targetResponse, data) { | ||
data = data.toString().replace('DOCTYPE','WINNING'); | ||
userResDecorator: function (targetResponse, data) { | ||
data = data.toString().replace('DOCTYPE', 'WINNING'); | ||
assert(data !== ''); | ||
@@ -212,8 +215,8 @@ return data; | ||
request(app) | ||
.get('/html') | ||
.end(function(err, res) { | ||
if (err) { return done(err); } | ||
assert(res.text.indexOf('WINNING') > -1); | ||
done(); | ||
}); | ||
.get('/html') | ||
.end(function (err, res) { | ||
if (err) { return done(err); } | ||
assert(res.text.indexOf('WINNING') > -1); | ||
done(); | ||
}); | ||
@@ -220,0 +223,0 @@ }); |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var assert = require('assert'); | ||
@@ -7,4 +9,3 @@ var express = require('express'); | ||
describe('http verbs', function() { | ||
'use strict'; | ||
describe('http verbs', function () { | ||
this.timeout(10000); | ||
@@ -14,3 +15,3 @@ | ||
beforeEach(function() { | ||
beforeEach(function () { | ||
app = express(); | ||
@@ -22,6 +23,6 @@ app.use(bodyParser.json()); | ||
it('test proxy get', function(done) { | ||
it('test proxy get', function (done) { | ||
request(app) | ||
.get('/get') | ||
.end(function(err, res) { | ||
.end(function (err, res) { | ||
if (err) { return done(err); } | ||
@@ -34,3 +35,3 @@ assert(/node-superagent/.test(res.body.headers['User-Agent'])); | ||
it('test proxy post', function(done) { | ||
it('test proxy post', function (done) { | ||
request(app) | ||
@@ -41,3 +42,3 @@ .post('/post') | ||
}) | ||
.end(function(err, res) { | ||
.end(function (err, res) { | ||
assert.equal(res.body.data, '{"mypost":"hello"}'); | ||
@@ -48,3 +49,3 @@ done(err); | ||
it('test proxy post by x-www-form-urlencoded', function(done) { | ||
it('test proxy post by x-www-form-urlencoded', function (done) { | ||
request(app) | ||
@@ -54,3 +55,3 @@ .post('/post') | ||
.send('mypost=hello') | ||
.end(function(err, res) { | ||
.end(function (err, res) { | ||
assert.equal(JSON.stringify(res.body.form), '{"mypost":"hello"}'); | ||
@@ -61,3 +62,3 @@ done(err); | ||
it('test proxy put', function(done) { | ||
it('test proxy put', function (done) { | ||
request(app) | ||
@@ -68,3 +69,3 @@ .put('/put') | ||
}) | ||
.end(function(err, res) { | ||
.end(function (err, res) { | ||
assert.equal(res.body.data, '{"mypost":"hello"}'); | ||
@@ -75,3 +76,3 @@ done(err); | ||
it('test proxy patch', function(done) { | ||
it('test proxy patch', function (done) { | ||
request(app) | ||
@@ -82,3 +83,3 @@ .patch('/patch') | ||
}) | ||
.end(function(err, res) { | ||
.end(function (err, res) { | ||
assert.equal(res.body.data, '{"mypost":"hello"}'); | ||
@@ -89,3 +90,3 @@ done(err); | ||
it('test proxy delete', function(done) { | ||
it('test proxy delete', function (done) { | ||
request(app) | ||
@@ -96,3 +97,3 @@ .del('/delete') | ||
}) | ||
.end(function(err, res) { | ||
.end(function (err, res) { | ||
assert.equal(res.body.data, '{"mypost":"hello"}'); | ||
@@ -99,0 +100,0 @@ done(err); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
102912
56
2441
570
9
1
1