Skip to content
Open
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
37 changes: 37 additions & 0 deletions packages/next-routing/src/__tests__/conditions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,43 @@ describe('Has conditions', () => {
expect(result.resolvedPathname).toBe('/admin-dashboard')
})

it('should NOT match substring values in has conditions', async () => {
const headers = new Headers({
'x-role': 'not-admin',
})

const params = createBaseParams({
url: new URL('https://example.com/dashboard'),
headers,
routes: {
beforeMiddleware: [],
beforeFiles: [
{
sourceRegex: '^/dashboard$',
destination: '/admin',
has: [
{
type: 'header',
key: 'x-role',
value: 'admin',
},
],
},
],
afterFiles: [],
dynamicRoutes: [],
onMatch: [],
fallback: [],
},
pathnames: ['/dashboard', '/admin'],
})

const result = await resolveRoutes(params)

expect(result.resolvedPathname).toBe('/dashboard')
expect(result.invocationTarget?.pathname).toBe('/dashboard')
})

it('should match route with cookie condition', async () => {
const headers = new Headers({
cookie: 'session=abc123; theme=dark',
Expand Down
4 changes: 1 addition & 3 deletions packages/next-routing/src/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ function matchesCondition(
// Try to match as regex first
try {
const exactRegex = new RegExp(`^(?:${conditionValue})$`)
const fallbackRegex = new RegExp(conditionValue)
const match =
actualValue.match(exactRegex) ?? actualValue.match(fallbackRegex)
const match = actualValue.match(exactRegex)
if (match) {
const namedCaptures: Record<string, string> = {}
if (match.groups) {
Expand Down