Skip to content

Commit

Permalink
fix: Support ALB Event response headers (#1756)
Browse files Browse the repository at this point in the history
* fix: Support ALB Event response headers

* fix: Run prettier:fix
  • Loading branch information
corwinm committed May 22, 2024
1 parent 8ee5d70 commit 378ef71
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 0 deletions.
9 changes: 9 additions & 0 deletions examples/events/alb/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,12 @@ functions:
listenerArn: arn:aws:elasticloadbalancing:us-east-1:12345:listener/app/my-load-balancer/50dc6c495c0c9188/50dc6c495c0c9188
priority: 1
handler: src/handler.hello
hello-headers:
events:
- alb:
conditions:
method: GET
path: hello-headers
listenerArn: arn:aws:elasticloadbalancing:us-east-1:12345:listener/app/my-load-balancer/50dc6c495c0c9188/50dc6c495c0c9188
priority: 2
handler: src/handler-response-header.responseHeader
19 changes: 19 additions & 0 deletions examples/events/alb/src/handler-response-header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { stringify } = JSON

export async function responseHeader(event, context) {
return {
body: stringify(
{
context,
event,
},
null,
2,
),
headers: {
"Content-Type": "text/plain",
"Set-Cookie": "alb-cookie=works",
},
statusCode: 200,
}
}
2 changes: 2 additions & 0 deletions src/events/alb/HttpServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ export default class HttpServer {
override: false,
})

response.headers = headers

if (typeof result === "string") {
response.source = stringify(result)
} else if (result && result.body !== undefined) {
Expand Down
36 changes: 36 additions & 0 deletions tests/integration/alb-headers/alb-headers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import assert from "node:assert"
import { join } from "desm"
import { setup, teardown } from "../../_testHelpers/index.js"
import { BASE_URL } from "../../config.js"

describe("ALB Headers Tests", function desc() {
beforeEach(() =>
setup({
noPrependStageInUrl: false,
servicePath: join(import.meta.url),
}),
)

afterEach(() => teardown())

//
;["GET", "POST"].forEach((method) => {
it(`${method} response headers`, async () => {
const url = new URL("/dev/response-headers", BASE_URL)
url.port = url.port ? "3003" : url.port
const options = {
method,
}

const response = await fetch(url, options)

assert.equal(response.status, 200)

const body = await response.text()

assert.equal(body, "Plain text")
assert.equal(response.headers.get("Content-Type"), "text/plain")
assert.equal(response.headers.get("Set-Cookie"), "alb-cookie=works")
})
})
})
27 changes: 27 additions & 0 deletions tests/integration/alb-headers/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
service: alb-headers

configValidationMode: error
deprecationNotificationMode: error

plugins:
- ../../../src/index.js

provider:
architecture: arm64
deploymentMethod: direct
memorySize: 1024
name: aws
region: us-east-1
runtime: nodejs18.x
stage: dev
versionFunctions: false

functions:
response-headers:
events:
- alb:
conditions:
path: response-headers
listenerArn: arn:aws:elasticloadbalancing:us-east-1:12345:listener/app/my-load-balancer/50dc6c495c0c9188/50dc6c495c0c9188
priority: 1
handler: src/handler.responseHeader
10 changes: 10 additions & 0 deletions tests/integration/alb-headers/src/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export async function responseHeader() {
return {
body: "Plain text",
headers: {
"Content-Type": "text/plain",
"Set-Cookie": "alb-cookie=works",
},
statusCode: 200,
}
}
3 changes: 3 additions & 0 deletions tests/integration/alb-headers/src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}

0 comments on commit 378ef71

Please sign in to comment.