Skip to content

Commit d9aecdc

Browse files
authored
Fixed issue with creating absolute instead of relative path (#391)
* Fixed issue with creating absolute instead of relative path * Implemented test
1 parent 7dfd216 commit d9aecdc

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

Resources/js/router.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -295,16 +295,18 @@ class Router {
295295
// Foo-bar!
296296
url = this.context_.base_url + url;
297297

298-
const isPortInHost = host.indexOf(':' + port) > -1 || this.getHost() && this.getHost().indexOf(':' + port) > -1;
299-
300298
if (route.requirements && ("_scheme" in route.requirements) && this.getScheme() != route.requirements["_scheme"]) {
301-
url = route.requirements["_scheme"] + "://" + (host || this.getHost()) + (isPortInHost || '' === port ? '' : ':' + port) + url;
299+
const currentHost = host || this.getHost();
300+
301+
url = route.requirements["_scheme"] + "://" + currentHost + (currentHost.indexOf(':' + port) > -1 || '' === port ? '' : ':' + port) + url;
302302
} else if ("undefined" !== typeof route.schemes && "undefined" !== typeof route.schemes[0] && this.getScheme() !== route.schemes[0]) {
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;
303+
const currentHost = host || this.getHost();
304+
305+
url = route.schemes[0] + "://" + currentHost + (currentHost.indexOf(':' + port) > -1 || '' === port ? '' : ':' + port) + url;
306+
} else if (host && this.getHost() !== host + (host.indexOf(':' + port) > -1 || '' === port ? '' : ':' + port)) {
307+
url = this.getScheme() + "://" + host + (host.indexOf(':' + port) > -1 || '' === port ? '' : ':' + port) + url;
306308
} else if (absolute === true) {
307-
url = this.getScheme() + "://" + this.getHost() + (isPortInHost || '' === port ? '' : ':' + port) + url;
309+
url = this.getScheme() + "://" + this.getHost() + (this.getHost().indexOf(':' + port) > -1 || '' === port ? '' : ':' + port) + url;
308310
}
309311

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

Resources/js/router.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,3 +519,18 @@ function testGenerateWithPort() {
519519

520520
assertEquals('http://api.localhost:443/foo/bar', router.generate('homepage'));
521521
}
522+
523+
// Regression test for issue #384 (https://github.com/FriendsOfSymfony/FOSJsRoutingBundle/issues/384)
524+
function testGenerateWithPortInHost() {
525+
var router = new fos.Router({base_url: '', host: "my-host.loc:81", scheme: "http", port: 81}, {
526+
homepage: {
527+
tokens: [['text', "\/foo\/"]],
528+
defaults: [],
529+
requirements: {},
530+
hosttokens: [["text", "my-host.loc"]],
531+
methods: ["GET", "POST"],
532+
}
533+
});
534+
535+
assertEquals('/foo/', router.generate('homepage'));
536+
}

Resources/public/js/router.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,16 +368,18 @@ var Router = function () {
368368
// Foo-bar!
369369
url = this.context_.base_url + url;
370370

371-
var isPortInHost = host.indexOf(':' + port) > -1 || this.getHost() && this.getHost().indexOf(':' + port) > -1;
372-
373371
if (route.requirements && "_scheme" in route.requirements && this.getScheme() != route.requirements["_scheme"]) {
374-
url = route.requirements["_scheme"] + "://" + (host || this.getHost()) + (isPortInHost || '' === port ? '' : ':' + port) + url;
372+
var currentHost = host || this.getHost();
373+
374+
url = route.requirements["_scheme"] + "://" + currentHost + (currentHost.indexOf(':' + port) > -1 || '' === port ? '' : ':' + port) + url;
375375
} else if ("undefined" !== typeof route.schemes && "undefined" !== typeof route.schemes[0] && this.getScheme() !== route.schemes[0]) {
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;
376+
var _currentHost = host || this.getHost();
377+
378+
url = route.schemes[0] + "://" + _currentHost + (_currentHost.indexOf(':' + port) > -1 || '' === port ? '' : ':' + port) + url;
379+
} else if (host && this.getHost() !== host + (host.indexOf(':' + port) > -1 || '' === port ? '' : ':' + port)) {
380+
url = this.getScheme() + "://" + host + (host.indexOf(':' + port) > -1 || '' === port ? '' : ':' + port) + url;
379381
} else if (absolute === true) {
380-
url = this.getScheme() + "://" + this.getHost() + (isPortInHost || '' === port ? '' : ':' + port) + url;
382+
url = this.getScheme() + "://" + this.getHost() + (this.getHost().indexOf(':' + port) > -1 || '' === port ? '' : ':' + port) + url;
381383
}
382384

383385
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)