Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: previously unsurported array-style query variants #2730

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/common.js
Expand Up @@ -715,14 +715,18 @@ const expand = input => {
const part = parts[i]
if (i === lastIndex) {
if (Array.isArray(resultPtr)) {
resultPtr[+part] = input[originalPath]
if (part === '') {
resultPtr.push(...input[originalPath])
} else {
resultPtr[+part] = input[originalPath]
}
} else {
resultPtr[part] = input[originalPath]
}
} else {
if (resultPtr[part] === undefined || resultPtr[part] === null) {
const nextPart = parts[i + 1]
if (/^\d+$/.test(nextPart)) {
if (/^\d+$/.test(nextPart) || nextPart === '') {
resultPtr[part] = []
} else {
resultPtr[part] = {}
Expand Down
16 changes: 16 additions & 0 deletions tests/got/test_query_complex.js
Expand Up @@ -111,6 +111,22 @@ describe('`query()` complex encoding', () => {
scope.done()
})

it('query with unindexed array', async () => {
const encodedQuery = 'list[]=123&list[]=456&list[]=789'

const expectedQuery = {
list: [123, 456, 789],
}

const scope = nock('http://example.test')
.get('/test')
.query(expectedQuery)
.reply()
await got(`http://example.test/test?${encodedQuery}`)

scope.done()
})

it('query with array and regexp', async () => {
// In Node 10.x this can be updated:
// const exampleQuery = new URLSearchParams([
Expand Down