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( ...@@ -66,15 +66,19 @@ export function matchHas(
return true return true
} else if (value) { } else if (value) {
const matcher = new RegExp(`^${hasItem.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) {
if (matches.groups) { if (Array.isArray(matches)) {
Object.keys(matches.groups).forEach((groupKey) => { if (matches.groups) {
params[groupKey] = matches.groups![groupKey] Object.keys(matches.groups).forEach((groupKey) => {
}) params[groupKey] = matches.groups![groupKey]
} else if (hasItem.type === 'host' && matches[0]) { })
params.host = matches[0] } else if (hasItem.type === 'host' && matches[0]) {
params.host = matches[0]
}
} }
return true return true
} }
......
...@@ -400,6 +400,18 @@ module.exports = { ...@@ -400,6 +400,18 @@ module.exports = {
destination: 'https://:subdomain.example.com/some-path/end?a=b', destination: 'https://:subdomain.example.com/some-path/end?a=b',
permanent: false, 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) => { ...@@ -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 () => { it('should match has header for header correctly', async () => {
const res = await fetchViaHTTP(appPort, '/has-header-1', undefined, { const res = await fetchViaHTTP(appPort, '/has-header-1', undefined, {
headers: { headers: {
...@@ -1396,6 +1415,19 @@ const runTests = (isDev = false) => { ...@@ -1396,6 +1415,19 @@ const runTests = (isDev = false) => {
source: '/has-redirect-6', source: '/has-redirect-6',
statusCode: 307, 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: [ 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