Skip to content

Commit

Permalink
Merge branch 'main' into feat/functions-v2-improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas authored Oct 10, 2023
2 parents a4887e2 + 970b3df commit 9c0bb59
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/lib/functions/netlify-function.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ export default class NetlifyFunction {
async matchURLPath(rawPath, method) {
await this.buildQueue

const path = (rawPath.endsWith('/') ? rawPath.slice(0, -1) : rawPath).toLowerCase()
let path = rawPath !== '/' && rawPath.endsWith('/') ? rawPath.slice(0, -1) : rawPath
path = path.toLowerCase()
const { routes = [] } = this.buildData
return routes.find(({ expression, literal, methods }) => {
if (methods.length !== 0 && !methods.includes(method)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default async (req) => new Response(`Catchall Path`)

export const config = {
path: '/*',
method: 'PATCH',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default async (req) => new Response(`With literal path: ${req.url}`)

export const config = {
path: '/',
method: 'GET'
}
15 changes: 15 additions & 0 deletions tests/integration/commands/dev/v2-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,21 @@ describe.runIf(gte(version, '18.13.0'))('v2 api', () => {
expect(await response.text()).toBe(`With expression path: {"sku":"netlify"}`)
})

test<FixtureTestContext>('should serve the custom path ath the / route as specified in the in source config', async ({
devServer,
}) => {
const url = `http://localhost:${devServer.port}/`
const response = await fetch(url)
expect(response.status).toBe(200)
expect(await response.text()).toBe(`With literal path: http://localhost:${devServer.port}/`)
})

test<FixtureTestContext>('catchall path applies to root path', async ({ devServer }) => {
const response = await fetch( `http://localhost:${devServer.port}/`, { method:"PATCH"})
expect(response.status).toBe(200)
expect(await response.text()).toBe(`Catchall Path`)
})

test<FixtureTestContext>('returns 404 when using the default function URL to access a function with custom routes', async ({
devServer,
}) => {
Expand Down

0 comments on commit 9c0bb59

Please sign in to comment.