Skip to content

Commit

Permalink
fix: event.resource in catch-all route gets + changed to * (#1524)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ankcorn authored Aug 5, 2022
1 parent c76939d commit 0494fdb
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/events/http/lambda-events/LambdaProxyIntegrationEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ export default class LambdaProxyIntegrationEvent {
const httpMethod = method.toUpperCase()
const requestTime = formatToClfTime(received)
const requestTimeEpoch = received
const resource = this.#routeKey || route.path.replace(`/${this.#stage}`, '')
// NOTE replace * added by generateHapiPath util so api gateway event is accurate
const resource =
this.#routeKey ||
route.path.replace(`/${this.#stage}`, '').replace('*', '+')

return {
body,
Expand Down
13 changes: 13 additions & 0 deletions tests/endToEnd/starRoutesRestApi/src/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

const { stringify } = JSON

exports.hello = async (event) => {
return {
body: stringify({
path: event.path,
resource: event.resource,
}),
statusCode: 200,
}
}
3 changes: 3 additions & 0 deletions tests/endToEnd/starRoutesRestApi/src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
22 changes: 22 additions & 0 deletions tests/endToEnd/starRoutesRestApi/src/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
service: uncategorized-tests

configValidationMode: error

plugins:
- ../../../../

provider:
memorySize: 128
name: aws
region: us-east-1 # default
runtime: nodejs16.x
stage: dev
versionFunctions: false

functions:
hello:
events:
- http:
method: any
path: /{proxy+}
handler: handler.hello
27 changes: 27 additions & 0 deletions tests/endToEnd/starRoutesRestApi/starRoutesRestApi.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import assert from 'node:assert'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { BASE_URL } from '../../config.js'
import { setup, teardown } from '../../_testHelpers/index.js'

const __dirname = dirname(fileURLToPath(import.meta.url))

describe('star routes', function desc() {
beforeEach(() =>
setup({
servicePath: resolve(__dirname, 'src'),
}),
)

afterEach(() => teardown())

describe('when a catch all route is used in a rest api', () => {
it('it should return a payload', async () => {
const url = new URL('/dev/hello', BASE_URL)
const response = await fetch(url)
const json = await response.json()

assert.deepEqual(json, { path: '/hello', resource: '/{proxy+}' })
})
})
})
2 changes: 1 addition & 1 deletion tests/endToEnd/trailingSlash/trailingSlash.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('noStripTrailingSlashInUrl option', function desc() {

assert.deepEqual(json, {
path: '/echo/something/',
resource: '/echo/{any*}',
resource: '/echo/{any+}',
})
})
})
Expand Down

0 comments on commit 0494fdb

Please sign in to comment.