From 4162fa8b2ddaf6a3c5b52162b03629118236847f Mon Sep 17 00:00:00 2001 From: Michael Solomon Date: Sat, 17 Feb 2024 20:38:55 +0200 Subject: [PATCH] fix: support literal query string (#2590) --- lib/interceptor.js | 2 +- tests/got/test_query.js | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/interceptor.js b/lib/interceptor.js index 67ef8b946..aebbdad14 100644 --- a/lib/interceptor.js +++ b/lib/interceptor.js @@ -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()) diff --git a/tests/got/test_query.js b/tests/got/test_query.js index 196b14783..336a8dbc9 100644 --- a/tests/got/test_query.js +++ b/tests/got/test_query.js @@ -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') @@ -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') }) })