Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(apig-v1-adapter): lowercase headers #259

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading