Skip to content

Commit ab65fe2

Browse files
authored
Merge pull request spring-media#20 from napicella/master
normalize request path takes into account localhost
2 parents 25dc31f + a5fc327 commit ab65fe2

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

lib/proxyIntegration.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ function process(proxyIntegrationConfig, event, context) {
108108
}
109109

110110
function normalizeRequestPath(event) {
111+
if (isLocalExecution(event)) {
112+
return event.path;
113+
}
114+
111115
// ugly hack: if host is from API-Gateway 'Custom Domain Name Mapping', then event.path has the value '/basepath/resource-path/';
112116
// if host is from amazonaws.com, then event.path is just '/resource-path':
113117
const apiId = event.requestContext ? event.requestContext.apiId : null; // the apiId that is the first part of the amazonaws.com-host
@@ -189,4 +193,10 @@ function extractPathNames(pathExpression) {
189193
return pathNames && pathNames.length > 0 ? pathNames.slice(1) : null;
190194
}
191195

196+
function isLocalExecution(event) {
197+
return event.headers
198+
&& event.headers.Host
199+
&& event.headers.Host.startsWith('localhost');
200+
}
201+
192202
module.exports = process;

test/proxyIntegration.spec.js

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -221,28 +221,10 @@ describe('proxyIntegration.routeHandler', () => {
221221
}, context);
222222
});
223223
it('should not change path if not coming over custom domain name', () => {
224-
const actionSpy = jasmine.createSpy('action');
225-
const context = {};
226-
const event = {
227-
httpMethod: 'GET', path: "/123/456",
228-
headers: {Host: "blabla.execute-api.eu-central-1.amazonaws.com"},
229-
requestContext: {apiId: 'blabla'}
230-
};
231-
proxyIntegration({
232-
routes: [{
233-
method: 'GET',
234-
path: '/123/456',
235-
action: actionSpy
236-
}]
237-
}, event, context, () => {
238-
});
239-
expect(actionSpy).toHaveBeenCalledWith({
240-
httpMethod: 'GET',
241-
headers: jasmine.anything(),
242-
requestContext: jasmine.anything(),
243-
path: "/123/456",
244-
paths: {}
245-
}, context);
224+
assertPathIsUnchanged("blabla.execute-api.eu-central-1.amazonaws.com");
225+
});
226+
it('should not change path if coming over localhost', () => {
227+
assertPathIsUnchanged("localhost");
246228
});
247229
it('should return 400 for an invalid body', (done) => {
248230
proxyIntegration({routes: [{}]}, {httpMethod: 'GET', path: '/', body: '{keinJson'}).then(result => {
@@ -489,3 +471,28 @@ describe('proxyIntegration.routeHandler.returnvalues', () => {
489471
});
490472
});
491473

474+
function assertPathIsUnchanged(hostname) {
475+
const actionSpy = jasmine.createSpy('action');
476+
const context = {};
477+
const event = {
478+
httpMethod: 'GET', path: "/123/456",
479+
headers: {Host: hostname},
480+
requestContext: {apiId: 'blabla'}
481+
};
482+
proxyIntegration({
483+
routes: [{
484+
method: 'GET',
485+
path: '/123/456',
486+
action: actionSpy
487+
}]
488+
}, event, context, () => {
489+
});
490+
expect(actionSpy).toHaveBeenCalledWith({
491+
httpMethod: 'GET',
492+
headers: jasmine.anything(),
493+
requestContext: jasmine.anything(),
494+
path: "/123/456",
495+
paths: {}
496+
}, context);
497+
}
498+

0 commit comments

Comments
 (0)