diff options
author | Samuel Mannehed <samuel@cendio.se> | 2021-09-07 22:06:43 +0200 |
---|---|---|
committer | Samuel Mannehed <samuel@cendio.se> | 2021-09-08 15:37:42 +0200 |
commit | f69d55c02f1d96d52e9929173544e02cfedf4d9b (patch) | |
tree | d45c76f8a63f1714f482dbf9e43865073e287453 | |
parent | 784103761829b7c14579b7e26a18193310a194f4 (diff) | |
download | novnc-f69d55c02f1d96d52e9929173544e02cfedf4d9b.tar.gz |
Fix parsing of query string variables
This space that was added here was added to the parsed value of the
query variable. This broke any comparisons with the value, for example
"myvar=true" resulted in a value of "true ".
This was broken by f796b05e42cfac7044cca9603e59f258605228f3
The commit also adds unit tests for webutil.getConfigVar() that will
detect problems like this in the future.
-rw-r--r-- | app/webutil.js | 2 | ||||
-rw-r--r-- | tests/test.webutil.js | 42 | ||||
-rw-r--r-- | vnc_lite.html | 2 |
3 files changed, 44 insertions, 2 deletions
diff --git a/app/webutil.js b/app/webutil.js index ef23fcb..d42b7f2 100644 --- a/app/webutil.js +++ b/app/webutil.js @@ -32,7 +32,7 @@ export function initLogging(level) { export function getQueryVar(name, defVal) { "use strict"; const re = new RegExp('.*[?&]' + name + '=([^&#]*)'), - match = ''.concat(document.location.href, " ", window.location.hash).match(re); + match = ''.concat(document.location.href, window.location.hash).match(re); if (typeof defVal === 'undefined') { defVal = null; } if (match) { diff --git a/tests/test.webutil.js b/tests/test.webutil.js index 82a9cc6..6681b3c 100644 --- a/tests/test.webutil.js +++ b/tests/test.webutil.js @@ -7,6 +7,48 @@ import * as WebUtil from '../app/webutil.js'; describe('WebUtil', function () { "use strict"; + describe('config variables', function () { + it('should parse query string variables', function () { + // history.pushState() will not cause the browser to attempt loading + // the URL, this is exactly what we want here for the tests. + history.pushState({}, '', "test?myvar=myval"); + expect(WebUtil.getConfigVar("myvar")).to.be.equal("myval"); + }); + it('should return default value when no query match', function () { + history.pushState({}, '', "test?myvar=myval"); + expect(WebUtil.getConfigVar("other", "def")).to.be.equal("def"); + }); + it('should handle no query match and no default value', function () { + history.pushState({}, '', "test?myvar=myval"); + expect(WebUtil.getConfigVar("other")).to.be.equal(null); + }); + it('should parse fragment variables', function () { + history.pushState({}, '', "test#myvar=myval"); + expect(WebUtil.getConfigVar("myvar")).to.be.equal("myval"); + }); + it('should return default value when no fragment match', function () { + history.pushState({}, '', "test#myvar=myval"); + expect(WebUtil.getConfigVar("other", "def")).to.be.equal("def"); + }); + it('should handle no fragment match and no default value', function () { + history.pushState({}, '', "test#myvar=myval"); + expect(WebUtil.getConfigVar("other")).to.be.equal(null); + }); + it('should handle both query and fragment', function () { + history.pushState({}, '', "test?myquery=1#myhash=2"); + expect(WebUtil.getConfigVar("myquery")).to.be.equal("1"); + expect(WebUtil.getConfigVar("myhash")).to.be.equal("2"); + }); + it('should prioritize fragment if both provide same var', function () { + history.pushState({}, '', "test?myvar=1#myvar=2"); + expect(WebUtil.getConfigVar("myvar")).to.be.equal("2"); + }); + }); + + describe('cookies', function () { + // TODO + }); + describe('settings', function () { describe('localStorage', function () { diff --git a/vnc_lite.html b/vnc_lite.html index 1f6e030..8e2f5cb 100644 --- a/vnc_lite.html +++ b/vnc_lite.html @@ -122,7 +122,7 @@ // Note that we use location.href instead of location.search // because Firefox < 53 has a bug w.r.t location.search const re = new RegExp('.*[?&]' + name + '=([^&#]*)'), - match = ''.concat(document.location.href, " ", window.location.hash).match(re); + match = ''.concat(document.location.href, window.location.hash).match(re); if (match) { // We have to decode the URL since want the cleartext value |