Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
proxy middleware: allow passing options
Browse files Browse the repository at this point in the history
  • Loading branch information
matz3 committed Jan 9, 2015
1 parent 9f1bc3c commit 6ab9920
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
Expand Down
4 changes: 2 additions & 2 deletions lib/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
48 changes: 48 additions & 0 deletions test/proxy_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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;
Expand Down

0 comments on commit 6ab9920

Please sign in to comment.