diff --git a/runtime_tests/lambda-edge/index.test.ts b/runtime_tests/lambda-edge/index.test.ts index 423dd6d49..6a089609e 100644 --- a/runtime_tests/lambda-edge/index.test.ts +++ b/runtime_tests/lambda-edge/index.test.ts @@ -71,7 +71,11 @@ describe('Lambda@Edge Adapter for Hono', () => { const response = await handler(event) expect(response.status).toBe('200') expect(response.body).toBe('Hello Lambda!') - expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/) + if(response.headers && response.headers['content-type']){ + expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/) + } else { + throw new Error("'content-type' header is missing in the response"); + } }) it('Should handle a GET request and return a 200 response (Lambda@Edge origin request)', async () => { @@ -142,7 +146,11 @@ describe('Lambda@Edge Adapter for Hono', () => { const response = await handler(event) expect(response.status).toBe('200') expect(response.body).toBe('Hello Lambda!') - expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/) + if(response.headers && response.headers['content-type']){ + expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/) + } else { + throw new Error("'content-type' header is missing in the response"); + } }) it('Should handle a GET request and return a 200 response (Lambda@Edge viewer response)', async () => { @@ -261,7 +269,11 @@ describe('Lambda@Edge Adapter for Hono', () => { const response = await handler(event) expect(response.status).toBe('200') expect(response.body).toBe('Hello Lambda!') - expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/) + if(response.headers && response.headers['content-type']){ + expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/) + } else { + throw new Error("'content-type' header is missing in the response"); + } }) it('Should handle a GET request and return a 200 response (Lambda@Edge origin response)', async () => { @@ -398,7 +410,11 @@ describe('Lambda@Edge Adapter for Hono', () => { const response = await handler(event) expect(response.status).toBe('200') expect(response.body).toBe('Hello Lambda!') - expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/) + if(response.headers && response.headers['content-type']){ + expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/) + } else { + throw new Error("'content-type' header is missing in the response"); + } }) it('Should handle a GET request and return a 200 response with binary', async () => { @@ -435,7 +451,11 @@ describe('Lambda@Edge Adapter for Hono', () => { expect(response.status).toBe('200') expect(response.body).toBe('RmFrZSBJbWFnZQ==') // base64 encoded fake image - expect(response.headers['content-type'][0].value).toMatch(/^image\/png/) + if(response.headers && response.headers['content-type']){ + expect(response.headers['content-type'][0].value).toMatch(/^image\/png/) + } else { + throw new Error("'content-type' header is missing in the response"); + } }) it('Should handle a GET request and return a 404 response', async () => { diff --git a/src/adapter/lambda-edge/handler.ts b/src/adapter/lambda-edge/handler.ts index 71c1bb919..505bb3c85 100644 --- a/src/adapter/lambda-edge/handler.ts +++ b/src/adapter/lambda-edge/handler.ts @@ -63,16 +63,18 @@ interface CloudFrontEdgeEvent { Records: CloudFrontEvent[]; } +// https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-generating-http-responses-in-requests.html#lambda-generating-http-responses-programming-model interface CloudFrontResult { status: string; statusDescription?: string; - headers: { + headers?: { [header: string]: { - key?: string; + key: string; value: string; }[]; }; body?: string; + bodyEncoding?: 'text' | 'base64'; } /** @@ -117,9 +119,7 @@ const createRequest = ( const headers = new Headers() for (const [k, v] of Object.entries(event.Records[0].cf.request.headers)) { - if (Array.isArray(v)) { - v.forEach(header => headers.set(k, header.value)) - } + v.forEach(header => headers.set(k, header.value)) } const method = event.Records[0].cf.request.method const requestInit: RequestInit = {