Skip to content

Commit c16a9a6

Browse files
authored
Fix duplicated port in absolute path (#381)
* Fixed duplicated port in absolute path on non-standard port * Implemented tests
1 parent 165b3f4 commit c16a9a6

File tree

4 files changed

+58
-11
lines changed

4 files changed

+58
-11
lines changed

Resources/js/router.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,14 +294,17 @@ class Router {
294294
});
295295
// Foo-bar!
296296
url = this.context_.base_url + url;
297+
298+
const isPortInHost = host.indexOf(':' + port) > -1 || this.getHost() && this.getHost().indexOf(':' + port) > -1;
299+
297300
if (route.requirements && ("_scheme" in route.requirements) && this.getScheme() != route.requirements["_scheme"]) {
298-
url = route.requirements["_scheme"] + "://" + (host || this.getHost()) + ('' === port ? '' : ':' + port) + url;
301+
url = route.requirements["_scheme"] + "://" + (host || this.getHost()) + (isPortInHost || '' === port ? '' : ':' + port) + url;
299302
} else if ("undefined" !== typeof route.schemes && "undefined" !== typeof route.schemes[0] && this.getScheme() !== route.schemes[0]) {
300-
url = route.schemes[0] + "://" + (host || this.getHost()) + ('' === port ? '' : ':' + port) + url;
301-
} else if (host && this.getHost() !== host + ('' === port ? '' : ':' + port)) {
302-
url = this.getScheme() + "://" + host + ('' === port ? '' : ':' + port) + url;
303+
url = route.schemes[0] + "://" + (host || this.getHost()) + (isPortInHost || '' === port ? '' : ':' + port) + url;
304+
} else if (host && this.getHost() !== host + (isPortInHost || '' === port ? '' : ':' + port)) {
305+
url = this.getScheme() + "://" + host + (isPortInHost || '' === port ? '' : ':' + port) + url;
303306
} else if (absolute === true) {
304-
url = this.getScheme() + "://" + this.getHost() + ('' === port ? '' : ':' + port) + url;
307+
url = this.getScheme() + "://" + this.getHost() + (isPortInHost || '' === port ? '' : ':' + port) + url;
305308
}
306309

307310
if (Object.keys(unusedParams).length > 0) {

Resources/js/router.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,19 @@ function testGenerateUsesAbsoluteUrlWithGivenPort() {
196196
assertEquals('http://localhost:8000/foo/bar', router.generate('homepage', [], true));
197197
}
198198

199+
function testGenerateUsesAbsoluteUrlWithGivenPortAndHostWithPort() {
200+
var router = new fos.Router({base_url: '/foo', host: "localhost:8080", scheme: "http", port: "8080"}, {
201+
homepage: {
202+
tokens: [['text', '/bar']],
203+
defaults: {},
204+
requirements: {},
205+
hosttokens: []
206+
}
207+
});
208+
209+
assertEquals('http://localhost:8080/foo/bar', router.generate('homepage', [], true));
210+
}
211+
199212
function testGenerateUsesAbsoluteUrlWhenSchemeRequirementGiven() {
200213
var router = new fos.Router({base_url: '/foo', host: "localhost", scheme: "http"}, {
201214
homepage: {
@@ -222,6 +235,19 @@ function testGenerateUsesAbsoluteUrlWithGivenPortWhenSchemeRequirementGiven() {
222235
assertEquals('http://localhost:8080/foo/bar', router.generate('homepage', [], true));
223236
}
224237

238+
function testGenerateUsesAbsoluteUrlWithGivenPortWhenSchemeRequirementAndHostWithPortGiven() {
239+
var router = new fos.Router({base_url: '/foo', host: "localhost:8080", scheme: "http", port: "8080"}, {
240+
homepage: {
241+
tokens: [['text', '/bar']],
242+
defaults: {},
243+
requirements: {"_scheme": "http"},
244+
hosttokens: []
245+
}
246+
});
247+
248+
assertEquals('http://localhost:8080/foo/bar', router.generate('homepage', [], true));
249+
}
250+
225251
function testGenerateUsesAbsoluteUrlWhenSchemeGiven() {
226252
var router = new fos.Router({base_url: '/foo', host: "localhost", scheme: "http"}, {
227253
homepage: {
@@ -252,6 +278,21 @@ function testGenerateUsesAbsoluteUrlWithGivenPortWhenSchemeGiven() {
252278
assertEquals('http://localhost:1234/foo/bar', router.generate('homepage', [], true));
253279
}
254280

281+
function testGenerateUsesAbsoluteUrlWithGivenPortWhenSchemeAndHostWithPortGiven() {
282+
var router = new fos.Router({base_url: '/foo', host: "localhost:8080", scheme: "http", port:"8080"}, {
283+
homepage: {
284+
tokens: [['text', '/bar']],
285+
defaults: {},
286+
requirements: {},
287+
hosttokens: [],
288+
schemes: ['http'],
289+
methods: []
290+
}
291+
});
292+
293+
assertEquals('http://localhost:8080/foo/bar', router.generate('homepage', [], true));
294+
}
295+
255296
function testGenerateWithOptionalTrailingParam() {
256297
var router = new fos.Router({base_url: ''}, {
257298
posts: {

Resources/public/js/router.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,14 +367,17 @@ var Router = function () {
367367
});
368368
// Foo-bar!
369369
url = this.context_.base_url + url;
370+
371+
var isPortInHost = host.indexOf(':' + port) > -1 || this.getHost() && this.getHost().indexOf(':' + port) > -1;
372+
370373
if (route.requirements && "_scheme" in route.requirements && this.getScheme() != route.requirements["_scheme"]) {
371-
url = route.requirements["_scheme"] + "://" + (host || this.getHost()) + ('' === port ? '' : ':' + port) + url;
374+
url = route.requirements["_scheme"] + "://" + (host || this.getHost()) + (isPortInHost || '' === port ? '' : ':' + port) + url;
372375
} else if ("undefined" !== typeof route.schemes && "undefined" !== typeof route.schemes[0] && this.getScheme() !== route.schemes[0]) {
373-
url = route.schemes[0] + "://" + (host || this.getHost()) + ('' === port ? '' : ':' + port) + url;
374-
} else if (host && this.getHost() !== host + ('' === port ? '' : ':' + port)) {
375-
url = this.getScheme() + "://" + host + ('' === port ? '' : ':' + port) + url;
376+
url = route.schemes[0] + "://" + (host || this.getHost()) + (isPortInHost || '' === port ? '' : ':' + port) + url;
377+
} else if (host && this.getHost() !== host + (isPortInHost || '' === port ? '' : ':' + port)) {
378+
url = this.getScheme() + "://" + host + (isPortInHost || '' === port ? '' : ':' + port) + url;
376379
} else if (absolute === true) {
377-
url = this.getScheme() + "://" + this.getHost() + ('' === port ? '' : ':' + port) + url;
380+
url = this.getScheme() + "://" + this.getHost() + (isPortInHost || '' === port ? '' : ':' + port) + url;
378381
}
379382

380383
if (Object.keys(unusedParams).length > 0) {

Resources/public/js/router.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)