| 1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | @TestOn('chrome' ) // Uses web-only Flutter SDK |
| 6 | library; |
| 7 | |
| 8 | import 'package:flutter_test/flutter_test.dart'; |
| 9 | import 'package:flutter_web_plugins/url_strategy.dart'; |
| 10 | |
| 11 | import 'common.dart'; |
| 12 | |
| 13 | void main() { |
| 14 | group(' $PathUrlStrategy' , () { |
| 15 | late TestPlatformLocation location; |
| 16 | |
| 17 | setUp(() { |
| 18 | location = TestPlatformLocation(); |
| 19 | location.baseHref = '/' ; |
| 20 | }); |
| 21 | |
| 22 | test('allows null state' , () { |
| 23 | final PathUrlStrategy strategy = PathUrlStrategy(location); |
| 24 | expect(() => strategy.pushState(null, '' , '/' ), returnsNormally); |
| 25 | expect(() => strategy.replaceState(null, '' , '/' ), returnsNormally); |
| 26 | }); |
| 27 | |
| 28 | test('validates base href' , () { |
| 29 | expect(() => PathUrlStrategy(location), returnsNormally); |
| 30 | |
| 31 | location.baseHref = '/foo/' ; |
| 32 | expect(() => PathUrlStrategy(location), returnsNormally); |
| 33 | |
| 34 | location.baseHref = '' ; |
| 35 | expect(() => PathUrlStrategy(location), throwsException); |
| 36 | |
| 37 | location.baseHref = 'foo' ; |
| 38 | expect(() => PathUrlStrategy(location), throwsException); |
| 39 | |
| 40 | location.baseHref = '/foo' ; |
| 41 | expect(() => PathUrlStrategy(location), throwsException); |
| 42 | }); |
| 43 | |
| 44 | test('leading slash is always prepended' , () { |
| 45 | final PathUrlStrategy strategy = PathUrlStrategy(location); |
| 46 | |
| 47 | location.pathname = '' ; |
| 48 | expect(strategy.getPath(), '/' ); |
| 49 | |
| 50 | location.pathname = 'foo' ; |
| 51 | expect(strategy.getPath(), '/foo' ); |
| 52 | }); |
| 53 | |
| 54 | test('gets path correctly in the presence of basePath' , () { |
| 55 | location.baseHref = 'https://example.com/foo/'; |
| 56 | final PathUrlStrategy strategy = PathUrlStrategy(location); |
| 57 | |
| 58 | location.pathname = '/foo/' ; |
| 59 | expect(strategy.getPath(), '/' ); |
| 60 | |
| 61 | location.pathname = '/foo' ; |
| 62 | expect(strategy.getPath(), '/' ); |
| 63 | |
| 64 | location.pathname = '/foo/bar' ; |
| 65 | expect(strategy.getPath(), '/bar' ); |
| 66 | }); |
| 67 | |
| 68 | test( |
| 69 | 'gets path correctly in the presence of query params and omits fragment if no flag specified' , |
| 70 | () { |
| 71 | location.baseHref = 'https://example.com/foo/'; |
| 72 | location.pathname = '/foo/bar' ;
|
| 73 | final PathUrlStrategy strategy = PathUrlStrategy(location);
|
| 74 |
|
| 75 | location.search = '?q=1' ;
|
| 76 | expect(strategy.getPath(), '/bar?q=1' );
|
| 77 |
|
| 78 | location.search = '?q=1&t=r' ;
|
| 79 | expect(strategy.getPath(), '/bar?q=1&t=r' );
|
| 80 |
|
| 81 | location.hash = '#fragment=1' ;
|
| 82 | expect(strategy.getPath(), '/bar?q=1&t=r' );
|
| 83 | },
|
| 84 | );
|
| 85 |
|
| 86 | test('gets path correctly in the presence of query params and fragment' , () {
|
| 87 | location.baseHref = 'https://example.com/foo/';
|
| 88 | location.pathname = '/foo/bar' ;
|
| 89 | final PathUrlStrategy strategy = PathUrlStrategy(location, true);
|
| 90 |
|
| 91 | location.search = '?q=1' ;
|
| 92 | expect(strategy.getPath(), '/bar?q=1' );
|
| 93 |
|
| 94 | location.search = '?q=1&t=r' ;
|
| 95 | expect(strategy.getPath(), '/bar?q=1&t=r' );
|
| 96 |
|
| 97 | location.hash = '#fragment=1' ;
|
| 98 | expect(strategy.getPath(), '/bar?q=1&t=r#fragment=1' );
|
| 99 | });
|
| 100 |
|
| 101 | test('empty route name is ok' , () {
|
| 102 | final PathUrlStrategy strategy = PathUrlStrategy(location);
|
| 103 | expect(strategy.prepareExternalUrl('' ), '/' );
|
| 104 | expect(() => strategy.pushState(null, '' , '' ), returnsNormally);
|
| 105 | expect(() => strategy.replaceState(null, '' , '' ), returnsNormally);
|
| 106 | });
|
| 107 |
|
| 108 | test('route names must start with /' , () {
|
| 109 | final PathUrlStrategy strategy = PathUrlStrategy(location);
|
| 110 |
|
| 111 | expect(() => strategy.prepareExternalUrl('foo' ), throwsAssertionError);
|
| 112 | expect(() => strategy.prepareExternalUrl('foo/' ), throwsAssertionError);
|
| 113 | expect(() => strategy.prepareExternalUrl('foo/bar' ), throwsAssertionError);
|
| 114 |
|
| 115 | expect(() => strategy.pushState(null, '' , 'foo' ), throwsAssertionError);
|
| 116 | expect(() => strategy.pushState(null, '' , 'foo/' ), throwsAssertionError);
|
| 117 | expect(() => strategy.pushState(null, '' , 'foo/bar' ), throwsAssertionError);
|
| 118 |
|
| 119 | expect(() => strategy.replaceState(null, '' , 'foo' ), throwsAssertionError);
|
| 120 | expect(() => strategy.replaceState(null, '' , 'foo/' ), throwsAssertionError);
|
| 121 | expect(() => strategy.replaceState(null, '' , 'foo/bar' ), throwsAssertionError);
|
| 122 | });
|
| 123 |
|
| 124 | test('generates external path correctly in the presence of basePath' , () {
|
| 125 | location.baseHref = 'https://example.com/foo/';
|
| 126 | final PathUrlStrategy strategy = PathUrlStrategy(location);
|
| 127 |
|
| 128 | expect(strategy.prepareExternalUrl('' ), '/foo/' );
|
| 129 | expect(strategy.prepareExternalUrl('/' ), '/foo/' );
|
| 130 | expect(strategy.prepareExternalUrl('/bar' ), '/foo/bar' );
|
| 131 | expect(strategy.prepareExternalUrl('/bar/' ), '/foo/bar/' );
|
| 132 | });
|
| 133 | });
|
| 134 | }
|
| 135 |
|