From ec88e8919527ad43e632e67bf9dd1787e845853b Mon Sep 17 00:00:00 2001 From: Alex Li Date: Sat, 1 Jul 2023 20:13:17 +0800 Subject: [PATCH] Revert "[dio] Ensure consistent handling of "/" in concatenation of baseUrl with path" (#1887) Reverts cfug/dio#1873 --- dio/CHANGELOG.md | 1 - dio/lib/src/options.dart | 21 ++++------ dio/test/options_test.dart | 84 -------------------------------------- 3 files changed, 9 insertions(+), 97 deletions(-) diff --git a/dio/CHANGELOG.md b/dio/CHANGELOG.md index b1db236df..f245beca8 100644 --- a/dio/CHANGELOG.md +++ b/dio/CHANGELOG.md @@ -5,7 +5,6 @@ See the [Migration Guide][] for the complete breaking changes list.** ## Unreleased -- Fix url concatenation '/' handling - Remove `http` from `dev_dependencies`. ## 5.2.1+1 diff --git a/dio/lib/src/options.dart b/dio/lib/src/options.dart index 756a66f37..e43849216 100644 --- a/dio/lib/src/options.dart +++ b/dio/lib/src/options.dart @@ -1,5 +1,3 @@ -import 'dart:io'; - import 'package:meta/meta.dart'; import 'adapter.dart'; @@ -587,21 +585,20 @@ class RequestOptions extends _RequestConfig with OptionsMixin { /// generate uri Uri get uri { - Uri uri = Uri.parse(path); - - if (!uri.isAbsolute) { - uri = Uri.parse(baseUrl).resolveUri(uri); + String url = path; + if (!url.startsWith(RegExp(r'https?:'))) { + url = baseUrl + url; + final s = url.split(':/'); + if (s.length == 2) { + url = '${s[0]}:/${s[1].replaceAll('//', '/')}'; + } } - final query = Transformer.urlEncodeQueryMap(queryParameters, listFormat); if (query.isNotEmpty) { - final separator = uri.query.isNotEmpty ? '&' : ''; - final mergedQuery = uri.query + separator + query; - uri = uri.replace(query: mergedQuery); + url += (url.contains('?') ? '&' : '?') + query; } - // Normalize the url. - return uri.normalizePath(); + return Uri.parse(url).normalizePath(); } /// Request data, can be any type. diff --git a/dio/test/options_test.dart b/dio/test/options_test.dart index 5c85eadc5..7f18566e6 100644 --- a/dio/test/options_test.dart +++ b/dio/test/options_test.dart @@ -469,88 +469,4 @@ void main() { expect(response.data, 'test'); } }); - - test('Ensure consistent slash handling', () { - final dio = Dio(); - final inputs = [ - ['https://www.example.com', 'path'], - ['https://www.example.com/', 'path'], - ['https://www.example.com', '/path'], - ['https://www.example.com/', '/path'], - ]; - - for (final input in inputs) { - final baseUrl = input[0]; - final path = input[1]; - dio.options.baseUrl = baseUrl; - final actual = Options().compose(dio.options, path).uri.toString(); - expect(actual, equals('https://www.example.com/path')); - } - }); - - test('Should return absolute URI when path is already absolute', () { - final baseUrl = 'https://www.example.com'; - final path = 'https://www.another-example.com/path/to/resource'; - final baseOptions = BaseOptions(baseUrl: baseUrl); - - final actual = Options().compose(baseOptions, path).uri; - - expect(actual.toString(), equals(path)); - }); - - test('Should resolve relative path with base URL', () { - final baseUrl = 'https://www.example.com'; - final path = '/path/to/resource'; - final baseOptions = BaseOptions(baseUrl: baseUrl); - - final actual = Options().compose(baseOptions, path).uri; - - expect( - actual.toString(), - equals('https://www.example.com/path/to/resource'), - ); - }); - - test('Should add query parameters to the URI', () { - final baseUrl = 'https://www.example.com'; - final path = '/path/to/resource'; - final baseOptions = BaseOptions( - baseUrl: baseUrl, - queryParameters: {'param1': 'value1'}, - ); - - final actual = Options().compose( - baseOptions, - path, - queryParameters: {'param2': 'value2'}, - ).uri; - - expect( - actual.queryParameters, - equals({ - 'param1': 'value1', - 'param2': 'value2', - }), - ); - }); - - test('Should preserve path query parameters to the URI', () { - final baseUrl = 'https://www.example.com'; - final path = '/path/to/resource?param1=value1'; - final baseOptions = BaseOptions(baseUrl: baseUrl); - - final actual = Options(listFormat: ListFormat.csv).compose( - baseOptions, - path, - queryParameters: {'param2': 'value2'}, - ).uri; - - expect( - actual.queryParameters, - equals({ - 'param1': 'value1', - 'param2': 'value2', - }), - ); - }); }