fix(lambda-edge): update schema and refactor (#1230)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* remove array check * update if * fix test * fix origin infomation
authored by watany · committed by GitHub · parent 096e3b7 · Browse files · 2 files·+30 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+30 added−10 removed
- tests:+25 added−5 removed
- other:+5 added−5 removed
Provenance
- Commit
f37de25- Tree
1e1a267- Author
watany <76135106+watany-dev@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
096e3b7
2 files changed+30 added−10 removed
modified:runtime_tests/lambda-edge/index.test.ts+25 added-5 removed
69 unmodified lines70 }71 const response = await handler(event)72 expect(response.status).toBe('200')73 expect(response.body).toBe('Hello Lambda!')74+ if(response.headers && response.headers['content-type']){74- expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/)75+ expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/)76+ } else {77+ throw new Error("'content-type' header is missing in the response");78+ }79 })80 81 it('Should handle a GET request and return a 200 response (Lambda@Edge origin request)', async () => {82 const event = {62 unmodified lines145 }146 const response = await handler(event)147 expect(response.status).toBe('200')148 expect(response.body).toBe('Hello Lambda!')149+ if(response.headers && response.headers['content-type']){145- expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/)150+ expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/)151+ } else {152+ throw new Error("'content-type' header is missing in the response");153+ }154 })155 156 it('Should handle a GET request and return a 200 response (Lambda@Edge viewer response)', async () => {157 const event = {110 unmodified lines268 }269 const response = await handler(event)270 expect(response.status).toBe('200')271 expect(response.body).toBe('Hello Lambda!')272+ if(response.headers && response.headers['content-type']){264- expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/)273+ expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/)274+ } else {275+ throw new Error("'content-type' header is missing in the response");276+ }277 })278 279 it('Should handle a GET request and return a 200 response (Lambda@Edge origin response)', async () => {280 const event = {128 unmodified lines409 }410 const response = await handler(event)411 expect(response.status).toBe('200')412 expect(response.body).toBe('Hello Lambda!')413+ if(response.headers && response.headers['content-type']){401- expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/)414+ expect(response.headers['content-type'][0].value).toMatch(/^text\/plain/)415+ } else {416+ throw new Error("'content-type' header is missing in the response");417+ }418 })419 420 it('Should handle a GET request and return a 200 response with binary', async () => {421 const event = {28 unmodified lines450 const response = await handler(event)451 452 expect(response.status).toBe('200')453 expect(response.body).toBe('RmFrZSBJbWFnZQ==') // base64 encoded fake image454+ if(response.headers && response.headers['content-type']){438- expect(response.headers['content-type'][0].value).toMatch(/^image\/png/)455+ expect(response.headers['content-type'][0].value).toMatch(/^image\/png/)456+ } else {457+ throw new Error("'content-type' header is missing in the response");458+ }459 })460 461 it('Should handle a GET request and return a 404 response', async () => {462 const event = {166 unmodified linesmodified:src/adapter/lambda-edge/handler.ts+5 added-5 removed
61 unmodified lines6263646566676869697071717273747576777879808136 unmodified lines11811912012112012112212212312412512621 unmodified lines61 unmodified linesinterface CloudFrontEdgeEvent { Records: CloudFrontEvent[];}
// https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-generating-http-responses-in-requests.html#lambda-generating-http-responses-programming-modelinterface CloudFrontResult { status: string; statusDescription?: string; headers: { headers?: { [header: string]: { key?: string; key: string; value: string; }[]; }; body?: string; bodyEncoding?: 'text' | 'base64';}
/** * Accepts events from 'Lambda@Edge' event36 unmodified lines const url = queryString ? `${urlPath}?${queryString}` : urlPath
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 = { headers,21 unmodified lines