Skip to content
This repository has been archived by the owner on Feb 13, 2023. It is now read-only.

Add validation for baseUrl #18

Merged
merged 2 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dio/lib/src/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class BaseOptions extends _RequestConfig with OptionsMixin {
ListFormat? listFormat,
this.setRequestContentTypeWhenNoPayload = false,
}) : assert(connectTimeout == null || !connectTimeout.isNegative),
assert(baseUrl.isEmpty || Uri.parse(baseUrl).host.isNotEmpty),
super(
method: method,
receiveTimeout: receiveTimeout,
Expand Down
15 changes: 15 additions & 0 deletions dio/test/options_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,21 @@ void main() {
expect(textResponse2.data, json.encode(expectedResponseData));
});

test('#test option invalid base url', () {
final opt1 = 'blob:http://localhost/xyz123';
final opt2 = 'https://flutterchina.club';
final opt3 = 'https://';
final opt4 = 'https://loremipsum/';
final opt5 = '';
final opt6 = 'google.com';
expect(Uri.parse(opt1).host.isNotEmpty, false);
expect(Uri.parse(opt2).host.isNotEmpty, true);
expect(Uri.parse(opt3).host.isNotEmpty, false);
expect(Uri.parse(opt4).host.isNotEmpty, true);
expect(Uri.parse(opt5).host.isNotEmpty, false);
expect(Uri.parse(opt6).host.isNotEmpty, false);
});

test('Throws when using invalid methods', () async {
final dio = Dio();
void testInvalidArgumentException(String method) async {
Expand Down