Skip to content

Commit

Permalink
Revert "[dio] Ensure consistent handling of "/" in concatenation of b…
Browse files Browse the repository at this point in the history
…aseUrl with path" (cfug#1887)

Reverts cfug#1873
  • Loading branch information
AlexV525 authored Jul 1, 2023
1 parent 1c640a6 commit ec88e89
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 97 deletions.
1 change: 0 additions & 1 deletion dio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 9 additions & 12 deletions dio/lib/src/options.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:io';

import 'package:meta/meta.dart';

import 'adapter.dart';
Expand Down Expand Up @@ -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.
Expand Down
84 changes: 0 additions & 84 deletions dio/test/options_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}),
);
});
}

0 comments on commit ec88e89

Please sign in to comment.