You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by ki...@apache.org on 2022/02/05 19:29:55 UTC

[jena] branch main updated: JENA-2271: Do not remove slashes from graph name (with tests)

This is an automated email from the ASF dual-hosted git repository.

kinow pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/jena.git


The following commit(s) were added to refs/heads/main by this push:
     new 0a9e1de  JENA-2271: Do not remove slashes from graph name (with tests)
0a9e1de is described below

commit 0a9e1de9620aafe3caee721fa006bb41e04d661c
Author: Bruno P. Kinoshita <ki...@apache.org>
AuthorDate: Sat Feb 5 14:37:42 2022 +1300

    JENA-2271: Do not remove slashes from graph name (with tests)
---
 .../jena-fuseki-ui/src/services/fuseki.service.js  |  9 ++++-
 .../tests/unit/services/fuseki.service.spec.js     | 44 ++++++++++++++++++++++
 2 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/jena-fuseki2/jena-fuseki-ui/src/services/fuseki.service.js b/jena-fuseki2/jena-fuseki-ui/src/services/fuseki.service.js
index e1a215e..60a332d 100644
--- a/jena-fuseki2/jena-fuseki-ui/src/services/fuseki.service.js
+++ b/jena-fuseki2/jena-fuseki-ui/src/services/fuseki.service.js
@@ -48,8 +48,13 @@ class FusekiService {
    * @return {string} a new route URL that includes any pathname in the URL
    */
   getFusekiUrl (url) {
-    // modified version from: https://stackoverflow.com/a/24381515
-    return `${this.pathname}/${url}`.replace(/(\/)\/+/g, '$1')
+    // remove leading `/`'s
+    let normalizedUrl = url
+    while (normalizedUrl.startsWith('/') && normalizedUrl.length > 0) {
+      normalizedUrl = normalizedUrl.slice(1)
+    }
+    const pathname = this.pathname.endsWith('/') ? this.pathname : `${this.pathname}/`
+    return `${pathname}${normalizedUrl}`
   }
 
   async getServerData () {
diff --git a/jena-fuseki2/jena-fuseki-ui/tests/unit/services/fuseki.service.spec.js b/jena-fuseki2/jena-fuseki-ui/tests/unit/services/fuseki.service.spec.js
index ed8b121..d8b5d18 100644
--- a/jena-fuseki2/jena-fuseki-ui/tests/unit/services/fuseki.service.spec.js
+++ b/jena-fuseki2/jena-fuseki-ui/tests/unit/services/fuseki.service.spec.js
@@ -314,4 +314,48 @@ describe('FusekiService', () => {
     expect(stub.called).to.equal(true)
     stub.restore()
   })
+  it('creates a valid URL when using a URL graph name', () => {
+    // pathname is managed by the browser, we don't need to test if it has
+    // multiple `/`'s... in case it does, the only way to call this code
+    // is if the server accepted the URL like that, so it should be OK to
+    // keep using it as it is.
+    const tests = [
+      {
+        pathname: '/',
+        url: '/ds/data?graph=http://example.com',
+        expected: '/ds/data?graph=http://example.com'
+      },
+      {
+        pathname: '/',
+        url: '//ds/data?graph=http://example.com',
+        expected: '/ds/data?graph=http://example.com'
+      },
+      {
+        pathname: '',
+        url: '//ds/data?graph=http://example.com',
+        expected: '/ds/data?graph=http://example.com'
+      },
+      {
+        pathname: '',
+        url: '',
+        expected: '/'
+      },
+      {
+        pathname: '/',
+        url: '/ds/data?graph=',
+        expected: '/ds/data?graph='
+      },
+      {
+        pathname: '/',
+        url: '/ds/data?graph=default',
+        expected: '/ds/data?graph=default'
+      }
+    ]
+    const originalPathname = fusekiService.pathname
+    for (const test of tests) {
+      fusekiService.pathname = test.pathname
+      expect(fusekiService.getFusekiUrl(test.url)).to.equal(test.expected)
+    }
+    fusekiService.pathname = originalPathname
+  })
 })