Skip to content

Commit

Permalink
Correctly handle query strings that contain = (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
genomics-geek authored and sindresorhus committed Mar 27, 2019
1 parent a429c8e commit 78e321c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion index.js
@@ -1,6 +1,7 @@
'use strict';
const strictUriEncode = require('strict-uri-encode');
const decodeComponent = require('decode-uri-component');
const splitOnFirst = require('split-on-first');

function encoderForArrayFormat(options) {
switch (options.arrayFormat) {
Expand Down Expand Up @@ -182,7 +183,7 @@ function parse(input, options) {
}

for (const param of input.split('&')) {
let [key, value] = param.replace(/\+/g, ' ').split('=');
let [key, value] = splitOnFirst(param.replace(/\+/g, ' '), '=');

// Missing `=` should be `null`:
// http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -42,6 +42,7 @@
"ava": "^1.3.1",
"deep-equal": "^1.0.1",
"fast-check": "^1.5.0",
"split-on-first": "^1.0.0",

This comment has been minimized.

Copy link
@wartab

wartab Mar 27, 2019

This should be a dependency, not a devDependency

This comment has been minimized.

Copy link
@wartab

wartab Mar 27, 2019

nvm saw you fixed it just now :)

"tsd-check": "^0.3.0",
"xo": "^0.24.0"
}
Expand Down
5 changes: 5 additions & 0 deletions test/parse-url.js
Expand Up @@ -13,6 +13,11 @@ test('handles strings not containing query string', t => {
t.deepEqual(m.parseUrl(''), {url: '', query: {}});
});

test('handles strings with query string that contain =', t => {
t.deepEqual(m.parseUrl('https://foo.bar?foo=baz=bar&foo=baz#top'), {url: 'https://foo.bar', query: {foo: ['baz=bar', 'baz']}});
t.deepEqual(m.parseUrl('https://foo.bar?foo=bar=&foo=baz='), {url: 'https://foo.bar', query: {foo: ['bar=', 'baz=']}});
});

test('throws for invalid values', t => {
t.throws(() => {
m.parseUrl(null);
Expand Down
5 changes: 5 additions & 0 deletions test/parse.js
Expand Up @@ -137,6 +137,11 @@ test('query strings having indexed arrays and format option as `index`', t => {
}), {foo: ['bar', 'baz']});
});

test('query strings having = within parameters (i.e. GraphQL IDs)', t => {
t.deepEqual(m.parse('foo=bar=&foo=ba=z=', {
}), {foo: ['bar=', 'ba=z=']});
});

test('query strings having ordered index arrays and format option as `index`', t => {
t.deepEqual(m.parse('foo[1]=bar&foo[0]=baz&foo[3]=one&foo[2]=two', {
arrayFormat: 'index'
Expand Down

0 comments on commit 78e321c

Please sign in to comment.