Skip to content

Commit a28b68a

Browse files
fix(urlRouter): Fix absolute 'href' generation by using location.hostname (not location.host)
Closes #70
1 parent 10558a3 commit a28b68a

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

src/vanilla/browserLocationConfig.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class BrowserLocationConfig implements LocationConfig {
2626
}
2727

2828
host(): string {
29-
return location.host;
29+
return location.hostname;
3030
}
3131

3232
html5Mode(): boolean {
@@ -48,4 +48,4 @@ export class BrowserLocationConfig implements LocationConfig {
4848
}
4949

5050
dispose() {}
51-
}
51+
}

test/urlRouterSpec.ts

+5
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ describe("UrlRouter", function () {
235235
it('should return URLs with #fragments', function () {
236236
expect(urlRouter.href(matcher('/hello/:name'), { name: 'world', '#': 'frag' })).toBe('#/hello/world#frag');
237237
});
238+
239+
it('should return absolute URLs', function () {
240+
let actual = urlRouter.href(matcher('/hello/:name'), { name: 'world', '#': 'frag' }, { absolute: true });
241+
expect(actual).toBe('http://localhost/#/hello/world#frag');
242+
});
238243
});
239244

240245
describe('Url Rule priority', () => {

test/vanilla.browserHistorySpec.ts

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { UrlMatcher } from "../src/index";
21
import { UIRouter } from "../src/router";
32
import { UrlService } from "../src/url/urlService";
43
import * as vanilla from "../src/vanilla";
54
import { UrlMatcherFactory } from "../src/url/urlMatcherFactory";
5+
import { BrowserLocationConfig } from '../src/vanilla';
66

77
describe('browserHistory implementation', () => {
88

@@ -18,22 +18,22 @@ describe('browserHistory implementation', () => {
1818

1919
mockHistory = {
2020
replaceState: (a, b, url) => mockLocation.href = url,
21-
pushState: (a, b, url) => mockLocation.href = url
21+
pushState: (a, b, url) => mockLocation.href = url,
2222
};
2323

2424
mockLocation = {
2525
_href: "/",
2626
pathname: "/",
2727
search: "",
2828
get href() {
29-
return this._href
29+
return this._href;
3030
},
3131
set href(val) {
3232
this._href = val;
33-
var [pathname, search] = val.split("?");
33+
let [pathname, search] = val.split("?");
3434
this.pathname = pathname;
3535
this.search = search ? "?" + search : "";
36-
}
36+
},
3737
};
3838

3939
plugin.service._history = mockHistory;
@@ -51,7 +51,7 @@ describe('browserHistory implementation', () => {
5151

5252
router.stateRegistry.register({
5353
url: '/path/:urlParam?queryParam',
54-
name: 'path'
54+
name: 'path',
5555
});
5656
});
5757

@@ -71,7 +71,8 @@ describe('browserHistory implementation', () => {
7171
});
7272

7373
it('returns the correct url query', async(done) => {
74-
let service = mockPushState(router);
74+
mockPushState(router);
75+
7576
expect(router.urlService.config.html5Mode()).toBe(true);
7677

7778
await router.stateService.go('path', { urlParam: 'bar' });
@@ -89,8 +90,16 @@ describe('browserHistory implementation', () => {
8990

9091
expect($url.path()).toBe('/path/bar');
9192
expect($url.search()).toEqual({ queryParam: 'query' });
93+
expect($url.search()).toEqual({ queryParam: 'query' });
9294

9395
done();
9496
});
9597

96-
});
98+
it('returns URL portions from the location object', () => {
99+
const blc = new BrowserLocationConfig();
100+
101+
expect(blc.host()).toBe(location.hostname);
102+
expect(blc.port()).toBe(parseInt(location.port, 10));
103+
expect(blc.protocol() + ":").toBe(location.protocol);
104+
});
105+
});

0 commit comments

Comments
 (0)