Skip to content

Commit

Permalink
fix: previously unsurported array-style queyr variants
Browse files Browse the repository at this point in the history
  • Loading branch information
tsebas committed Mar 26, 2024
1 parent e44812b commit d9d32af
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
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

0 comments on commit d9d32af

Please sign in to comment.