Skip to content
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
1 change: 1 addition & 0 deletions packages/open-next/src/converters/aws-apigw-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ async function convertToApiGatewayProxyResultV2(
): Promise<APIGatewayProxyResultV2> {
const headers: Record<string, string> = {};
Object.entries(result.headers)
.map(([key, value]) => [key.toLowerCase(), value] as const)
.filter(
([key]) =>
!CloudFrontBlacklistedHeaders.some((header) =>
Expand Down
4 changes: 2 additions & 2 deletions packages/open-next/src/http/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ export class IncomingMessage extends http.IncomingMessage {
}: {
method: string;
url: string;
headers: Record<string, string>;
headers: Record<string, string | string[]>;
body?: Buffer;
remoteAddress: string;
remoteAddress?: string;
}) {
super({
encrypted: true,
Expand Down
264 changes: 264 additions & 0 deletions packages/tests-unit/tests/converters/aws-apigw-v1.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
/* eslint-disable sonarjs/no-duplicate-string */
import converter from "@opennextjs/aws/converters/aws-apigw-v1.js";
import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
import { Readable } from "stream";

describe("convertTo", () => {
describe("AWS API Gateway v2 Result", () => {
it("Should parse the headers", async () => {
const response = (await converter.convertTo({
body: Readable.toWeb(Readable.from(Buffer.from(""))),
headers: {
"content-type": "application/json",
test: "test",
},
isBase64Encoded: false,
statusCode: 200,
})) as APIGatewayProxyResult;

expect(response.headers).toStrictEqual({
"content-type": "application/json",
test: "test",
});
});

it("Should parse the headers with arrays", async () => {
const response = (await converter.convertTo({
body: Readable.toWeb(Readable.from(Buffer.from(""))),
headers: {
test: ["test1", "test2"],
},
isBase64Encoded: false,
statusCode: 200,
})) as APIGatewayProxyResult;

expect(response.multiValueHeaders).toStrictEqual({
test: ["test1", "test2"],
});
});

it("Should parse single and array headers", async () => {
const response = (await converter.convertTo({
body: Readable.toWeb(Readable.from(Buffer.from(""))),
headers: {
single: "test",
multi: ["test1", "test2"],
},
isBase64Encoded: false,
statusCode: 200,
})) as APIGatewayProxyResult;

expect(response.headers).toStrictEqual({
single: "test",
});
expect(response.multiValueHeaders).toStrictEqual({
multi: ["test1", "test2"],
});
});
});
});

describe("convertFrom", () => {
it("Should convert a simple APIGatewayProxyEvent", async () => {
const event: APIGatewayProxyEvent = {
body: JSON.stringify({ message: "Hello, world!" }),
headers: {
"content-type": "application/json",
},
multiValueHeaders: {},
httpMethod: "POST",
isBase64Encoded: false,
path: "/",
pathParameters: null,
queryStringParameters: null,
multiValueQueryStringParameters: null,
stageVariables: null,
requestContext: {
identity: {
sourceIp: "::1",
},
} as any,
resource: "",
};

const response = await converter.convertFrom(event);

expect(response).toEqual({
type: "core",
method: "POST",
rawPath: "/",
url: "/",
body: Buffer.from('{"message":"Hello, world!"}'),
headers: {
"content-type": "application/json",
},
remoteAddress: "::1",
query: {},
cookies: {},
});
});

it("Should handle multiValueHeaders", async () => {
const event: APIGatewayProxyEvent = {
body: JSON.stringify({ message: "Hello, world!" }),
headers: {},
multiValueHeaders: {
test: ["test1", "test2"],
},
httpMethod: "POST",
isBase64Encoded: false,
path: "/",
pathParameters: null,
queryStringParameters: null,
multiValueQueryStringParameters: null,
stageVariables: null,
requestContext: {
identity: {
sourceIp: "::1",
},
} as any,
resource: "",
};

const response = await converter.convertFrom(event);

expect(response).toEqual({
type: "core",
method: "POST",
rawPath: "/",
url: "/",
body: Buffer.from('{"message":"Hello, world!"}'),
headers: {
test: "test1,test2",
},
remoteAddress: "::1",
query: {},
cookies: {},
});
});

it("Should handle queryStringParameters and multiValueQueryStringParameters", async () => {
const event: APIGatewayProxyEvent = {
body: JSON.stringify({ message: "Hello, world!" }),
headers: {},
multiValueHeaders: {},
httpMethod: "POST",
isBase64Encoded: false,
path: "/",
pathParameters: null,
queryStringParameters: {
test: "test",
},
multiValueQueryStringParameters: {
test: ["test"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when the same key (test) has different values? Do we want to merge the two? Is that even possible?
eg:

      queryStringParameters: {
        test: "test",
      },
      multiValueQueryStringParameters: {
        test: ["test-multi"],
      }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it shouldn't be possible, hopefully aws has some kind of validation on this to avoid this kind of case.

},
stageVariables: null,
requestContext: {
identity: {
sourceIp: "::1",
},
} as any,
resource: "",
};

const response = await converter.convertFrom(event);

expect(response).toEqual({
type: "core",
method: "POST",
rawPath: "/",
url: "/?test=test",
body: Buffer.from('{"message":"Hello, world!"}'),
headers: {},
remoteAddress: "::1",
query: {
test: ["test"],
},
cookies: {},
});
});

it("Should handle cookies", async () => {
const event: APIGatewayProxyEvent = {
body: JSON.stringify({ message: "Hello, world!" }),
headers: {
"content-type": "application/json",
},
multiValueHeaders: {
cookie: ["test1=1", "test2=2"],
},
httpMethod: "POST",
isBase64Encoded: false,
path: "/",
pathParameters: null,
queryStringParameters: null,
multiValueQueryStringParameters: null,
stageVariables: null,
requestContext: {
identity: {
sourceIp: "::1",
},
} as any,
resource: "",
};

const response = await converter.convertFrom(event);

expect(response).toEqual({
type: "core",
method: "POST",
rawPath: "/",
url: "/",
body: Buffer.from('{"message":"Hello, world!"}'),
headers: {
"content-type": "application/json",
cookie: "test1=1,test2=2",
},
remoteAddress: "::1",
query: {},
cookies: {
test1: "1",
test2: "2",
},
});
});

it("Should handle base64 encoded body", async () => {
const event: APIGatewayProxyEvent = {
body: Buffer.from("Hello, world!").toString("base64"),
headers: {
"content-type": "application/json",
},
multiValueHeaders: {},
httpMethod: "GET",
isBase64Encoded: true,
path: "/",
pathParameters: null,
queryStringParameters: null,
multiValueQueryStringParameters: null,
stageVariables: null,
requestContext: {
identity: {
sourceIp: "::1",
},
} as any,
resource: "",
};

const response = await converter.convertFrom(event);

expect(response).toEqual({
type: "core",
method: "GET",
rawPath: "/",
url: "/",
body: Buffer.from("Hello, world!"),
headers: {
"content-type": "application/json",
},
remoteAddress: "::1",
query: {},
cookies: {},
});
});
});
Loading
Loading