Skip to content

Commit

Permalink
fix(api-gateway-v1): probably missing query string value when multiple
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad committed Jun 7, 2023
1 parent 70f7020 commit 78b9f18
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
17 changes: 16 additions & 1 deletion src/adapters/aws/api-gateway-v1.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,22 @@ export class ApiGatewayV1Adapter
*/
protected getPathFromEvent(event: APIGatewayProxyEvent): string {
const path = this.stripPathFn(event.path);
const queryParams = event.queryStringParameters || {};
const queryParams = event.multiValueQueryStringParameters || {};

if (event.queryStringParameters) {
for (const queryStringKey of Object.keys(event.queryStringParameters)) {
const queryStringValue = event.queryStringParameters[queryStringKey];

if (queryStringValue === undefined) continue;

if (!Array.isArray(queryParams[queryStringKey]))
queryParams[queryStringKey] = [];

if (queryParams[queryStringKey]!.includes(queryStringValue)) continue;

queryParams[queryStringKey]!.push(queryStringValue);
}
}

return getPathWithQueryStringParams(path, queryParams);
}
Expand Down
15 changes: 12 additions & 3 deletions test/adapters/aws/api-gateway-v1.adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,23 @@ describe(ApiGatewayV1Adapter.name, () => {
const body = { name: 'H4ad Event' };

const event = createApiGatewayV1(method, path, body);
event.queryStringParameters = {
potato: 'v1',
nanana: 'oh nanana',
unkown: 'oi',
ignore: undefined,
};
event.multiValueQueryStringParameters = {
potato: ['v1', 'v2'],
nanana: ['oh nanana'],
};
const result = adapter.getRequest(event);

const remoteAddress = event.requestContext.identity.sourceIp;

expect(result).toHaveProperty('method', method);
expect(result).toHaveProperty('headers');
expect(result.headers).not.toHaveProperty('Accept');
expect(result.headers).toHaveProperty('accept');
expect(result.headers).toHaveProperty('Accept');

expect(result).toHaveProperty('body');
expect(result.body).toBeInstanceOf(Buffer);
Expand All @@ -61,7 +70,7 @@ describe(ApiGatewayV1Adapter.name, () => {

const resultPath = getPathWithQueryStringParams(
path,
event.queryStringParameters,
event.multiValueQueryStringParameters,
);
expect(result).toHaveProperty('path', resultPath);
});
Expand Down
4 changes: 2 additions & 2 deletions test/adapters/aws/utils/api-gateway-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function createApiGatewayV1(
httpMethod,
headers: {
Accept: '*/*',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Encoding': 'gzip',
'Accept-Language': 'en-US,en;q=0.9',
'cache-control': 'no-cache',
'CloudFront-Forwarded-Proto': 'https',
Expand All @@ -43,7 +43,7 @@ export function createApiGatewayV1(
},
multiValueHeaders: {
Accept: ['*/*'],
'Accept-Encoding': ['gzip, deflate, br'],
'Accept-Encoding': ['gzip', 'deflate', 'br'],
'Accept-Language': ['en-US,en;q=0.9'],
'cache-control': ['no-cache'],
'CloudFront-Forwarded-Proto': ['https'],
Expand Down

0 comments on commit 78b9f18

Please sign in to comment.