Skip to content

Commit cf94929

Browse files
committed
allow x-http-method-overrid header to be passed in from the client request
1 parent 2d88347 commit cf94929

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

server/middleware/apiProxy.js

+9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ function apiProxy(dataAdapter) {
2424
'x-forwarded-for': apiProxy.getXForwardedForHeader(req.headers, req.ip)
2525
};
2626

27+
api.headers = apiProxy.setXHTTPMethodOverride(req.headers, api.headers);
28+
2729
dataAdapter.request(req, api, {
2830
convertErrorCode: false
2931
}, function(err, response, body) {
@@ -65,3 +67,10 @@ apiProxy.getXForwardedForHeader = function (headers, clientIp) {
6567

6668
return newHeaderValue;
6769
};
70+
71+
apiProxy.setXHTTPMethodOverride = function (requestHeaders, apiHeaders) {
72+
if (requestHeaders['x-http-method-override']) {
73+
apiHeaders['x-http-method-override'] = requestHeaders['x-http-method-override'];
74+
}
75+
return apiHeaders;
76+
};

test/server/middleware/apiProxy.test.js

+27
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,33 @@ describe('apiProxy', function() {
8585
incomingHeaders['x-forwarded-for']);
8686
});
8787

88+
it('should add an x-http-method-override header to the request if there is one', function () {
89+
var incomingHeaders = { 'x-http-method-override': 'PUT' },
90+
outgoingHeaders;
91+
92+
requestFromClient.headers = incomingHeaders;
93+
94+
proxy(requestFromClient, responseToClient);
95+
96+
requestToApi.should.have.been.calledOnce;
97+
outgoingHeaders = requestToApi.firstCall.args[1].headers;
98+
outgoingHeaders['x-http-method-override'].should.eq(incomingHeaders['x-http-method-override']);
99+
});
100+
101+
it('should not add an x-http-method-override header to the request if there is not one', function () {
102+
var incomingHeaders = {},
103+
outgoingHeaders;
104+
105+
requestFromClient.headers = incomingHeaders;
106+
107+
proxy(requestFromClient, responseToClient);
108+
109+
requestToApi.should.have.been.calledOnce;
110+
outgoingHeaders = requestToApi.firstCall.args[1].headers;
111+
112+
expect(outgoingHeaders['x-http-method-override']).to.be.undefined;
113+
});
114+
88115

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

0 commit comments

Comments
 (0)