Skip to content

Commit

Permalink
fix(urlRouter): Fix absolute 'href' generation by using location.host…
Browse files Browse the repository at this point in the history
…name (not location.host)

Closes #70
  • Loading branch information
christopherthielen committed Aug 12, 2017
1 parent 10558a3 commit a28b68a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/vanilla/browserLocationConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class BrowserLocationConfig implements LocationConfig {
}

host(): string {
return location.host;
return location.hostname;
}

html5Mode(): boolean {
Expand All @@ -48,4 +48,4 @@ export class BrowserLocationConfig implements LocationConfig {
}

dispose() {}
}
}
5 changes: 5 additions & 0 deletions test/urlRouterSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ describe("UrlRouter", function () {
it('should return URLs with #fragments', function () {
expect(urlRouter.href(matcher('/hello/:name'), { name: 'world', '#': 'frag' })).toBe('#/hello/world#frag');
});

it('should return absolute URLs', function () {
let actual = urlRouter.href(matcher('/hello/:name'), { name: 'world', '#': 'frag' }, { absolute: true });
expect(actual).toBe('http://localhost/#/hello/world#frag');
});
});

describe('Url Rule priority', () => {
Expand Down
25 changes: 17 additions & 8 deletions test/vanilla.browserHistorySpec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { UrlMatcher } from "../src/index";
import { UIRouter } from "../src/router";
import { UrlService } from "../src/url/urlService";
import * as vanilla from "../src/vanilla";
import { UrlMatcherFactory } from "../src/url/urlMatcherFactory";
import { BrowserLocationConfig } from '../src/vanilla';

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

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

mockHistory = {
replaceState: (a, b, url) => mockLocation.href = url,
pushState: (a, b, url) => mockLocation.href = url
pushState: (a, b, url) => mockLocation.href = url,
};

mockLocation = {
_href: "/",
pathname: "/",
search: "",
get href() {
return this._href
return this._href;
},
set href(val) {
this._href = val;
var [pathname, search] = val.split("?");
let [pathname, search] = val.split("?");
this.pathname = pathname;
this.search = search ? "?" + search : "";
}
},
};

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

router.stateRegistry.register({
url: '/path/:urlParam?queryParam',
name: 'path'
name: 'path',
});
});

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

it('returns the correct url query', async(done) => {
let service = mockPushState(router);
mockPushState(router);

expect(router.urlService.config.html5Mode()).toBe(true);

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

expect($url.path()).toBe('/path/bar');
expect($url.search()).toEqual({ queryParam: 'query' });
expect($url.search()).toEqual({ queryParam: 'query' });

done();
});

});
it('returns URL portions from the location object', () => {
const blc = new BrowserLocationConfig();

expect(blc.host()).toBe(location.hostname);
expect(blc.port()).toBe(parseInt(location.port, 10));
expect(blc.protocol() + ":").toBe(location.protocol);
});
});

0 comments on commit a28b68a

Please sign in to comment.