Skip to content

Commit

Permalink
Resolves dherault#1771
Browse files Browse the repository at this point in the history
  • Loading branch information
Yiannis Siantos committed Mar 28, 2024
1 parent c85a192 commit e63b88a
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 40 deletions.
83 changes: 43 additions & 40 deletions src/events/alb/HttpServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,11 @@ export default class HttpServer {
}

createRoutes(functionKey, albEvent) {
let method = "ANY"
let methods = ["ANY"]
if ((albEvent.conditions.method || []).length > 0) {
method = albEvent.conditions.method[0].toUpperCase()
methods = albEvent.conditions.method.map((m) => m.toUpperCase())
}
methods = methods.includes('ANY') ? ['ANY'] : methods;

const path = albEvent.conditions.path[0]
const hapiPath = generateAlbHapiPath(path, this.#options, this.#serverless)
Expand All @@ -304,51 +305,53 @@ export default class HttpServer {
const { host, albPort, httpsProtocol } = this.#options
const server = `${httpsProtocol ? "https" : "http"}://${host}:${albPort}`

this.#terminalInfo.push({
invokePath: `/2015-03-31/functions/${functionKey}/invocations`,
method,
path: hapiPath,
server,
stage: this.#options.noPrependStageInUrl ? null : stage,
})
methods.forEach((method) => {
this.#terminalInfo.push({
invokePath: `/2015-03-31/functions/${functionKey}/invocations`,
method,
path: hapiPath,
server,
stage: this.#options.noPrependStageInUrl ? null : stage,
})

const hapiMethod = method === "ANY" ? "*" : method
const hapiOptions = {
response: {
emptyStatusCode: 200,
},
}
const hapiMethod = method === "ANY" ? "*" : method
const hapiOptions = {
response: {
emptyStatusCode: 200,
},
}

// skip HEAD routes as hapi will fail with 'Method name not allowed: HEAD ...'
// for more details, check https://github.com/dherault/serverless-offline/issues/204
if (hapiMethod === "HEAD") {
log.notice(
"HEAD method event detected. Skipping HAPI server route mapping",
)
// skip HEAD routes as hapi will fail with 'Method name not allowed: HEAD ...'
// for more details, check https://github.com/dherault/serverless-offline/issues/204
if (hapiMethod === "HEAD") {
log.notice(
"HEAD method event detected. Skipping HAPI server route mapping",
)

return
}
return
}

if (hapiMethod !== "HEAD" && hapiMethod !== "GET") {
// maxBytes: Increase request size from 1MB default limit to 10MB.
// Cf AWS API GW payload limits.
hapiOptions.payload = {
maxBytes: 1024 * 1024 * 10,
parse: false,
if (hapiMethod !== "HEAD" && hapiMethod !== "GET") {
// maxBytes: Increase request size from 1MB default limit to 10MB.
// Cf AWS API GW payload limits.
hapiOptions.payload = {
maxBytes: 1024 * 1024 * 10,
parse: false,
}
}
}

const hapiHandler = this.#createHapiHandler({
functionKey,
method,
stage,
})
const hapiHandler = this.#createHapiHandler({
functionKey,
method,
stage,
})

this.#server.route({
handler: hapiHandler,
method: hapiMethod,
options: hapiOptions,
path: hapiPath,
this.#server.route({
handler: hapiHandler,
method: hapiMethod,
options: hapiOptions,
path: hapiPath,
})
})
}

Expand Down
14 changes: 14 additions & 0 deletions tests/integration/alb-handler/handlerPayload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,20 @@ describe("ALB handler payload tests with prepend off", function desc() {
path: "/test-no-method-conditions",
status: 200,
},
{
description: "test multiple-method handler GET",
expected: "GET",
method: "GET",
path: "/test-multiple-methods",
status: 200,
},
{
description: "test multiple-method handler POST",
expected: "POST",
method: "POST",
path: "/test-multiple-methods",
status: 200,
},
].forEach(({ description, expected, path, status, method }) => {
it(description, async () => {
const url = new URL(path, BASE_URL)
Expand Down
11 changes: 11 additions & 0 deletions tests/integration/alb-handler/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,17 @@ functions:
path: /test-query-parameters
handler: src/handler.TestQueryParameters

TestMultipleMethods:
events:
- alb:
listenerArn: arn:aws:elasticloadbalancing:us-east-1:12345:listener/app/my-load-balancer/50dc6c495c0c9188/50dc6c495c0c9188
priority: 1
conditions:
method:
- GET
- POST
path: /test-multiple-methods
handler: src/handler.TestMultipleMethods
TestNoMethodCondition:
events:
- alb:
Expand Down
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 @@ -190,6 +190,13 @@ export const TestQueryParameters = (event, context, callback) => {
})
}

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

export const TestNoMethodConditions = (event, context, callback) => {
callback(null, {
body: stringify(event.httpMethod),
Expand Down

0 comments on commit e63b88a

Please sign in to comment.