Skip to content

Commit

Permalink
Fix file to URL resolving for multi-host.
Browse files Browse the repository at this point in the history
* Patch fix

* Remove console log

* Fix Ruben Comments

* replace backslashes to handle windows routes

* Fix tests

* Reverted original test back because it is async

* More specific test to check for error

* Correct error comaparison
  • Loading branch information
jaxoncreed authored and RubenVerborgh committed Jun 14, 2019
1 parent b7b5bff commit 6dbc45b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
9 changes: 8 additions & 1 deletion lib/resource-mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,20 @@ class ResourceMapper {
// Maps a given server file to a URL
async mapFileToUrl ({ path, hostname }) {
// Remove the root path if specified
path = path.replace(/\\/g, '/')
if (path.startsWith(this._rootPath)) {
path = path.substring(this._rootPath.length)
}
if (this._includeHost) {
if (!path.startsWith(`/${hostname}/`)) {
throw new Error(`Path must start with hostname (/${hostname})`)
}
path = path.substring(hostname.length + 1)
}

// Determine the URL by chopping off everything after the dollar sign
const pathname = this._removeDollarExtension(path)
const url = `${this.resolveUrl(hostname)}${encodeURI(pathname.replace(/\\/g, '/'))}`
const url = `${this.resolveUrl(hostname)}${encodeURI(pathname)}`
return { url, contentType: this._getContentTypeByExtension(path) }
}

Expand Down
10 changes: 3 additions & 7 deletions test/resources/accounts-scenario/alice/profile/card$.ttl
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
@prefix : <#>.
@prefix acl: <http://www.w3.org/ns/auth/acl#>.
@prefix c: <https://localhost:7000/profile/card#>.
@prefix c0: <https://localhost:8443/profile/card#>.

c:me
acl:trustedApp
[ acl:mode acl:Read, acl:Write; acl:origin <https://app.example.com> ].
c0:me
:me
acl:trustedApp
[
acl:mode acl:Append, acl:Control, acl:Read, acl:Write;
acl:origin <https://trusted.app>
].
],
[ acl:mode acl:Read, acl:Write; acl:origin <https://app.example.com> ].
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
@prefix : <#>.
@prefix acl: <http://www.w3.org/ns/auth/acl#>.
@prefix c: <https://localhost:7010/profile/card#>.
@prefix c0: <https://localhost:8443/profile/card#>.

c:me
acl:trustedApp
[ acl:mode acl:Read, acl:Write; acl:origin <https://app.example.com> ].
c0:me
:me
acl:trustedApp
[
acl:mode acl:Append, acl:Control, acl:Read, acl:Write;
acl:origin <https://trusted.app>
].
],
[ acl:mode acl:Read, acl:Write; acl:origin <https://app.example.com> ].
29 changes: 21 additions & 8 deletions test/unit/resource-mapper-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ describe('ResourceMapper', () => {

itMapsFile(mapper, 'a file on a host',
{
path: `${rootPath}space/foo.html`,
path: `${rootPath}example.org/space/foo.html`,
hostname: 'example.org'
},
{
Expand All @@ -550,7 +550,7 @@ describe('ResourceMapper', () => {

itMapsFile(mapper, 'a file on a host',
{
path: `${rootPath}space/foo.html`,
path: `${rootPath}example.org/space/foo.html`,
hostname: 'example.org'
},
{
Expand All @@ -564,10 +564,11 @@ describe('ResourceMapper', () => {

itMapsFile(mapper, 'a file with the port',
{
path: `${rootPath}space/foo.html`
path: `${rootPath}example.org/space/foo.html`,
hostname: 'example.org'
},
{
url: 'http://localhost:81/space/foo.html',
url: 'http://localhost:81/example.org/space/foo.html',
contentType: 'text/html'
})
})
Expand All @@ -577,7 +578,7 @@ describe('ResourceMapper', () => {

itMapsFile(mapper, 'a file with the port',
{
path: `${rootPath}space/foo.html`,
path: `${rootPath}example.org/space/foo.html`,
hostname: 'example.org'
},
{
Expand All @@ -591,10 +592,11 @@ describe('ResourceMapper', () => {

itMapsFile(mapper, 'a file with the port',
{
path: `${rootPath}space/foo.html`
path: `${rootPath}example.org/space/foo.html`,
hostname: 'example.org'
},
{
url: 'https://localhost:81/space/foo.html',
url: 'https://localhost:81/example.org/space/foo.html',
contentType: 'text/html'
})
})
Expand All @@ -604,14 +606,25 @@ describe('ResourceMapper', () => {

itMapsFile(mapper, 'a file with the port',
{
path: `${rootPath}space/foo.html`,
path: `${rootPath}example.org/space/foo.html`,
hostname: 'example.org'
},
{
url: 'https://example.org:81/space/foo.html',
contentType: 'text/html'
})
})

describe('A ResourceMapper instance for an HTTPS host with non-default port in a multi-host setup', () => {
const mapper = new ResourceMapper({ rootUrl: 'https://localhost:81/', rootPath, includeHost: true })

it('throws an error when there is an improper file path', () => {
return expect(mapper.mapFileToUrl({
path: `${rootPath}example.orgspace/foo.html`,
hostname: 'example.org'
})).to.be.rejectedWith(Error, 'Path must start with hostname (/example.org)')
})
})
})

function asserter (assert) {
Expand Down

0 comments on commit 6dbc45b

Please sign in to comment.