From fb1def12e1214e1a690eec21ea73d517ac367876 Mon Sep 17 00:00:00 2001 From: Ashita Prasad Date: Sun, 15 Jun 2025 15:52:54 +0530 Subject: [PATCH] fixes --- packages/apidash_core/lib/consts.dart | 2 ++ .../apidash_core/lib/utils/uri_utils.dart | 7 +---- .../test/utils/uri_utils_test.dart | 31 +++++++++++++++++++ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/packages/apidash_core/lib/consts.dart b/packages/apidash_core/lib/consts.dart index c1d45ad4..3f1d74d8 100644 --- a/packages/apidash_core/lib/consts.dart +++ b/packages/apidash_core/lib/consts.dart @@ -30,6 +30,8 @@ final kSupportedUriSchemes = SupportedUriSchemes.values.map((i) => i.name).toList(); const kDefaultUriScheme = SupportedUriSchemes.https; final kLocalhostRegex = RegExp(r'^localhost(:\d+)?(/.*)?$'); +final kIPHostRegex = + RegExp(r'^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}(:\d+)?(/.*)?$'); const kMethodsWithBody = [ HTTPVerb.post, diff --git a/packages/apidash_core/lib/utils/uri_utils.dart b/packages/apidash_core/lib/utils/uri_utils.dart index 339e5393..a330e582 100644 --- a/packages/apidash_core/lib/utils/uri_utils.dart +++ b/packages/apidash_core/lib/utils/uri_utils.dart @@ -30,13 +30,8 @@ String stripUrlParams(String url) { return (null, "URL is missing!"); } - if (kLocalhostRegex.hasMatch(url)) { + if (kLocalhostRegex.hasMatch(url) || kIPHostRegex.hasMatch(url)) { url = '${SupportedUriSchemes.http.name}://$url'; - } else { - final hasScheme = RegExp(r'^[a-zA-Z][a-zA-Z0-9+.-]*://').hasMatch(url); - if (!hasScheme) { - url = "${defaultUriScheme.name}://$url"; - } } Uri? uri = Uri.tryParse(url); diff --git a/packages/apidash_core/test/utils/uri_utils_test.dart b/packages/apidash_core/test/utils/uri_utils_test.dart index 34758e08..e92ab1fa 100644 --- a/packages/apidash_core/test/utils/uri_utils_test.dart +++ b/packages/apidash_core/test/utils/uri_utils_test.dart @@ -62,6 +62,37 @@ void main() { expect(getValidRequestUri(url1, []), (uri1Expected, null)); }); + test('Testing getValidRequestUri with IP URL without port or path', () { + String url1 = "8.8.8.8"; + Uri uri1Expected = Uri(scheme: 'http', host: '8.8.8.8'); + expect(getValidRequestUri(url1, []), (uri1Expected, null)); + }); + + test('Testing getValidRequestUri with IP URL with port', () { + String url1 = "8.8.8.8:8080"; + Uri uri1Expected = Uri(scheme: 'http', host: '8.8.8.8', port: 8080); + expect(getValidRequestUri(url1, []), (uri1Expected, null)); + }); + + test('Testing getValidRequestUri with IP URL with port and path', () { + String url1 = "8.8.8.8:8080/hello"; + Uri uri1Expected = + Uri(scheme: 'http', host: '8.8.8.8', port: 8080, path: '/hello'); + expect(getValidRequestUri(url1, []), (uri1Expected, null)); + }); + + test('Testing getValidRequestUri with IP URL with http prefix', () { + String url1 = "http://8.8.8.8:3080"; + Uri uri1Expected = Uri(scheme: 'http', host: '8.8.8.8', port: 3080); + expect(getValidRequestUri(url1, []), (uri1Expected, null)); + }); + + test('Testing getValidRequestUri with IP URL with https prefix', () { + String url1 = "https://8.8.8.8:8080"; + Uri uri1Expected = Uri(scheme: 'https', host: '8.8.8.8', port: 8080); + expect(getValidRequestUri(url1, []), (uri1Expected, null)); + }); + test('Testing getValidRequestUri for normal values', () { String url1 = "https://api.apidash.dev/country/data"; const kvRow1 = NameValueModel(name: "code", value: "US");