Skip to content

Commit ec88e89

Browse files
authored
Revert "[dio] Ensure consistent handling of "/" in concatenation of baseUrl with path" (#1887)
Reverts #1873
1 parent 1c640a6 commit ec88e89

File tree

3 files changed

+9
-97
lines changed

3 files changed

+9
-97
lines changed

dio/CHANGELOG.md

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ See the [Migration Guide][] for the complete breaking changes list.**
55

66
## Unreleased
77

8-
- Fix url concatenation '/' handling
98
- Remove `http` from `dev_dependencies`.
109

1110
## 5.2.1+1

dio/lib/src/options.dart

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'dart:io';
2-
31
import 'package:meta/meta.dart';
42

53
import 'adapter.dart';
@@ -587,21 +585,20 @@ class RequestOptions extends _RequestConfig with OptionsMixin {
587585

588586
/// generate uri
589587
Uri get uri {
590-
Uri uri = Uri.parse(path);
591-
592-
if (!uri.isAbsolute) {
593-
uri = Uri.parse(baseUrl).resolveUri(uri);
588+
String url = path;
589+
if (!url.startsWith(RegExp(r'https?:'))) {
590+
url = baseUrl + url;
591+
final s = url.split(':/');
592+
if (s.length == 2) {
593+
url = '${s[0]}:/${s[1].replaceAll('//', '/')}';
594+
}
594595
}
595-
596596
final query = Transformer.urlEncodeQueryMap(queryParameters, listFormat);
597597
if (query.isNotEmpty) {
598-
final separator = uri.query.isNotEmpty ? '&' : '';
599-
final mergedQuery = uri.query + separator + query;
600-
uri = uri.replace(query: mergedQuery);
598+
url += (url.contains('?') ? '&' : '?') + query;
601599
}
602-
603600
// Normalize the url.
604-
return uri.normalizePath();
601+
return Uri.parse(url).normalizePath();
605602
}
606603

607604
/// Request data, can be any type.

dio/test/options_test.dart

-84
Original file line numberDiff line numberDiff line change
@@ -469,88 +469,4 @@ void main() {
469469
expect(response.data, 'test');
470470
}
471471
});
472-
473-
test('Ensure consistent slash handling', () {
474-
final dio = Dio();
475-
final inputs = [
476-
['https://www.example.com', 'path'],
477-
['https://www.example.com/', 'path'],
478-
['https://www.example.com', '/path'],
479-
['https://www.example.com/', '/path'],
480-
];
481-
482-
for (final input in inputs) {
483-
final baseUrl = input[0];
484-
final path = input[1];
485-
dio.options.baseUrl = baseUrl;
486-
final actual = Options().compose(dio.options, path).uri.toString();
487-
expect(actual, equals('https://www.example.com/path'));
488-
}
489-
});
490-
491-
test('Should return absolute URI when path is already absolute', () {
492-
final baseUrl = 'https://www.example.com';
493-
final path = 'https://www.another-example.com/path/to/resource';
494-
final baseOptions = BaseOptions(baseUrl: baseUrl);
495-
496-
final actual = Options().compose(baseOptions, path).uri;
497-
498-
expect(actual.toString(), equals(path));
499-
});
500-
501-
test('Should resolve relative path with base URL', () {
502-
final baseUrl = 'https://www.example.com';
503-
final path = '/path/to/resource';
504-
final baseOptions = BaseOptions(baseUrl: baseUrl);
505-
506-
final actual = Options().compose(baseOptions, path).uri;
507-
508-
expect(
509-
actual.toString(),
510-
equals('https://www.example.com/path/to/resource'),
511-
);
512-
});
513-
514-
test('Should add query parameters to the URI', () {
515-
final baseUrl = 'https://www.example.com';
516-
final path = '/path/to/resource';
517-
final baseOptions = BaseOptions(
518-
baseUrl: baseUrl,
519-
queryParameters: {'param1': 'value1'},
520-
);
521-
522-
final actual = Options().compose(
523-
baseOptions,
524-
path,
525-
queryParameters: {'param2': 'value2'},
526-
).uri;
527-
528-
expect(
529-
actual.queryParameters,
530-
equals({
531-
'param1': 'value1',
532-
'param2': 'value2',
533-
}),
534-
);
535-
});
536-
537-
test('Should preserve path query parameters to the URI', () {
538-
final baseUrl = 'https://www.example.com';
539-
final path = '/path/to/resource?param1=value1';
540-
final baseOptions = BaseOptions(baseUrl: baseUrl);
541-
542-
final actual = Options(listFormat: ListFormat.csv).compose(
543-
baseOptions,
544-
path,
545-
queryParameters: {'param2': 'value2'},
546-
).uri;
547-
548-
expect(
549-
actual.queryParameters,
550-
equals({
551-
'param1': 'value1',
552-
'param2': 'value2',
553-
}),
554-
);
555-
});
556472
}

0 commit comments

Comments
 (0)