fix(adapter/aws-lambda): add alb event requestContext undefined check for testing convenience (#3691)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Add alb event null check for testing convenience * Add tests for getProcessor function * Export processor classes and add instanceOf assertions in tests
authored by Omkar Patil · committed by GitHub · parent bfc190f · Browse files · 2 files·+72 added−8 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+72 added−8 removed
- tests:+63 added−2 removed
- other:+9 added−6 removed
Provenance
- Commit
7ebf854- Tree
b7f5102- Author
Omkar Patil <ospatil@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
bfc190f
2 files changed+72 added−8 removed
modified:runtime-tests/lambda/index.test.ts+63 added-2 removed
1 import { Readable } from 'stream'2-import { handle, streamHandle } from '../../src/adapter/aws-lambda/handler'3-import type { LambdaEvent } from '../../src/adapter/aws-lambda/handler'2+import {3+ ALBProcessor,4+ EventV1Processor,5+ EventV2Processor,6+ getProcessor,7+ handle,8+ streamHandle,9+} from '../../src/adapter/aws-lambda/handler'10 import type {11+ ALBProxyEvent,12+ APIGatewayProxyEventV2,13+ LambdaEvent,14+} from '../../src/adapter/aws-lambda/handler'15+import type {16 ApiGatewayRequestContext,17 ApiGatewayRequestContextV2,18 LambdaContext,19 } from '../../src/adapter/aws-lambda/types'957 unmodified lines977 const nullSequence = '\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000'978 expect(output).toContain(jsonResponsePrelude.replace(nullSequence, ''))979 })980 })981+982+describe('getProcessor function', () => {983+ it('Should return ALBProcessor for an ALBProxyEvent event', () => {984+ const event: ALBProxyEvent = {985+ httpMethod: 'GET',986+ path: '/',987+ body: null,988+ isBase64Encoded: false,989+ requestContext: {990+ elb: {991+ targetGroupArn:992+ 'arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/lambda-279XGJDqGZ5rsrHC2Fjr/49e9d65c45c6791a',993+ },994+ },995+ }996+997+ const processor = getProcessor(event)998+ expect(processor).toBeInstanceOf(ALBProcessor)999+ })1000+1001+ it('Should return EventV1Processor for an event without requestContext', () => {1002+ const event = {1003+ httpMethod: 'GET',1004+ path: '/',1005+ body: null,1006+ isBase64Encoded: false,1007+ }1008+1009+ // while LambdaEvent RequestContext property is mandatory, it can be absent when testing through invoke-api or AWS Console1010+ // in such cases, a V1 processor should be returned1011+ const processor = getProcessor(event as unknown as LambdaEvent)1012+ expect(processor).toBeInstanceOf(EventV1Processor)1013+ })1014+1015+ it('Should return EventV2Processor for an APIGatewayProxyEventV2 event', () => {1016+ const event: APIGatewayProxyEventV2 = {1017+ version: '2.0',1018+ routeKey: '$default',1019+ headers: { 'content-type': 'text/plain' },1020+ rawPath: '/',1021+ rawQueryString: '',1022+ body: null,1023+ isBase64Encoded: false,1024+ requestContext: testApiGatewayRequestContextV2,1025+ }1026+1027+ const processor = getProcessor(event)1028+ expect(processor).toBeInstanceOf(EventV2Processor)1029+ })1030+})modified:src/adapter/aws-lambda/handler.ts+9 added-6 removed
182 unmodified lines183 return processor.createResult(event, res)184 }185 }186 187-abstract class EventProcessor<E extends LambdaEvent> {187+export abstract class EventProcessor<E extends LambdaEvent> {188 protected abstract getPath(event: E): string189 190 protected abstract getMethod(event: E): string191 79 unmodified lines271 }272 }273 }274 275-class EventV2Processor extends EventProcessor<APIGatewayProxyEventV2> {275+export class EventV2Processor extends EventProcessor<APIGatewayProxyEventV2> {276 protected getPath(event: APIGatewayProxyEventV2): string {277 return event.rawPath278 }279 34 unmodified lines314 }315 316 const v2Processor: EventV2Processor = new EventV2Processor()317 318-class EventV1Processor extends EventProcessor<Exclude<LambdaEvent, APIGatewayProxyEventV2>> {318+export class EventV1Processor extends EventProcessor<Exclude<LambdaEvent, APIGatewayProxyEventV2>> {319 protected getPath(event: Exclude<LambdaEvent, APIGatewayProxyEventV2>): string {320 return event.path321 }322 51 unmodified lines374 }375 376 const v1Processor: EventV1Processor = new EventV1Processor()377 378-class ALBProcessor extends EventProcessor<ALBProxyEvent> {378+export class ALBProcessor extends EventProcessor<ALBProxyEvent> {379 protected getHeaders(event: ALBProxyEvent): Headers {380 const headers = new Headers()381 // if multiValueHeaders is present the ALB will use it instead of the headers field382 // https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#multi-value-headers23 unmodified lines406 }407 408 protected getQueryString(event: ALBProxyEvent): string {409 // In the case of ALB Integration either queryStringParameters or multiValueQueryStringParameters can be present not both410- /* 410+ /*411 In other cases like when using the serverless framework, the event object does contain both queryStringParameters and multiValueQueryStringParameters:412 Below is an example event object for this URL: /payment/b8c55e69?select=amount&select=currency413 {414 ...55 unmodified lines470 return v1Processor471 }472 473 const isProxyEventALB = (event: LambdaEvent): event is ALBProxyEvent => {474+ if (event.requestContext) {474- return Object.hasOwn(event.requestContext, 'elb')475+ return Object.hasOwn(event.requestContext, 'elb')476+ }477+ return false478 }479 480 const isProxyEventV2 = (event: LambdaEvent): event is APIGatewayProxyEventV2 => {481 return Object.hasOwn(event, 'rawPath')14 unmodified lines