Skip to content

Commit

Permalink
Merge pull request #259 from tcandens/main
Browse files Browse the repository at this point in the history
fix(apig-v1-adapter): lowercase headers
  • Loading branch information
H4ad authored Sep 6, 2024
2 parents df02cf6 + 4fbb588 commit 0e161a1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/adapters/aws/api-gateway-v1.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
GetResponseAdapterProps,
OnErrorProps,
} from '../../contracts';
import { keysToLowercase } from '../../core';
import {
type StripBasePathFn,
buildStripBasePath,
Expand Down Expand Up @@ -42,6 +43,13 @@ export interface ApiGatewayV1Options {
* @defaultValue true
*/
throwOnChunkedTransferEncoding?: boolean;

/**
* Emulates the behavior of Node.js `http` module by ensuring all request headers are lowercase.
*
* @defaultValue false
*/
lowercaseRequestHeaders?: boolean;
}

/**
Expand Down Expand Up @@ -122,7 +130,9 @@ export class ApiGatewayV1Adapter
*/
public getRequest(event: APIGatewayProxyEvent): AdapterRequest {
const method = event.httpMethod;
const headers = { ...event.headers };
const headers = this.options?.lowercaseRequestHeaders
? keysToLowercase(event.headers)
: { ...event.headers };

for (const multiValueHeaderKey of Object.keys(
event.multiValueHeaders || {},
Expand Down
9 changes: 9 additions & 0 deletions src/core/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,12 @@ export function parseHeaders(

return result;
}

export function keysToLowercase<T extends Record<string, unknown>>(
obj: T,
): { [K in keyof T as Lowercase<string & K>]: T[K] } {
const result: any = {};
for (const [k, v] of Object.entries(obj)) result[k.toLowerCase()] = v;

return result as { [K in keyof T as Lowercase<string & K>]: T[K] };
}
16 changes: 16 additions & 0 deletions test/adapters/aws/api-gateway-v1.adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ describe(ApiGatewayV1Adapter.name, () => {
expect(result).toHaveProperty('path', resultPath);
});

it('should return lowercase request headers if option `lowercaseRequestHeaders` is `true`', () => {
const method = 'GET';
const path = '/events';

adapter = new ApiGatewayV1Adapter({ lowercaseRequestHeaders: true });
const event = createApiGatewayV1(method, path);
event.headers['Cookie'] = 'test=test;';

const { headers } = adapter.getRequest(event);

expect(headers).not.toHaveProperty('Cookie');
expect(headers).toHaveProperty('cookie');
expect(headers).not.toHaveProperty('Accept');
expect(headers).toHaveProperty('accept');
});

it('should return the correct mapping for the request when it has no body', () => {
const method = 'GET';
const path = '/users';
Expand Down
9 changes: 9 additions & 0 deletions www/docs/main/adapters/aws/api-gateway-v1.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ When you configure your API with some `basePath` like `/prod`, you should either

:::

You can also ensure request headers are lowercase like the default behavior of Node.js `http` module by setting the `lowercaseRequestHeaders` option to `true`.

:::caution

ApiGateway may already be ensuring your headers are lowercase but it may be worth confirming in your own system as many libraries in the Node.js ecosystem will assume this lowercasing.

:::


## Usage

To add support to AWS API Gateway V1 you do the following:
Expand Down

0 comments on commit 0e161a1

Please sign in to comment.