dherault / serverless-offline

Emulate AWS λ and API Gateway locally when developing your Serverless project
MIT License
5.16k stars 794 forks source link

Only top method is used in alb event with multiple methods #1771

Open apokryfos opened 3 months ago

apokryfos commented 3 months ago

Bug Report

The ALB event documentation allows specifying multiple methods. However if I try to run my configuration using serverless-offline, only the top method is used.

Current Behavior

Sample Code

service: my-service

plugins:
  - serverless-webpack
  - serverless-offline
custom:
  serverless-offline:
    httpPort: 3000
    host: 0.0.0.0 

provider:
  name: aws
  runtime: nodejs18.x

functions:
  myFunc:
    handler: src/alb.myFunc
    events:
      - alb:
          listenerArn: <some value, probably not important>
          priority: <some other value, probably not important>
          conditions:
            host: <host>
            path: /api/*
            method:
              - POST
              - GET

Expected behavior/code

When I run sls offline I expect there to be two listeners on the /{stage}/api path but instead I only get one i.e.

What I currently see:

   ┌──────────────────────────────────────────────────────────────────────────────┐
   │                                                                              │
   │   POST | http://0.0.0.0:3003/dev/api/{0}                                     │
   │   POST | http://0.0.0.0:3003/2015-03-31/functions/myFunc/invocations         │
   │                                                                              │
   └──────────────────────────────────────────────────────────────────────────────┘

What I was expecting to see:

   ┌──────────────────────────────────────────────────────────────────────────────┐
   │                                                                              │
   │   POST | http://0.0.0.0:3003/dev/api/{0}                                     │
   │   GET  | http://0.0.0.0:3003/dev/api/{0}                                     │
   │   POST | http://0.0.0.0:3003/2015-03-31/functions/myFunc/invocations         │
   │                                                                              │
   └──────────────────────────────────────────────────────────────────────────────┘

Environment

Possible Solution

The issue seems to be at https://github.com/dherault/serverless-offline/blob/master/src/events/alb/HttpServer.js#L297 where the method is selected as

method = albEvent.conditions.method[0].toUpperCase()

a possible solution would be to not select the first method but keep the methods as an array then iterate through each to create an hapiHandler and route

I don't currently have time to make a PR but if no one else can I can come back to this when time permits.