@@ -5,6 +5,7 @@ import { UrlService } from "../src/url/urlService";
5
5
import { StateRegistry } from "../src/state/stateRegistry" ;
6
6
import { noop } from "../src/common/common" ;
7
7
import { UrlRule , MatchResult } from "../src/url/interface" ;
8
+ import { pushStateLocationPlugin } from '../src/vanilla' ;
8
9
9
10
declare let jasmine ;
10
11
let _anything = jasmine . anything ( ) ;
@@ -240,6 +241,134 @@ describe("UrlRouter", function () {
240
241
let actual = urlRouter . href ( matcher ( '/hello/:name' ) , { name : 'world' , '#' : 'frag' } , { absolute : true } ) ;
241
242
expect ( actual ) . toBe ( 'http://localhost/#/hello/world#frag' ) ;
242
243
} ) ;
244
+
245
+ describe ( 'in html5mode' , ( ) => {
246
+ let baseTag : HTMLBaseElement ;
247
+ const applyBaseTag = ( href : string ) => {
248
+ baseTag = document . createElement ( 'base' ) ;
249
+ baseTag . href = href ;
250
+ document . head . appendChild ( baseTag ) ;
251
+ } ;
252
+
253
+ afterEach ( ( ) => baseTag . parentElement . removeChild ( baseTag ) ) ;
254
+
255
+ beforeEach ( ( ) => {
256
+ router . dispose ( router . getPlugin ( 'vanilla.memoryLocation' ) ) ;
257
+ router . plugin ( pushStateLocationPlugin ) ;
258
+ router . urlService = new UrlService ( router , false ) ;
259
+ } ) ;
260
+
261
+ describe ( 'with base="/base/"' , ( ) => {
262
+ beforeEach ( ( ) => applyBaseTag ( '/base/' ) ) ;
263
+
264
+ it ( "should prefix the href with /base/" , function ( ) {
265
+ expect ( urlRouter . href ( matcher ( "/foo" ) ) ) . toBe ( '/base/foo' ) ;
266
+ } ) ;
267
+
268
+ it ( 'should include #fragments' , function ( ) {
269
+ expect ( urlRouter . href ( matcher ( "/foo" ) , { '#' : "hello" } ) ) . toBe ( '/base/foo#hello' ) ;
270
+ } ) ;
271
+
272
+ it ( 'should return absolute URLs' , function ( ) {
273
+ // don't use urlService var
274
+ const cfg = router . urlService . config ;
275
+ const href = urlRouter . href ( matcher ( '/hello/:name' ) , { name : 'world' , '#' : 'frag' } , { absolute : true } ) ;
276
+ const prot = cfg . protocol ( ) ;
277
+ const host = cfg . host ( ) ;
278
+ const port = cfg . port ( ) ;
279
+ let portStr = ( port === 80 || port === 443 ) ? '' : `:${ port } ` ;
280
+ expect ( href ) . toBe ( `${ prot } ://${ host } ${ portStr } /base/hello/world#frag` ) ;
281
+ } ) ;
282
+ } ) ;
283
+
284
+ describe ( 'with base="/base/index.html"' , ( ) => {
285
+ beforeEach ( ( ) => applyBaseTag ( '/base/index.html' ) ) ;
286
+
287
+ it ( "should prefix the href with /base/ but not with index.html" , function ( ) {
288
+ expect ( urlRouter . href ( matcher ( "/foo" ) ) ) . toBe ( '/base/foo' ) ;
289
+ } ) ;
290
+
291
+ it ( 'should include #fragments' , function ( ) {
292
+ expect ( urlRouter . href ( matcher ( "/foo" ) , { '#' : "hello" } ) ) . toBe ( '/base/foo#hello' ) ;
293
+ } ) ;
294
+
295
+ it ( 'should return absolute URLs' , function ( ) {
296
+ // don't use urlService var
297
+ const cfg = router . urlService . config ;
298
+ const href = urlRouter . href ( matcher ( '/hello/:name' ) , { name : 'world' , '#' : 'frag' } , { absolute : true } ) ;
299
+ const prot = cfg . protocol ( ) ;
300
+ const host = cfg . host ( ) ;
301
+ const port = cfg . port ( ) ;
302
+ let portStr = ( port === 80 || port === 443 ) ? '' : `:${ port } ` ;
303
+ expect ( href ) . toBe ( `${ prot } ://${ host } ${ portStr } /base/hello/world#frag` ) ;
304
+ } ) ;
305
+ } ) ;
306
+
307
+ describe ( 'with base="http://localhost:8080/base/"' , ( ) => {
308
+ beforeEach ( ( ) => applyBaseTag ( 'http://localhost:8080/base/' ) ) ;
309
+
310
+ it ( "should prefix the href with /base/" , function ( ) {
311
+ expect ( urlRouter . href ( matcher ( "/foo" ) ) ) . toBe ( '/base/foo' ) ;
312
+ } ) ;
313
+
314
+ it ( 'should include #fragments' , function ( ) {
315
+ expect ( urlRouter . href ( matcher ( "/foo" ) , { '#' : "hello" } ) ) . toBe ( '/base/foo#hello' ) ;
316
+ } ) ;
317
+
318
+ it ( 'should return absolute URLs' , function ( ) {
319
+ // don't use urlService var
320
+ const cfg = router . urlService . config ;
321
+ const href = urlRouter . href ( matcher ( '/hello/:name' ) , { name : 'world' , '#' : 'frag' } , { absolute : true } ) ;
322
+ const prot = cfg . protocol ( ) ;
323
+ const host = cfg . host ( ) ;
324
+ const port = cfg . port ( ) ;
325
+ let portStr = ( port === 80 || port === 443 ) ? '' : `:${ port } ` ;
326
+ expect ( href ) . toBe ( `${ prot } ://${ host } ${ portStr } /base/hello/world#frag` ) ;
327
+ } ) ;
328
+ } ) ;
329
+
330
+ describe ( 'with base="http://localhost:8080/base"' , ( ) => {
331
+ beforeEach ( ( ) => applyBaseTag ( 'http://localhost:8080/base' ) ) ;
332
+
333
+ it ( "should not prefix the href with /base" , function ( ) {
334
+ expect ( urlRouter . href ( matcher ( "/foo" ) ) ) . toBe ( '/foo' ) ;
335
+ } ) ;
336
+
337
+ it ( 'should return absolute URLs' , function ( ) {
338
+ // don't use urlService var
339
+ const cfg = router . urlService . config ;
340
+ const href = urlRouter . href ( matcher ( '/hello/:name' ) , { name : 'world' , '#' : 'frag' } , { absolute : true } ) ;
341
+ const prot = cfg . protocol ( ) ;
342
+ const host = cfg . host ( ) ;
343
+ const port = cfg . port ( ) ;
344
+ let portStr = ( port === 80 || port === 443 ) ? '' : `:${ port } ` ;
345
+ expect ( href ) . toBe ( `${ prot } ://${ host } ${ portStr } /hello/world#frag` ) ;
346
+ } ) ;
347
+ } ) ;
348
+
349
+ describe ( 'with base="http://localhost:8080/base/index.html"' , ( ) => {
350
+ beforeEach ( ( ) => applyBaseTag ( 'http://localhost:8080/base/index.html' ) ) ;
351
+
352
+ it ( "should prefix the href with /base/" , function ( ) {
353
+ expect ( urlRouter . href ( matcher ( "/foo" ) ) ) . toBe ( '/base/foo' ) ;
354
+ } ) ;
355
+
356
+ it ( 'should include #fragments' , function ( ) {
357
+ expect ( urlRouter . href ( matcher ( "/foo" ) , { '#' : "hello" } ) ) . toBe ( '/base/foo#hello' ) ;
358
+ } ) ;
359
+
360
+ it ( 'should return absolute URLs' , function ( ) {
361
+ // don't use urlService var
362
+ const cfg = router . urlService . config ;
363
+ const href = urlRouter . href ( matcher ( '/hello/:name' ) , { name : 'world' , '#' : 'frag' } , { absolute : true } ) ;
364
+ const prot = cfg . protocol ( ) ;
365
+ const host = cfg . host ( ) ;
366
+ const port = cfg . port ( ) ;
367
+ let portStr = ( port === 80 || port === 443 ) ? '' : `:${ port } ` ;
368
+ expect ( href ) . toBe ( `${ prot } ://${ host } ${ portStr } /base/hello/world#frag` ) ;
369
+ } ) ;
370
+ } ) ;
371
+ } ) ;
243
372
} ) ;
244
373
245
374
describe ( 'Url Rule priority' , ( ) => {
0 commit comments