Skip to content
Myr

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

2 files+72 added−8 removed

modified:runtime-tests/lambda/index.test.ts+63 added-2 removed
1import { Readable } from 'stream'2import { handle, streamHandle } from '../../src/adapter/aws-lambda/handler'3import type { LambdaEvent } from '../../src/adapter/aws-lambda/handler'2import {3 ALBProcessor,4 EventV1Processor,5 EventV2Processor,6 getProcessor,7 handle,8 streamHandle,9} from '../../src/adapter/aws-lambda/handler'10import type {11 ALBProxyEvent,12 APIGatewayProxyEventV2,13 LambdaEvent,14} from '../../src/adapter/aws-lambda/handler'15import type {16 ApiGatewayRequestContext,17 ApiGatewayRequestContextV2,18 LambdaContext,19} from '../../src/adapter/aws-lambda/types'
957 unmodified lines
977 const nullSequence = '\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000'978 expect(output).toContain(jsonResponsePrelude.replace(nullSequence, ''))979 })980})981982describe('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 }996997 const processor = getProcessor(event)998 expect(processor).toBeInstanceOf(ALBProcessor)999 })10001001 it('Should return EventV1Processor for an event without requestContext', () => {1002 const event = {1003 httpMethod: 'GET',1004 path: '/',1005 body: null,1006 isBase64Encoded: false,1007 }10081009 // 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 })10141015 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 }10261027 const processor = getProcessor(event)1028 expect(processor).toBeInstanceOf(EventV2Processor)1029 })1030})
modified:src/adapter/aws-lambda/handler.ts+9 added-6 removed
182 unmodified lines
183 return processor.createResult(event, res)184 }185}186187abstract class EventProcessor<E extends LambdaEvent> {187export abstract class EventProcessor<E extends LambdaEvent> {188 protected abstract getPath(event: E): string189190 protected abstract getMethod(event: E): string191
79 unmodified lines
271 }272 }273}274275class EventV2Processor extends EventProcessor<APIGatewayProxyEventV2> {275export class EventV2Processor extends EventProcessor<APIGatewayProxyEventV2> {276 protected getPath(event: APIGatewayProxyEventV2): string {277 return event.rawPath278 }279
34 unmodified lines
314}315316const v2Processor: EventV2Processor = new EventV2Processor()317318class EventV1Processor extends EventProcessor<Exclude<LambdaEvent, APIGatewayProxyEventV2>> {318export class EventV1Processor extends EventProcessor<Exclude<LambdaEvent, APIGatewayProxyEventV2>> {319 protected getPath(event: Exclude<LambdaEvent, APIGatewayProxyEventV2>): string {320 return event.path321 }322
51 unmodified lines
374}375376const v1Processor: EventV1Processor = new EventV1Processor()377378class ALBProcessor extends EventProcessor<ALBProxyEvent> {378export 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-headers
23 unmodified lines
406 }407408 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 lines
470 return v1Processor471}472473const 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}479480const isProxyEventV2 = (event: LambdaEvent): event is APIGatewayProxyEventV2 => {481 return Object.hasOwn(event, 'rawPath')
14 unmodified lines