Skip to content

Commit

Permalink
fix: support literal query string (#2590)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikicho committed Feb 17, 2024
1 parent 7e957b3 commit 4162fa8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/interceptor.js
Expand Up @@ -510,7 +510,7 @@ module.exports = class Interceptor {
strFormattingFn = common.percentDecode
}

if (queries instanceof URLSearchParams) {
if (queries instanceof URLSearchParams || typeof queries === 'string') {
// Normalize the data into the shape that is matched against.
// Duplicate keys are handled by combining the values into an array.
queries = querystring.parse(queries.toString())
Expand Down
20 changes: 18 additions & 2 deletions tests/got/test_query.js
Expand Up @@ -19,6 +19,22 @@ describe('query params in path', () => {
})

describe('`query()`', () => {
describe('when called with a string', () => {
it('matches a url encoded query string of the same name=value', async () => {
const scope = nock('http://example.test')
.get('/')
.query('foo%5Bbar%5D%3Dhello%20world%21')
.reply()

const { statusCode } = await got(
'http://example.test/?foo%5Bbar%5D%3Dhello%20world%21',
)

expect(statusCode).to.equal(200)
scope.done()
})
})

describe('when called with an object', () => {
it('matches a query string of the same name=value', async () => {
const scope = nock('http://example.test')
Expand Down Expand Up @@ -256,8 +272,8 @@ describe('`query()`', () => {
const interceptor = nock('http://example.test').get('/')

expect(() => {
interceptor.query('foo=bar')
}).to.throw(Error, 'Argument Error: foo=bar')
interceptor.query(1)
}).to.throw(Error, 'Argument Error: 1')
})
})

Expand Down

0 comments on commit 4162fa8

Please sign in to comment.