From 64b16715cdc468bc38a7e8efb038ead10f64988d Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 7 Sep 2018 18:02:58 -0400 Subject: [PATCH] handle value-less query string parameters - fixes #426 --- src/runtime/index.ts | 4 ++-- test/app/routes/index.html | 1 + test/common/test.js | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/runtime/index.ts b/src/runtime/index.ts index 9530604..9e07bad 100644 --- a/src/runtime/index.ts +++ b/src/runtime/index.ts @@ -66,8 +66,8 @@ function select_route(url: URL): Target { const query: Record = {}; if (url.search.length > 0) { url.search.slice(1).split('&').forEach(searchParam => { - const [, key, value] = /([^=]+)=(.*)/.exec(searchParam); - query[key] = value ? decodeURIComponent(value.replace(/\+/g, ' ')) : true; + const [, key, value] = /([^=]+)(?:=(.*))?/.exec(searchParam); + query[key] = decodeURIComponent((value || '').replace(/\+/g, ' ')); }); } return { url, path, page, match, query }; diff --git a/test/app/routes/index.html b/test/app/routes/index.html index 3b889ef..65c7e59 100644 --- a/test/app/routes/index.html +++ b/test/app/routes/index.html @@ -16,6 +16,7 @@ blog const echo/page/encöded?message=hëllö+wörld +echo/page/empty?message
diff --git a/test/common/test.js b/test/common/test.js index 8a61b59..f149911 100644 --- a/test/common/test.js +++ b/test/common/test.js @@ -772,6 +772,24 @@ function run({ mode, basepath = '' }) { }); }); + it('accepts value-less query string parameter on server', () => { + return nightmare.goto(`${base}/echo/page/empty?message`) + .page.title() + .then(title => { + assert.equal(title, 'empty ()'); + }); + }); + + it('accepts value-less query string parameter on client', () => { + return nightmare.goto(base).init() + .click('a[href="echo/page/empty?message"]') + .wait(100) + .page.title() + .then(title => { + assert.equal(title, 'empty ()'); + }); + }); + it('encodes req.params for server routes', () => { return nightmare.goto(`${base}/echo/server-route/encöded`) .page.title()