Skip to content

Commit

Permalink
Merge pull request #478 from ingiulio/allowXHTTPMethodOverrideHeader
Browse files Browse the repository at this point in the history
allow x-http-method-override header to be passed in from the client request
  • Loading branch information
saponifi3d committed Jun 12, 2015
2 parents b105588 + cf94929 commit 2b0cccc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
9 changes: 9 additions & 0 deletions server/middleware/apiProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ function apiProxy(dataAdapter) {
'x-forwarded-for': apiProxy.getXForwardedForHeader(req.headers, req.ip)
};

api.headers = apiProxy.setXHTTPMethodOverride(req.headers, api.headers);

dataAdapter.request(req, api, {
convertErrorCode: false
}, function(err, response, body) {
Expand Down Expand Up @@ -65,3 +67,10 @@ apiProxy.getXForwardedForHeader = function (headers, clientIp) {

return newHeaderValue;
};

apiProxy.setXHTTPMethodOverride = function (requestHeaders, apiHeaders) {
if (requestHeaders['x-http-method-override']) {
apiHeaders['x-http-method-override'] = requestHeaders['x-http-method-override'];
}
return apiHeaders;
};
27 changes: 27 additions & 0 deletions test/server/middleware/apiProxy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,33 @@ describe('apiProxy', function() {
incomingHeaders['x-forwarded-for']);
});

it('should add an x-http-method-override header to the request if there is one', function () {
var incomingHeaders = { 'x-http-method-override': 'PUT' },
outgoingHeaders;

requestFromClient.headers = incomingHeaders;

proxy(requestFromClient, responseToClient);

requestToApi.should.have.been.calledOnce;
outgoingHeaders = requestToApi.firstCall.args[1].headers;
outgoingHeaders['x-http-method-override'].should.eq(incomingHeaders['x-http-method-override']);
});

it('should not add an x-http-method-override header to the request if there is not one', function () {
var incomingHeaders = {},
outgoingHeaders;

requestFromClient.headers = incomingHeaders;

proxy(requestFromClient, responseToClient);

requestToApi.should.have.been.calledOnce;
outgoingHeaders = requestToApi.firstCall.args[1].headers;

expect(outgoingHeaders['x-http-method-override']).to.be.undefined;
});


it('should not pass through the host header', function () {
proxy(requestFromClient, responseToClient);
Expand Down

0 comments on commit 2b0cccc

Please sign in to comment.