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

Commit

Permalink
Add validation for baseUrl (#18)
Browse files Browse the repository at this point in the history
Co-authored-by: dafinoer <dafinoer@outlook.com>
  • Loading branch information
AlexV525 and dafinoer committed Nov 8, 2022
1 parent 090f18f commit 55f4529
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
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

0 comments on commit 55f4529

Please sign in to comment.