diff --git a/README.md b/README.md index e14324f..445a6f0 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,17 @@ Provides a generic proxy to consume resources from other origins without causing URL-Format `/{http|https}/{host}/{path}` +### API + +#### proxy(options) + +##### options + +Type: `object` +Default: `{}` + +Options for [http-proxy](https://github.com/nodejitsu/node-http-proxy#options). + ## discovery Provides a resource discovery service (consumed in the [OpenUI5 testsuite](https://github.com/SAP/openui5/tree/master/src/sap.ui.core/test/testsuite)). diff --git a/lib/proxy.js b/lib/proxy.js index 737f03b..aef277c 100644 --- a/lib/proxy.js +++ b/lib/proxy.js @@ -65,10 +65,10 @@ function getProxyUri(uri) { return null; } -module.exports = function() { +module.exports = function(mOptions) { var urlPattern = /^\/(http|https)\/(.*)/; - var proxy = httpProxy.createProxyServer({}); + var proxy = httpProxy.createProxyServer(mOptions || {}); return function(req, res, next) { diff --git a/test/proxy_test.js b/test/proxy_test.js index 7f8a9eb..e3ebb0e 100644 --- a/test/proxy_test.js +++ b/test/proxy_test.js @@ -31,6 +31,10 @@ describe('proxy middleware should proxy generic requests', function () { iActualStatusCode; oAppToBeProxied.use(function (oRequest, oResponse) { + // no x-forwareded headers expected (wasn't configured in proxy) + assert.equal(oRequest.headers['x-forwarded-for'], undefined); + assert.equal(oRequest.headers['x-forwarded-port'], undefined); + assert.equal(oRequest.headers['x-forwarded-proto'], undefined); sActualPath = oRequest.url; oResponse.end(sExpectedResponse); }); @@ -43,6 +47,50 @@ describe('proxy middleware should proxy generic requests', function () { var oProxyServer = http.createServer(oProxyApp); oProxyServer.listen(9000); + http.get('http://localhost:9000/http/localhost:8080' + sExpectedPath, function (oResponse) { + oResponse.on('data', function(oData) { + sActualResponse += oData; + }); + iActualStatusCode = oResponse.statusCode; + oResponse.on('end', function () { + assert.equal(sActualPath, sExpectedPath); + assert.equal(sActualResponse, sExpectedResponse); + assert.equal(iActualStatusCode, iExpectedStatusCode); + + oServerToBeProxied.close(); + oProxyServer.close(); + done(); + }); + }); + }); + it('should proxy by respecting custom options', function (done) { + var sExpectedResponse = 'All ok!', + sExpectedPath = '/foo/bar', + iExpectedStatusCode = 200, + oAppToBeProxied = connect(), + sActualResponse = '', + sActualPath, + iActualStatusCode; + + oAppToBeProxied.use(function (oRequest, oResponse) { + // x-forwareded headers are expected (see xfwd option in proxy config) + assert.equal(oRequest.headers['x-forwarded-for'], '127.0.0.1'); + assert.equal(oRequest.headers['x-forwarded-port'], '8080'); + assert.equal(oRequest.headers['x-forwarded-proto'], 'http'); + sActualPath = oRequest.url; + oResponse.end(sExpectedResponse); + }); + + var oServerToBeProxied = http.createServer(oAppToBeProxied); + oServerToBeProxied.listen(8080); + + var oProxyApp = connect(); + oProxyApp.use(proxy({ + xfwd: true + })); + var oProxyServer = http.createServer(oProxyApp); + oProxyServer.listen(9000); + http.get('http://localhost:9000/http/localhost:8080' + sExpectedPath, function (oResponse) { oResponse.on('data', function(oData) { sActualResponse += oData;