Skip to content

Commit 4162fa8

Browse files
author
Michael Solomon
authored
fix: support literal query string (#2590)
1 parent 7e957b3 commit 4162fa8

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

lib/interceptor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ module.exports = class Interceptor {
510510
strFormattingFn = common.percentDecode
511511
}
512512

513-
if (queries instanceof URLSearchParams) {
513+
if (queries instanceof URLSearchParams || typeof queries === 'string') {
514514
// Normalize the data into the shape that is matched against.
515515
// Duplicate keys are handled by combining the values into an array.
516516
queries = querystring.parse(queries.toString())

tests/got/test_query.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ describe('query params in path', () => {
1919
})
2020

2121
describe('`query()`', () => {
22+
describe('when called with a string', () => {
23+
it('matches a url encoded query string of the same name=value', async () => {
24+
const scope = nock('http://example.test')
25+
.get('/')
26+
.query('foo%5Bbar%5D%3Dhello%20world%21')
27+
.reply()
28+
29+
const { statusCode } = await got(
30+
'http://example.test/?foo%5Bbar%5D%3Dhello%20world%21',
31+
)
32+
33+
expect(statusCode).to.equal(200)
34+
scope.done()
35+
})
36+
})
37+
2238
describe('when called with an object', () => {
2339
it('matches a query string of the same name=value', async () => {
2440
const scope = nock('http://example.test')
@@ -256,8 +272,8 @@ describe('`query()`', () => {
256272
const interceptor = nock('http://example.test').get('/')
257273

258274
expect(() => {
259-
interceptor.query('foo=bar')
260-
}).to.throw(Error, 'Argument Error: foo=bar')
275+
interceptor.query(1)
276+
}).to.throw(Error, 'Argument Error: 1')
261277
})
262278
})
263279

0 commit comments

Comments
 (0)