Skip to content

Commit

Permalink
Fix duplicated port in absolute path (#381)
Browse files Browse the repository at this point in the history
* Fixed duplicated port in absolute path on non-standard port

* Implemented tests
  • Loading branch information
dew326 authored Apr 15, 2020
1 parent 165b3f4 commit c16a9a6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
13 changes: 8 additions & 5 deletions Resources/js/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,17 @@ class Router {
});
// Foo-bar!
url = this.context_.base_url + url;

const isPortInHost = host.indexOf(':' + port) > -1 || this.getHost() && this.getHost().indexOf(':' + port) > -1;

if (route.requirements && ("_scheme" in route.requirements) && this.getScheme() != route.requirements["_scheme"]) {
url = route.requirements["_scheme"] + "://" + (host || this.getHost()) + ('' === port ? '' : ':' + port) + url;
url = route.requirements["_scheme"] + "://" + (host || this.getHost()) + (isPortInHost || '' === port ? '' : ':' + port) + url;
} else if ("undefined" !== typeof route.schemes && "undefined" !== typeof route.schemes[0] && this.getScheme() !== route.schemes[0]) {
url = route.schemes[0] + "://" + (host || this.getHost()) + ('' === port ? '' : ':' + port) + url;
} else if (host && this.getHost() !== host + ('' === port ? '' : ':' + port)) {
url = this.getScheme() + "://" + host + ('' === port ? '' : ':' + port) + url;
url = route.schemes[0] + "://" + (host || this.getHost()) + (isPortInHost || '' === port ? '' : ':' + port) + url;
} else if (host && this.getHost() !== host + (isPortInHost || '' === port ? '' : ':' + port)) {
url = this.getScheme() + "://" + host + (isPortInHost || '' === port ? '' : ':' + port) + url;
} else if (absolute === true) {
url = this.getScheme() + "://" + this.getHost() + ('' === port ? '' : ':' + port) + url;
url = this.getScheme() + "://" + this.getHost() + (isPortInHost || '' === port ? '' : ':' + port) + url;
}

if (Object.keys(unusedParams).length > 0) {
Expand Down
41 changes: 41 additions & 0 deletions Resources/js/router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,19 @@ function testGenerateUsesAbsoluteUrlWithGivenPort() {
assertEquals('http://localhost:8000/foo/bar', router.generate('homepage', [], true));
}

function testGenerateUsesAbsoluteUrlWithGivenPortAndHostWithPort() {
var router = new fos.Router({base_url: '/foo', host: "localhost:8080", scheme: "http", port: "8080"}, {
homepage: {
tokens: [['text', '/bar']],
defaults: {},
requirements: {},
hosttokens: []
}
});

assertEquals('http://localhost:8080/foo/bar', router.generate('homepage', [], true));
}

function testGenerateUsesAbsoluteUrlWhenSchemeRequirementGiven() {
var router = new fos.Router({base_url: '/foo', host: "localhost", scheme: "http"}, {
homepage: {
Expand All @@ -222,6 +235,19 @@ function testGenerateUsesAbsoluteUrlWithGivenPortWhenSchemeRequirementGiven() {
assertEquals('http://localhost:8080/foo/bar', router.generate('homepage', [], true));
}

function testGenerateUsesAbsoluteUrlWithGivenPortWhenSchemeRequirementAndHostWithPortGiven() {
var router = new fos.Router({base_url: '/foo', host: "localhost:8080", scheme: "http", port: "8080"}, {
homepage: {
tokens: [['text', '/bar']],
defaults: {},
requirements: {"_scheme": "http"},
hosttokens: []
}
});

assertEquals('http://localhost:8080/foo/bar', router.generate('homepage', [], true));
}

function testGenerateUsesAbsoluteUrlWhenSchemeGiven() {
var router = new fos.Router({base_url: '/foo', host: "localhost", scheme: "http"}, {
homepage: {
Expand Down Expand Up @@ -252,6 +278,21 @@ function testGenerateUsesAbsoluteUrlWithGivenPortWhenSchemeGiven() {
assertEquals('http://localhost:1234/foo/bar', router.generate('homepage', [], true));
}

function testGenerateUsesAbsoluteUrlWithGivenPortWhenSchemeAndHostWithPortGiven() {
var router = new fos.Router({base_url: '/foo', host: "localhost:8080", scheme: "http", port:"8080"}, {
homepage: {
tokens: [['text', '/bar']],
defaults: {},
requirements: {},
hosttokens: [],
schemes: ['http'],
methods: []
}
});

assertEquals('http://localhost:8080/foo/bar', router.generate('homepage', [], true));
}

function testGenerateWithOptionalTrailingParam() {
var router = new fos.Router({base_url: ''}, {
posts: {
Expand Down
13 changes: 8 additions & 5 deletions Resources/public/js/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,17 @@ var Router = function () {
});
// Foo-bar!
url = this.context_.base_url + url;

var isPortInHost = host.indexOf(':' + port) > -1 || this.getHost() && this.getHost().indexOf(':' + port) > -1;

if (route.requirements && "_scheme" in route.requirements && this.getScheme() != route.requirements["_scheme"]) {
url = route.requirements["_scheme"] + "://" + (host || this.getHost()) + ('' === port ? '' : ':' + port) + url;
url = route.requirements["_scheme"] + "://" + (host || this.getHost()) + (isPortInHost || '' === port ? '' : ':' + port) + url;
} else if ("undefined" !== typeof route.schemes && "undefined" !== typeof route.schemes[0] && this.getScheme() !== route.schemes[0]) {
url = route.schemes[0] + "://" + (host || this.getHost()) + ('' === port ? '' : ':' + port) + url;
} else if (host && this.getHost() !== host + ('' === port ? '' : ':' + port)) {
url = this.getScheme() + "://" + host + ('' === port ? '' : ':' + port) + url;
url = route.schemes[0] + "://" + (host || this.getHost()) + (isPortInHost || '' === port ? '' : ':' + port) + url;
} else if (host && this.getHost() !== host + (isPortInHost || '' === port ? '' : ':' + port)) {
url = this.getScheme() + "://" + host + (isPortInHost || '' === port ? '' : ':' + port) + url;
} else if (absolute === true) {
url = this.getScheme() + "://" + this.getHost() + ('' === port ? '' : ':' + port) + url;
url = this.getScheme() + "://" + this.getHost() + (isPortInHost || '' === port ? '' : ':' + port) + url;
}

if (Object.keys(unusedParams).length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion Resources/public/js/router.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c16a9a6

Please sign in to comment.