From a2208d1c59ddfdc35392c8581b280691c5b0dcc5 Mon Sep 17 00:00:00 2001 From: "Matt R. Wilson" Date: Mon, 15 Jul 2019 16:02:59 -0600 Subject: [PATCH] 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. --- lib/interceptor.js | 10 +++++++--- tests/test_query.js | 18 ++++++++++-------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/interceptor.js b/lib/interceptor.js index 0afbcc77a..0b579c62c 100644 --- a/lib/interceptor.js +++ b/lib/interceptor.js @@ -492,10 +492,14 @@ Interceptor.prototype.query = function query(queries) { queries instanceof url.URLSearchParams ? queries : Object.entries(queries) for (const [key, value] of entries) { - if (this.queries[key] === undefined) { - const formattedPair = common.formatQueryValue(key, value, strFormattingFn) - this.queries[formattedPair[0]] = formattedPair[1] + const formatted = common.formatQueryValue(key, value, strFormattingFn) + const [formattedKey, formattedValue] = formatted + + if (formattedKey in this.queries) { + throw Error(`${formattedKey} already defined as a query parameter`) } + + this.queries[formattedKey] = formattedValue } return this diff --git a/tests/test_query.js b/tests/test_query.js index 282cadad0..58224bdad 100644 --- a/tests/test_query.js +++ b/tests/test_query.js @@ -121,17 +121,19 @@ test('query() accepts URLSearchParams as input', async t => { scope.done() }) -test('multiple set query keys use the first occurrence', async t => { - const scope = nock('http://example.test') +test('query() throws for duplicate keys', async t => { + const interceptor = nock('http://example.test') .get('/') .query({ foo: 'bar' }) - .query({ foo: 'baz' }) - .reply() - - const { statusCode } = await got('http://example.test?foo=bar') - t.is(statusCode, 200) - scope.done() + t.throws( + () => { + interceptor.query({ foo: 'baz' }) + }, + { + message: 'foo already defined as a query parameter', + } + ) }) test('query() matches a query string that contains special RFC3986 characters', t => {