dherault / serverless-offline

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

ALB event doesn't reflect custom headers specified in handler.ts #1728

Closed kappa0923 closed 2 months ago

kappa0923 commented 10 months ago

Bug Report

Current Behavior

When using the ALB event in serverless.ts, specifying the response headers in handler.ts doesn't reflect in the actual response.
No error messages are displayed.

Sample Code

Here is a minimal code snippet to reproduce the issue.

service: 'my-service',
frameworkVersion: '3',
plugins: ['serverless-esbuild', 'serverless-offline'],
provider: {
  name: 'aws',
  runtime: 'nodejs18.x',
  region: 'ap-northeast-1',
  stage: 'api',
  environment: {
    AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1',
    NODE_OPTIONS: '--enable-source-maps --stack-trace-limit=1000',
  },
},
functions: { 
  hello: {
    handler: 'src/functions/hello/handler.main',
    events: [
      {
        alb: {
          listenerArn: 'arn:aws:elasticloadbalancing:region:account-id:listener/app/my-load-balancer-name/load-balancer-id/abcdef1234567890',
          priority: 1,
          conditions: {
            path: ['/hello'],
            method: ['GET']
          }
        }
      }
    ]
  }
}
return {
  statusCode: 200,
  body: csv,
  headers: {
    'Content-Type': 'text/csv',
    'Content-Disposition': 'attachment; filename="download.csv"'
  }
}
$ curl --head "http://localhost:3003/api/hello"

HTTP/1.1 200 OK
content-type: application/json; charset=utf-8
cache-control: no-cache
content-length: 2664
vary: accept-encoding
Date: Thu, 31 Aug 2023 11:42:43 GMT
Connection: keep-alive
Keep-Alive: timeout=5

Expected Behavior/Code

The expected request and response should be:

$ curl --head "http://localhost:3003/api/hello"

HTTP/1.1 200 OK
content-type: text/csv; charset=utf-8
content-disposition: attachment; filename="download.csv"
cache-control: no-cache
content-length: 2664
vary: accept-encoding
Date: Thu, 31 Aug 2023 11:42:43 GMT
Connection: keep-alive
Keep-Alive: timeout=5

Environment

Possible Solution

None

Additional Context/Screenshots

None

ly-tc commented 7 months ago

Getting the same issue!

corwinm commented 5 months ago

Hey everyone! I opened a PR to address this. I've been running it locally using patch-package but hopefully if the PR is accepted it will be fixed for everyone needing to use ALBs.