Skip to content

Commit b71811b

Browse files
committed
Fixed broken tests - DC
1 parent 8486160 commit b71811b

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

packages/go_router/lib/src/configuration.dart

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -292,24 +292,11 @@ class RouteConfiguration {
292292

293293
/// Finds the routes that matched the given URL.
294294
RouteMatchList findMatch(String location, {Object? extra}) {
295-
final Uri uri = Uri.parse(location);
296-
// Remove the host and scheme from the uri to match
297-
// TODO(ChopinDavid): Do we want to expose this as a getter on `Uri` itself? Or use an extension/helper method?
298-
final Uri uriToMatch = Uri.parse(
299-
canonicalUri(
300-
location.substring(
301-
uri.host.isNotEmpty
302-
? uri.toString().indexOf(uri.host) + uri.host.length
303-
: uri.hasScheme
304-
? uri.toString().indexOf(uri.scheme) + uri.scheme.length
305-
: 0,
306-
),
307-
),
308-
);
295+
final Uri uri = Uri.parse(canonicalUri(location));
309296

310297
final Map<String, String> pathParameters = <String, String>{};
311298
final List<RouteMatchBase> matches =
312-
_getLocRouteMatches(uriToMatch, pathParameters);
299+
_getLocRouteMatches(uri, pathParameters);
313300

314301
if (matches.isEmpty) {
315302
return _errorRouteMatchList(

packages/go_router/lib/src/path_utils.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'misc/errors.dart';
56
import 'route.dart';
67

78
final RegExp _parameterRegExp = RegExp(r':(\w+)(\((?:\\.|[^\\()])+\))?');
@@ -120,6 +121,9 @@ String concatenatePaths(String parentPath, String childPath) {
120121

121122
/// Normalizes the location string.
122123
String canonicalUri(String loc) {
124+
if (loc.isEmpty) {
125+
throw GoException('Location cannot be empty.');
126+
}
123127
String canon = Uri.parse(loc).toString();
124128
canon = canon.endsWith('?') ? canon.substring(0, canon.length - 1) : canon;
125129

@@ -131,9 +135,18 @@ String canonicalUri(String loc) {
131135
? canon.substring(0, canon.length - 1)
132136
: canon;
133137

138+
// replace '/?', except for first occurrence, from path only
134139
// /login/?from=/ => /login?from=/
135140
// /?from=/ => /?from=/
136-
canon = canon.replaceFirst('/?', '?', 1);
141+
final Uri uri = Uri.parse(canon);
142+
final int pathStartIndex = uri.host.isNotEmpty
143+
? uri.toString().indexOf(uri.host) + uri.host.length
144+
: uri.hasScheme
145+
? uri.toString().indexOf(uri.scheme) + uri.scheme.length
146+
: 0;
147+
if (pathStartIndex < canon.length) {
148+
canon = canon.replaceFirst('/?', '?', pathStartIndex + 1);
149+
}
137150

138151
return canon;
139152
}

0 commit comments

Comments
 (0)