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: support literal query string #2590

Merged
merged 3 commits into from Feb 17, 2024
Merged
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
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