fix(adapter): handle multi value headers in AWS Lambda (#2494)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Headers can be undefined on ALB * Delegate headers from multiValueHeaders * Add test * Assert header values * format * Make multiValueHeaders optional so it can be omitted * Avoid Object.entries when undefined * Write tests in runtime_tests
authored by TATSUNO “Taz” Yasuhiro · committed by GitHub · parent 932307e · Browse files · 2 files·+117 added−5 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+117 added−5 removed
- tests:+101 added−0 removed
- other:+16 added−5 removed
Provenance
- Commit
acb56b8- Tree
210850e- Author
TATSUNO “Taz” Yasuhiro <ytatsuno.jp@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
932307e
2 files changed+117 added−5 removed
modified:runtime_tests/lambda/index.test.ts+101 added
137 unmodified lines138 }139 return c.text('Valid Cookies')140 })141 142+ app.post('/headers', (c) => {143+ if (c.req.header('foo')?.includes('bar')) {144+ return c.json({ message: 'ok' })145+ }146+ return c.json({ message: 'fail' }, 400)147+ })148+149 const handler = handle(app)150 151 const testApiGatewayRequestContext = {152 accountId: '123456789012',344 unmodified lines497 testCookie2.serialized,498 ])499 })500 501+ describe('headers', () => {502+ describe('single-value headers', () => {503+ it('Should extract single-value headers and return 200 (ALBProxyEvent)', async () => {504+ const event = {505+ body: '{}',506+ httpMethod: 'POST',507+ isBase64Encoded: false,508+ path: '/headers',509+ headers: {510+ host: 'localhost',511+ foo: 'bar',512+ },513+ requestContext: testALBRequestContext,514+ }515+ const apiGatewayResponseV2 = await handler(event)516+ expect(apiGatewayResponseV2.statusCode).toBe(200)517+ })518+519+ it('Should extract single-value headers and return 200 (APIGatewayProxyEvent)', async () => {520+ const apigatewayProxyEvent = {521+ version: '1.0',522+ resource: '/headers',523+ httpMethod: 'POST',524+ headers: {525+ host: 'localhost',526+ foo: 'bar',527+ },528+ path: '/headers',529+ body: null,530+ isBase64Encoded: false,531+ requestContext: testApiGatewayRequestContext,532+ }533+ const apiGatewayResponseV2 = await handler(apigatewayProxyEvent)534+ expect(apiGatewayResponseV2.statusCode).toBe(200)535+ })536+537+ it('Should extract single-value headers and return 200 (APIGatewayProxyEventV2)', async () => {538+ const apigatewayProxyV2Event = {539+ version: '2.0',540+ routeKey: '$default',541+ headers: {542+ host: 'localhost',543+ foo: 'bar',544+ },545+ rawPath: '/headers',546+ rawQueryString: '',547+ requestContext: testApiGatewayRequestContextV2,548+ resource: '/headers',549+ body: null,550+ isBase64Encoded: false,551+ }552+ const apiGatewayResponseV2 = await handler(apigatewayProxyV2Event)553+ expect(apiGatewayResponseV2.statusCode).toBe(200)554+ })555+ })556+557+ describe('multi-value headers', () => {558+ it('Should extract multi-value headers and return 200 (ALBProxyEvent)', async () => {559+ const event = {560+ body: '{}',561+ httpMethod: 'POST',562+ isBase64Encoded: false,563+ path: '/headers',564+ multiValueHeaders: {565+ host: ['localhost'],566+ foo: ['bar'],567+ },568+ requestContext: testALBRequestContext,569+ }570+ const apiGatewayResponseV2 = await handler(event)571+ expect(apiGatewayResponseV2.statusCode).toBe(200)572+ })573+574+ it('Should extract multi-value headers and return 200 (APIGatewayProxyEvent)', async () => {575+ const apigatewayProxyEvent = {576+ version: '1.0',577+ resource: '/headers',578+ httpMethod: 'POST',579+ headers: {},580+ multiValueHeaders: {581+ host: ['localhost'],582+ foo: ['bar'],583+ },584+ path: '/headers',585+ body: null,586+ isBase64Encoded: false,587+ requestContext: testApiGatewayRequestContext,588+ }589+ const apiGatewayResponseV2 = await handler(apigatewayProxyEvent)590+ expect(apiGatewayResponseV2.statusCode).toBe(200)591+ })592+ })593+ })594+595 it('Should handle a POST request and return a 200 response if cookies match (APIGatewayProxyEvent V1 and V2)', async () => {596 const apiGatewayEvent = {597 version: '1.0',598 resource: '/cookie',160 unmodified linesmodified:src/adapter/aws-lambda/handler.ts+16 added-5 removed
20 unmodified lines21 export interface APIGatewayProxyEventV2 {22 version: string23 routeKey: string24 headers: Record<string, string | undefined>25+ multiValueHeaders?: undefined26 cookies?: string[]27 rawPath: string28 rawQueryString: string29 body: string | null33 unmodified lines63 64 // When calling Lambda through an Application Load Balancer65 export interface ALBProxyEvent {66 httpMethod: string66- headers: Record<string, string | undefined>67+ headers?: Record<string, string | undefined>68+ multiValueHeaders?: Record<string, string[] | undefined>69 path: string70 body: string | null71 isBase64Encoded: boolean72 queryStringParameters?: Record<string, string | undefined>126 unmodified lines199 const queryString = extractQueryString(event)200 const domainName =201 event.requestContext && 'domainName' in event.requestContext202 ? event.requestContext.domainName201- : event.headers['host']203+ : event.headers?.['host'] ?? event.multiValueHeaders?.['host']?.[0]204 const path = isProxyEventV2(event) ? event.rawPath : event.path205 const urlPath = `https://${domainName}${path}`206 const url = queryString ? `${urlPath}?${queryString}` : urlPath207 208 const headers = new Headers()209 getCookies(event, headers)210+ if (event.headers) {208- for (const [k, v] of Object.entries(event.headers)) {209- if (v) {210- headers.set(k, v)211+ for (const [k, v] of Object.entries(event.headers)) {212+ if (v) {213+ headers.set(k, v)214+ }215 }216 }217+ if (event.multiValueHeaders) {218+ for (const [k, values] of Object.entries(event.multiValueHeaders)) {219+ if (values) {220+ values.forEach((v) => headers.append(k, v))221+ }222+ }223+ }224 225 const method = isProxyEventV2(event) ? event.requestContext.http.method : event.httpMethod226 const requestInit: RequestInit = {227 headers,57 unmodified lines