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

feat(interceptor): duplicate query keys throw #1626

Merged
merged 1 commit into from
Jul 15, 2019
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
10 changes: 7 additions & 3 deletions lib/interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 10 additions & 8 deletions tests/test_query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down