Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: event.resource in catch-all route gets + changed to * #1524

Merged
merged 4 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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