Skip to content
Closed
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
6 changes: 6 additions & 0 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,12 @@ export class Request extends Macroable {
return false
}

if (identifier.includes('*')) {
const escaped = identifier.replace(/[-/\\^$+?.()|[\]{}]/g, '\\$&')
const regex = new RegExp('^' + escaped.replace(/\*/g, '.*') + '$')
return regex.test(route.name || route.pattern)
}

return route.handler.reference === identifier
}
)
Expand Down
30 changes: 30 additions & 0 deletions tests/request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,36 @@ test.group('Request', () => {
})
})

test('find if an wildcard identifiers matches the request route name', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
const request = new RequestFactory().merge({ req, res, encryption }).create()
request.ctx = new HttpContextFactory().merge({ request }).create()
;(request.ctx.route as any) = {
pattern: '/users/:id',
name: 'admin.users.show',
handler: { name: '#controllers/user', handle: () => {} },
}

res.writeHead(200, { 'content-type': 'application/json' })
res.end(
JSON.stringify({
wildcard_trailing_match: request.matchesRoute(['admin.users.*']),
wildcard_middle_match: request.matchesRoute(['admin.*.show']),
wildcard_array_match: request.matchesRoute(['admin.posts.*', 'admin.users.*']),
wildcard_no_match: request.matchesRoute(['admin.invoices.*']),
})
)
})

const { body } = await supertest(url).get('/users/1')
assert.deepEqual(body, {
wildcard_trailing_match: true,
wildcard_middle_match: true,
wildcard_array_match: true,
wildcard_no_match: false,
})
})

test('get request json representation', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
const request = new RequestFactory().merge({ req, res, encryption }).create()
Expand Down