Skip to content

Commit

Permalink
Fix proxy parameter population.
Browse files Browse the repository at this point in the history
  • Loading branch information
wparad committed Dec 20, 2023
1 parent 757b9db commit a7b9517
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node-terminal",
"request": "launch",
"name": "Debug tests",
"skipFiles": [
"<node_internals>/**"
],
"command": "npm test"
}
]
}
16 changes: 9 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,20 @@ class ApiFactory {
const map = apiFactory.pathResolver.resolvePath(apiFactory.ProxyRoutes, method, event.path);
const definedMethods = map && map.methods;

// default to defined path when proxy is not specified.
if (routeKey.lastIndexOf(proxyPath) === -1 && routeKey !== '$default') {
if (map) {
// if it is a proxy path then then look up the proxied value.
definedRoute = map.value;
delete event.pathParameters.proxy;
event.pathParameters = Object.assign({}, map.tokens, event.pathParameters);
}

if (!definedRoute && routeKey.lastIndexOf(proxyPath) === -1 && routeKey !== '$default') {
// default to defined path when proxy is not specified.
if (apiFactory.Routes[routeKey] && apiFactory.Routes[routeKey][method]) {
definedRoute = apiFactory.Routes[routeKey][method];
} else if (apiFactory.Routes[routeKey] && apiFactory.Routes[routeKey].ANY) {
definedRoute = apiFactory.Routes[routeKey].ANY;
}
} else if (map) {
// if it is a proxy path then then look up the proxied value.
definedRoute = map.value;
delete event.pathParameters.proxy;
event.pathParameters = Object.assign({}, map.tokens, event.pathParameters);
}

// either it is proxied and not defined or not defined, either way go to the first proxy route available in the hierarchy.
Expand Down
4 changes: 2 additions & 2 deletions tests/fullApi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ describe('index.js', () => {
expect(spyMap['ANY-PROXY'].calledOnce).to.be.true;
expect(spyMap['ANY-PROXY'].getCall(0).args[0].httpMethod).to.eql('GET');
expect(spyMap['ANY-PROXY'].getCall(0).args[0].path).to.eql('/test');
expect(spyMap['ANY-PROXY'].getCall(0).args[0].pathParameters).to.eql({ });
expect(spyMap['ANY-PROXY'].getCall(0).args[0].pathParameters).to.eql({ proxy: 'test' });
});
});

Expand Down Expand Up @@ -401,7 +401,7 @@ describe('index.js', () => {
expect(spyMap['ANY-PROXY'].calledOnce).to.be.true;
expect(spyMap['ANY-PROXY'].getCall(0).args[0].httpMethod).to.eql('GET');
expect(spyMap['ANY-PROXY'].getCall(0).args[0].path).to.eql('/test');
expect(spyMap['ANY-PROXY'].getCall(0).args[0].pathParameters).to.eql({ });
expect(spyMap['ANY-PROXY'].getCall(0).args[0].pathParameters).to.eql({ proxy: 'test' });
});

it('Validate ANY works when the wrong method is picked', async () => {
Expand Down
10 changes: 5 additions & 5 deletions tests/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require('error-object-polyfill');
const { describe, it, beforeEach, afterEach } = require('mocha');
const chai = require('chai');
const { use: chaiUse, expect } = require('chai');
const { assert } = require('chai');
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
Expand All @@ -9,7 +9,7 @@ const PathResolver = require('../src/pathResolver');
const Api = require('../index');
const Response = require('../src/response');

chai.use(sinonChai);
chaiUse(sinonChai);

let sandbox;
beforeEach(() => { sandbox = sinon.createSandbox(); });
Expand Down Expand Up @@ -455,9 +455,9 @@ describe('index.js', () => {
let expectedResult = { value: 5 };
let api = new Api(null, noopFunction);
api.get('/test', request => {
assert.equal(request.pathParameters, expectedPathParameters);
assert.equal(request.stageVariables, expectedStageVariables);
assert.equal(request.queryStringParameters, expectedQueryStringParameters);
expect(request.pathParameters).to.eql(expectedPathParameters);
expect(request.stageVariables).to.eql(expectedStageVariables);
expect(request.queryStringParameters).to.eql(expectedQueryStringParameters);
return Promise.resolve({ body: expectedResult, statusCode: 201 });
});

Expand Down

0 comments on commit a7b9517

Please sign in to comment.