Skip to content

Commit

Permalink
fix(lambda-edge): update schema and refactor (#1230)
Browse files Browse the repository at this point in the history
* remove array check

* update if

* fix test

* fix origin infomation
  • Loading branch information
watany-dev authored Jul 10, 2023
1 parent 096e3b7 commit f37de25
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
30 changes: 25 additions & 5 deletions runtime_tests/lambda-edge/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 () => {
Expand Down
10 changes: 5 additions & 5 deletions src/adapter/lambda-edge/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

/**
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit f37de25

Please sign in to comment.