diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 44d27367b..2837192b1 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -65,6 +65,20 @@ jobs:
run: |
pytest
+ test-js-unit:
+ name: ๐งช JavaScript unit tests
+ needs: build
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v2
+ - name: ๐ Set up test dependencies
+ run: |
+ npm -g install node-qunit-puppeteer@2.1.0
+ - name: ๐ Run helpers.js test suite
+ run: |
+ node-qunit-puppeteer tests/static/js/test-helpers.html
+ node-qunit-puppeteer tests/static/js/test-client-base.html
+
test-install:
name: ๐งช Installation tests
needs: build
@@ -146,6 +160,7 @@ jobs:
needs:
- pre-commit
- test-unit
+ - test-js-unit
- test-install
- test-e2e
runs-on: ubuntu-latest
diff --git a/src/octoprint/static/js/app/client/base.js b/src/octoprint/static/js/app/client/base.js
index 79870fd86..9bc72ce2f 100644
--- a/src/octoprint/static/js/app/client/base.js
+++ b/src/octoprint/static/js/app/client/base.js
@@ -107,13 +107,14 @@
return url;
};
- OctoPrintClient.prototype.getParsedBaseUrl = function () {
+ OctoPrintClient.prototype.getParsedBaseUrl = function (location) {
if (!this.options.baseurl) return "";
try {
var url = new URL(this.options.baseurl);
} catch (e) {
- var parsed = new URL(window.location);
+ location = location || window.location;
+ var parsed = new URL(location);
var path = this.options.baseurl;
if (!path || path[0] !== "/") {
path = "/" + (path ? path : "");
@@ -135,7 +136,7 @@
if (path.endsWith("/")) {
path = path.substring(0, path.length - 1);
}
- return "_P" + port + "_R" + path.replace(/\//, "|");
+ return "_P" + port + "_R" + path.replace(/\//g, "|");
} else {
return "_P" + port;
}
diff --git a/tests/static/js/test-client-base.html b/tests/static/js/test-client-base.html
new file mode 100644
index 000000000..fa69c3fea
--- /dev/null
+++ b/tests/static/js/test-client-base.html
@@ -0,0 +1,18 @@
+
+
+
+
+ src/octoprint/static/js/app/client/base.js Tests
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/tests/static/js/test-client-base.js b/tests/static/js/test-client-base.js
new file mode 100644
index 000000000..6d39fa39f
--- /dev/null
+++ b/tests/static/js/test-client-base.js
@@ -0,0 +1,110 @@
+_.mixin({sprintf: sprintf, vsprintf: vsprintf});
+
+QUnit.module("getParsedBaseUrl");
+QUnit.cases(
+ (function () {
+ return [
+ {
+ title: "fully qualified baseurl",
+ baseurl: "https://example.com",
+ location: undefined,
+ expected: "https://example.com/"
+ },
+ {
+ title: "fully qualified baseurl with http",
+ baseurl: "http://example.com",
+ location: undefined,
+ expected: "http://example.com/"
+ },
+ {
+ title: "fully qualified baseurl with custom port",
+ baseurl: "https://example.com:5000",
+ location: undefined,
+ expected: "https://example.com:5000/"
+ },
+ {
+ title: "website root",
+ baseurl: "/",
+ location: "https://example.com",
+ expected: "https://example.com/"
+ },
+ {
+ title: "website root with custom port",
+ baseurl: "/",
+ location: "https://example.com:5000",
+ expected: "https://example.com:5000/"
+ },
+ {
+ title: "single level prefix",
+ baseurl: "/octoprint/",
+ location: "https://example.com",
+ expected: "https://example.com/octoprint/"
+ },
+ {
+ title: "multi level prefix",
+ baseurl: "/path/to/octoprint/",
+ location: "https://example.com",
+ expected: "https://example.com/path/to/octoprint/"
+ },
+ {
+ title: "multi level prefix with http and custom port",
+ baseurl: "/path/to/octoprint/",
+ location: "http://example.com:5000",
+ expected: "http://example.com:5000/path/to/octoprint/"
+ },
+ {
+ title: "multi level prefix with https, custom port, no trailing slash",
+ baseurl: "/path/to/octoprint",
+ location: "https://example.com:5001",
+ expected: "https://example.com:5001/path/to/octoprint"
+ }
+ ];
+ })()
+).test("getParsedBaseUrl", function (params, assert) {
+ OctoPrint.options.baseurl = params.baseurl;
+ assert.equal(
+ OctoPrint.getParsedBaseUrl(params.location).toString(),
+ params.expected,
+ "Expected: " + String(params.expected)
+ );
+});
+
+QUnit.module("getCookieSuffix");
+QUnit.cases(
+ (function () {
+ return [
+ {
+ title: "http with default port",
+ baseurl: "http://example.com",
+ expected: "_P80"
+ },
+ {
+ title: "https with default port",
+ baseurl: "https://example.com",
+ expected: "_P443"
+ },
+ {
+ title: "custom port",
+ baseurl: "http://example.com:5000",
+ expected: "_P5000"
+ },
+ {
+ title: "single level prefix",
+ baseurl: "https://example.com/octoprint/",
+ expected: "_P443_R|octoprint"
+ },
+ {
+ title: "multi level prefix",
+ baseurl: "https://example.com/path/to/octoprint",
+ expected: "_P443_R|path|to|octoprint"
+ }
+ ];
+ })()
+).test("getCookieSuffix", function (params, assert) {
+ OctoPrint.options.baseurl = params.baseurl;
+ assert.equal(
+ OctoPrint.getCookieSuffix(),
+ params.expected,
+ "Expected: " + String(params.expected)
+ );
+});