diff --git a/auto_route/lib/src/matcher/route_matcher.dart b/auto_route/lib/src/matcher/route_matcher.dart index 0f95f037..8fcfc04b 100644 --- a/auto_route/lib/src/matcher/route_matcher.dart +++ b/auto_route/lib/src/matcher/route_matcher.dart @@ -53,14 +53,16 @@ class RouteMatcher { return _handleRedirect( routesCollection: collection, includePrefixMatches: includePrefixMatches, - redirectTo: uri.replace( - path: Uri.parse( + redirectTo: Uri.parse( PageRouteInfo.expandPath( config.redirectTo, match.pathParams.rawMap, ), - ).path), - redirectedFrom: config.path, + ), + redirectedFrom: PageRouteInfo.expandPath( + config.path, + match.pathParams.rawMap, + ), ); } @@ -113,6 +115,7 @@ class RouteMatcher { required Uri redirectTo, required String redirectedFrom, }) { + print(redirectTo); return _match( redirectTo, routesCollection, diff --git a/auto_route/test/matcher_test.dart b/auto_route/test/matcher_test.dart index ad8cf3df..ee0d2936 100644 --- a/auto_route/test/matcher_test.dart +++ b/auto_route/test/matcher_test.dart @@ -5,9 +5,7 @@ import 'package:flutter_test/flutter_test.dart'; void main() { group('Testing RouteCollection', () { - test( - 'Building default constructor with empty map should throw in assertion error', - () { + test('Building default constructor with empty map should throw in assertion error', () { expect(() => RouteCollection.fromList(const []), throwsAssertionError); }); @@ -24,27 +22,18 @@ void main() { root: true, ); - test( - 'Creating root RouteCollection with a root route not starting with "/" should throw', - () { - expect( - () => - RouteCollection.fromList([TestRoute('A', path: 'a')], root: true), - throwsFlutterError); + test('Creating root RouteCollection with a root route not starting with "/" should throw', () { + expect(() => RouteCollection.fromList([TestRoute('A', path: 'a')], root: true), throwsFlutterError); }); - test( - 'Creating sub RouteCollection with a sub route starting with "/" should throw', - () { + test('Creating sub RouteCollection with a sub route starting with "/" should throw', () { expect( - () => - RouteCollection.fromList([TestRoute('A', path: '/a')], root: false), + () => RouteCollection.fromList([TestRoute('A', path: '/a')], root: false), throwsFlutterError, ); }); - test('Auto-generated root paths for routes marked as initial should be "/"', - () { + test('Auto-generated root paths for routes marked as initial should be "/"', () { expect( RouteCollection.fromList( [TestRoute('A', initial: true)], @@ -54,9 +43,7 @@ void main() { ); }); - test( - 'Auto-generated sub-paths for routes marked as initial should be empty""', - () { + test('Auto-generated sub-paths for routes marked as initial should be empty""', () { expect( RouteCollection.fromList( [TestRoute('A', initial: true)], @@ -66,9 +53,7 @@ void main() { ); }); - test( - 'Creating a RouteCollection with more than one routes marked as initial should throw', - () { + test('Creating a RouteCollection with more than one routes marked as initial should throw', () { expect( () => RouteCollection.fromList( [ @@ -108,8 +93,7 @@ void main() { expect(collection.routes, expectedRoutes); }); - test('Calling findPathTo to C1 should return a list of route trails[C,C1]', - () { + test('Calling findPathTo to C1 should return a list of route trails[C,C1]', () { expect(collection.findPathTo('C1'), [routeC, subRouteC1]); }); @@ -121,24 +105,17 @@ void main() { expect(collection.containsKey('X'), isFalse); }); - test( - 'Extracting sub collection of a branch route should return sub collection', - () { - var expectedCollection = - RouteCollection.fromList([subRouteC1], root: false); + test('Extracting sub collection of a branch route should return sub collection', () { + var expectedCollection = RouteCollection.fromList([subRouteC1], root: false); expect(collection.subCollectionOf('C'), expectedCollection); }); - test( - 'Extracting sub collection of a leaf or a non-existing route should throw', - () { + test('Extracting sub collection of a leaf or a non-existing route should throw', () { expect(() => collection.subCollectionOf('A'), throwsAssertionError); expect(() => collection.subCollectionOf('X'), throwsAssertionError); }); - test( - 'Calling [] operator with an existing key should return corresponding route', - () { + test('Calling [] operator with an existing key should return corresponding route', () { expect(collection['A'], routeA); }); @@ -274,8 +251,7 @@ void main() { expect(match('/b', includePrefixMatches: true), expectedMatches); }); - test('Should return two prefix matches with one nested match [A, C/C1]', - () { + test('Should return two prefix matches with one nested match [A, C/C1]', () { final expectedMatches = [ RouteMatch( config: routeA, @@ -301,8 +277,7 @@ void main() { expect(match('/c/c1', includePrefixMatches: true), expectedMatches); }); - test('Should return two prefix matches with one nested match [A, D/D0]', - () { + test('Should return two prefix matches with one nested match [A, D/D0]', () { final expectedMatches = [ RouteMatch( config: routeA, @@ -328,9 +303,7 @@ void main() { expect(match('/d', includePrefixMatches: true), expectedMatches); }); - test( - 'Should return two matches with two nested matches including empty path [A, D/D0/D1]', - () { + test('Should return two matches with two nested matches including empty path [A, D/D0/D1]', () { final expectedMatches = [ RouteMatch( config: routeA, @@ -366,8 +339,7 @@ void main() { expect(match('/c/c1/undefined', includePrefixMatches: true), isNull); }); - test('Should return two prefix matches and one full match [A, B, B/B1]', - () { + test('Should return two prefix matches and one full match [A, B, B/B1]', () { final expectedMatches = [ RouteMatch( config: routeA, @@ -450,7 +422,6 @@ void main() { group('Testing redirect routes', () { final routeA = TestRoute('A', path: '/a'); final routeARedirect = RedirectRoute(path: '/', redirectTo: '/a'); - final subRouteC1 = TestRoute('C1', path: 'c1'); final subRouteC1Redirect = RedirectRoute(path: '', redirectTo: 'c1'); @@ -533,6 +504,55 @@ void main() { ]; expect(match2('/a/r'), expectedMatches); }); + + test('Should redirect to [/A?foo=bar]', () { + final match = RouteMatcher( + RouteCollection.fromList( + [ + RedirectRoute(path: '/', redirectTo: '/a?foo=bar'), + routeA, + ], + root: true, + ), + ).match; + final expectedMatches = [ + RouteMatch( + config: routeA, + key: const ValueKey('A'), + stringMatch: '/a', + segments: const ['/', 'a'], + queryParams: const Parameters({'foo': 'bar'}), + redirectedFrom: '/', + ) + ]; + expect(match('/?foo=bar'), expectedMatches); + }); + + + test('Should redirect to [/A/:id]', () { + final routeA = TestRoute('A', path: '/a/:id'); + final match = RouteMatcher( + RouteCollection.fromList( + [ + RedirectRoute(path: '/x/:id', redirectTo: '/a/:id'), + routeA, + ], + root: true, + ), + ).match; + final expectedMatches = [ + RouteMatch( + config: routeA, + key: const ValueKey('A'), + stringMatch: '/a/foo', + segments: const ['/', 'a','foo'], + pathParams: const Parameters({'id': 'foo'}), + redirectedFrom: '/x/foo', + ) + ]; + expect(match('/x/foo'), expectedMatches); + }); + }); group('Testing Path parameters parsing', () { @@ -565,8 +585,7 @@ void main() { expect(match('/a/1'), expectedMatches); }); - test('Should match route [B] and extract path params {id:1, type:none}', - () { + test('Should match route [B] and extract path params {id:1, type:none}', () { final expectedMatches = [ RouteMatch( config: routeB, @@ -634,9 +653,7 @@ void main() { expect(match('/a?foo=bar'), expectedMatches); }); - test( - 'Should match routes [B,B1] and extract query params {foo:bar, bar:baz} for both', - () { + test('Should match routes [B,B1] and extract query params {foo:bar, bar:baz} for both', () { final expectedMatches = [ RouteMatch( key: const ValueKey('B'), @@ -653,13 +670,10 @@ void main() { queryParams: const Parameters({'foo': 'bar', 'bar': 'baz'}), ) ]; - expect(match('/b/b1?foo=bar&bar=baz', includePrefixMatches: true), - expectedMatches); + expect(match('/b/b1?foo=bar&bar=baz', includePrefixMatches: true), expectedMatches); }); - test( - 'Should match route [C/C1] and extract query parameters {foo:bar} for parent and child', - () { + test('Should match route [C/C1] and extract query parameters {foo:bar} for parent and child', () { final expectedMatches = [ RouteMatch( config: routeC,