serverless-offline icon indicating copy to clipboard operation
serverless-offline copied to clipboard

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

Open kappa0923 opened this issue 1 year ago • 2 comments

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.

  • file: serverless.ts
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']
          }
        }
      }
    ]
  }
}
  • file: handler.ts
return {
  statusCode: 200,
  body: csv,
  headers: {
    'Content-Type': 'text/csv',
    'Content-Disposition': 'attachment; filename="download.csv"'
  }
}
  • Actual Request and Response
$ 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

  • serverless version: 3.34.0
  • serverless-offline version: 12.0.4
  • @serverless/typescript version: 3.30.1
  • node.js version: v18.17.0
  • OS: Debian GNU/Linux 11 (bullseye) (Running on Docker/DevContainer with WSL2/Windows 10)

Possible Solution

None

Additional Context/Screenshots

None

kappa0923 avatar Aug 31 '23 23:08 kappa0923

Getting the same issue!

ly-tc avatar Dec 21 '23 07:12 ly-tc

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.

corwinm avatar Feb 08 '24 01:02 corwinm