Skip to content

Commit c121cce

Browse files
committed
Include ports in URL generation.
1 parent a704e96 commit c121cce

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

lib/resource-mapper.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ class ResourceMapper {
1717

1818
// If the host needs to be replaced on every call, pre-split the root URL
1919
if (includeHost) {
20-
const { protocol, pathname } = URL.parse(rootUrl)
20+
const { protocol, port, pathname } = URL.parse(rootUrl)
2121
this._protocol = protocol
22+
this._port = port === null ? '' : `:${port}`
2223
this._rootUrl = this._removeTrailingSlash(pathname)
2324
}
2425
}
@@ -64,12 +65,13 @@ class ResourceMapper {
6465

6566
// Gets the base file path for the given hostname
6667
getBasePath (hostname) {
67-
return this._includeHost ? `${this._rootPath}/${hostname}` : this._rootPath
68+
return !this._includeHost ? this._rootPath : `${this._rootPath}/${hostname}`
6869
}
6970

7071
// Gets the base URL for the given hostname
7172
getBaseUrl (hostname) {
72-
return this._includeHost ? `${this._protocol}//${hostname}${this._rootUrl}` : this._rootUrl
73+
return !this._includeHost ? this._rootUrl
74+
: `${this._protocol}//${hostname}${this._port}${this._rootUrl}`
7375
}
7476

7577
// Determine the full file path corresponding to a URL

test/unit/resource-mapper-test.js

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ describe('ResourceMapper', () => {
329329
})
330330

331331
describe('A ResourceMapper instance for a multi-host setup with a subfolder root URL', () => {
332-
const rootUrl = 'http://localhost/foo/bar/'
332+
const rootUrl = 'https://localhost/foo/bar/'
333333
const mapper = new ResourceMapper({ rootUrl, rootPath, includeHost: true })
334334

335335
itMapsFile(mapper, 'a file on a host',
@@ -338,7 +338,61 @@ describe('ResourceMapper', () => {
338338
hostname: 'example.org'
339339
},
340340
{
341-
url: 'http://example.org/foo/bar/space/foo.html',
341+
url: 'https://example.org/foo/bar/space/foo.html',
342+
contentType: 'text/html'
343+
})
344+
})
345+
346+
describe('A ResourceMapper instance for an HTTP host with non-default port', () => {
347+
const mapper = new ResourceMapper({ rootUrl: 'http://localhost:81/', rootPath })
348+
349+
itMapsFile(mapper, 'a file with the port',
350+
{
351+
path: `${rootPath}space/foo.html`
352+
},
353+
{
354+
url: 'http://localhost:81/space/foo.html',
355+
contentType: 'text/html'
356+
})
357+
})
358+
359+
describe('A ResourceMapper instance for an HTTP host with non-default port in a multi-host setup', () => {
360+
const mapper = new ResourceMapper({ rootUrl: 'http://localhost:81/', rootPath, includeHost: true })
361+
362+
itMapsFile(mapper, 'a file with the port',
363+
{
364+
path: `${rootPath}space/foo.html`,
365+
hostname: 'example.org'
366+
},
367+
{
368+
url: 'http://example.org:81/space/foo.html',
369+
contentType: 'text/html'
370+
})
371+
})
372+
373+
describe('A ResourceMapper instance for an HTTPS host with non-default port', () => {
374+
const mapper = new ResourceMapper({ rootUrl: 'https://localhost:81/', rootPath })
375+
376+
itMapsFile(mapper, 'a file with the port',
377+
{
378+
path: `${rootPath}space/foo.html`
379+
},
380+
{
381+
url: 'https://localhost:81/space/foo.html',
382+
contentType: 'text/html'
383+
})
384+
})
385+
386+
describe('A ResourceMapper instance for an HTTPS host with non-default port in a multi-host setup', () => {
387+
const mapper = new ResourceMapper({ rootUrl: 'https://localhost:81/', rootPath, includeHost: true })
388+
389+
itMapsFile(mapper, 'a file with the port',
390+
{
391+
path: `${rootPath}space/foo.html`,
392+
hostname: 'example.org'
393+
},
394+
{
395+
url: 'https://example.org:81/space/foo.html',
342396
contentType: 'text/html'
343397
})
344398
})

0 commit comments

Comments
 (0)