Unverified Commit 5b963422 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix array query value has matching (#29216)

parent 76dee14d
......@@ -66,15 +66,19 @@ export function matchHas(
return true
} else if (value) {
const matcher = new RegExp(`^${hasItem.value}$`)
const matches = value.match(matcher)
const matches = Array.isArray(value)
? value.slice(-1)[0].match(matcher)
: value.match(matcher)
if (matches) {
if (matches.groups) {
Object.keys(matches.groups).forEach((groupKey) => {
params[groupKey] = matches.groups![groupKey]
})
} else if (hasItem.type === 'host' && matches[0]) {
params.host = matches[0]
if (Array.isArray(matches)) {
if (matches.groups) {
Object.keys(matches.groups).forEach((groupKey) => {
params[groupKey] = matches.groups![groupKey]
})
} else if (hasItem.type === 'host' && matches[0]) {
params.host = matches[0]
}
}
return true
}
......
......@@ -400,6 +400,18 @@ module.exports = {
destination: 'https://:subdomain.example.com/some-path/end?a=b',
permanent: false,
},
{
source: '/has-redirect-7',
has: [
{
type: 'query',
key: 'hello',
value: '(?<hello>.*)',
},
],
destination: '/somewhere?value=:hello',
permanent: false,
},
]
},
......
......@@ -1052,6 +1052,25 @@ const runTests = (isDev = false) => {
})
})
it('should match has query redirect with duplicate query key', async () => {
const res = await fetchViaHTTP(
appPort,
'/has-redirect-7',
'?hello=world&hello=another',
{
redirect: 'manual',
}
)
expect(res.status).toBe(307)
const parsed = url.parse(res.headers.get('location'), true)
expect(parsed.pathname).toBe('/somewhere')
expect(parsed.query).toEqual({
hello: ['world', 'another'],
value: 'another',
})
})
it('should match has header for header correctly', async () => {
const res = await fetchViaHTTP(appPort, '/has-header-1', undefined, {
headers: {
......@@ -1396,6 +1415,19 @@ const runTests = (isDev = false) => {
source: '/has-redirect-6',
statusCode: 307,
},
{
source: '/has-redirect-7',
regex: normalizeRegEx('^(?!\\/_next)\\/has-redirect-7(?:\\/)?$'),
has: [
{
type: 'query',
key: 'hello',
value: '(?<hello>.*)',
},
],
destination: '/somewhere?value=:hello',
statusCode: 307,
},
],
headers: [
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment