From 5bb82ffa0e46c89243490eda1e34471f0f299be2 Mon Sep 17 00:00:00 2001 From: dafinoer Date: Fri, 21 Oct 2022 21:13:56 +0700 Subject: [PATCH] add validation for baseurl --- dio/lib/src/options.dart | 3 ++- dio/test/options_test.dart | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/dio/lib/src/options.dart b/dio/lib/src/options.dart index 5da0bbe41..ab7be88a3 100644 --- a/dio/lib/src/options.dart +++ b/dio/lib/src/options.dart @@ -99,7 +99,8 @@ class BaseOptions extends _RequestConfig with OptionsMixin { ResponseDecoder? responseDecoder, ListFormat? listFormat, this.setRequestContentTypeWhenNoPayload = false, - }) : super( + }) : assert(baseUrl.isEmpty || Uri.parse(baseUrl).host.isNotEmpty), + super( method: method, receiveTimeout: receiveTimeout, sendTimeout: sendTimeout, diff --git a/dio/test/options_test.dart b/dio/test/options_test.dart index 1322a9ce1..80de1fed1 100644 --- a/dio/test/options_test.dart +++ b/dio/test/options_test.dart @@ -270,4 +270,19 @@ void main() { assert(r3.uri.toString() == 'https://www.example.com/test'); assert(r3.headers[Headers.contentTypeHeader] == null); }); + + test('#test option invalid base url', () { + final opt = 'blob:http://localhost/xyz123'; + final opt1 = 'https://flutterchina.club'; + final opt2 = 'https://'; + final opt3 = 'https://loremipsum/'; + final opt4 = ''; + final opt5 = 'google.com'; + expect(Uri.parse(opt).host.isNotEmpty, false); + expect(Uri.parse(opt1).host.isNotEmpty, true); + expect(Uri.parse(opt2).host.isNotEmpty, false); + expect(Uri.parse(opt3).host.isNotEmpty, true); + expect(Uri.parse(opt4).host.isNotEmpty, false); + expect(Uri.parse(opt5).host.isNotEmpty, false); + }); }