Skip to content

Commit a2208d1

Browse files
mastermattgr2m
authored andcommitted
feat(interceptor): duplicate query keys throw
Resolves #1623 BREAKING CHANGE: Providing a duplicate search parameter to the `query` method throws an error instead of ignoring subsequent values.
1 parent 880224a commit a2208d1

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

lib/interceptor.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,14 @@ Interceptor.prototype.query = function query(queries) {
492492
queries instanceof url.URLSearchParams ? queries : Object.entries(queries)
493493

494494
for (const [key, value] of entries) {
495-
if (this.queries[key] === undefined) {
496-
const formattedPair = common.formatQueryValue(key, value, strFormattingFn)
497-
this.queries[formattedPair[0]] = formattedPair[1]
495+
const formatted = common.formatQueryValue(key, value, strFormattingFn)
496+
const [formattedKey, formattedValue] = formatted
497+
498+
if (formattedKey in this.queries) {
499+
throw Error(`${formattedKey} already defined as a query parameter`)
498500
}
501+
502+
this.queries[formattedKey] = formattedValue
499503
}
500504

501505
return this

tests/test_query.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,19 @@ test('query() accepts URLSearchParams as input', async t => {
121121
scope.done()
122122
})
123123

124-
test('multiple set query keys use the first occurrence', async t => {
125-
const scope = nock('http://example.test')
124+
test('query() throws for duplicate keys', async t => {
125+
const interceptor = nock('http://example.test')
126126
.get('/')
127127
.query({ foo: 'bar' })
128-
.query({ foo: 'baz' })
129-
.reply()
130-
131-
const { statusCode } = await got('http://example.test?foo=bar')
132128

133-
t.is(statusCode, 200)
134-
scope.done()
129+
t.throws(
130+
() => {
131+
interceptor.query({ foo: 'baz' })
132+
},
133+
{
134+
message: 'foo already defined as a query parameter',
135+
}
136+
)
135137
})
136138

137139
test('query() matches a query string that contains special RFC3986 characters', t => {

0 commit comments

Comments
 (0)