Skip to content

Commit

Permalink
fix: Support alb with no method conditions (#1653)
Browse files Browse the repository at this point in the history
Co-authored-by: Frode Aannevik <frodea@dintero.com>
  • Loading branch information
frodeaa and Frode Aannevik committed Sep 26, 2023
1 parent caf4fe8 commit 5e6514c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/events/alb/HttpServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,11 @@ export default class HttpServer {
}

createRoutes(functionKey, albEvent) {
const method = albEvent.conditions.method[0].toUpperCase()
let method = 'ANY'
if ((albEvent.conditions.method || []).length > 0) {
method = albEvent.conditions.method[0].toUpperCase()
}

const path = albEvent.conditions.path[0]
const hapiPath = generateAlbHapiPath(path, this.#options, this.#serverless)

Expand Down
17 changes: 15 additions & 2 deletions tests/integration/alb-handler/handlerPayload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,25 @@ describe('ALB handler payload tests with prepend off', function desc() {
path: '/test-query-parameters?foo=anything&bar=50',
status: 200,
},
].forEach(({ description, expected, path, status }) => {
{
description: 'test POST when no method condition',
expected: 'POST',
method: 'POST',
path: '/test-no-method-conditions',
status: 200,
},
{
description: 'test GET when no method condition',
expected: 'GET',
path: '/test-no-method-conditions',
status: 200,
},
].forEach(({ description, expected, path, status, method }) => {
it(description, async () => {
const url = new URL(path, BASE_URL)
url.port = url.port ? '3003' : url.port

const response = await fetch(url)
const response = await fetch(url, { method })
assert.equal(response.status, status)

if (expected) {
Expand Down
9 changes: 9 additions & 0 deletions tests/integration/alb-handler/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,12 @@ functions:
method: GET
path: /test-query-parameters
handler: src/handler.TestQueryParameters

TestNoMethodCondition:
events:
- alb:
listenerArn: arn:aws:elasticloadbalancing:us-east-1:12345:listener/app/my-load-balancer/50dc6c495c0c9188/50dc6c495c0c9188
priority: 1
conditions:
path: /test-no-method-conditions
handler: src/handler.TestNoMethodConditions
7 changes: 7 additions & 0 deletions tests/integration/alb-handler/src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,10 @@ export const TestQueryParameters = (event, context, callback) => {
statusCode: 200,
})
}

export const TestNoMethodConditions = (event, context, callback) => {
callback(null, {
body: stringify(event.httpMethod),
statusCode: 200,
})
}

0 comments on commit 5e6514c

Please sign in to comment.