From 053f7b8f17eeb32abfb78326630825da63a4b2e4 Mon Sep 17 00:00:00 2001 From: "aditya.chaudhary1558@gmail.com" Date: Fri, 1 Nov 2024 16:21:26 +0530 Subject: [PATCH 1/4] feat(uriScheme): Added the support for uriScheme without prefix http/https --- packages/apidash_core/lib/utils/uri_utils.dart | 5 +++++ test/utils/http_utils_test.dart | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/packages/apidash_core/lib/utils/uri_utils.dart b/packages/apidash_core/lib/utils/uri_utils.dart index fd755db2..2ff4bbf8 100644 --- a/packages/apidash_core/lib/utils/uri_utils.dart +++ b/packages/apidash_core/lib/utils/uri_utils.dart @@ -1,4 +1,5 @@ import 'package:collection/collection.dart' show mergeMaps; +import 'package:flutter/foundation.dart'; import '../consts.dart'; import '../models/name_value_model.dart'; import 'http_request_utils.dart'; @@ -29,6 +30,10 @@ String stripUrlParams(String url) { if (url == null || url == "") { return (null, "URL is missing!"); } + final localhostRegex = RegExp(r'^localhost(:\d+)?(/.*)?$'); + if (localhostRegex.hasMatch(url)) { + url = 'http://$url'; + } Uri? uri = Uri.tryParse(url); if (uri == null) { return (null, "Check URL (malformed)"); diff --git a/test/utils/http_utils_test.dart b/test/utils/http_utils_test.dart index 6ff34757..6db69cfa 100644 --- a/test/utils/http_utils_test.dart +++ b/test/utils/http_utils_test.dart @@ -183,6 +183,23 @@ void main() { queryParameters: {'code': 'US'}); expect(getValidRequestUri(url1, [kvRow1]), (uri1Expected, null)); }); + test('Testing getValidRequestUri with localhost URL', () { + // Test case for localhost without port or path + String url1 = "localhost"; + Uri uri1Expected = Uri(scheme: 'http', host: 'localhost'); + expect(getValidRequestUri(url1, []), (uri1Expected, null)); + + // Test case for localhost with port + String url2 = "localhost:8080"; + Uri uri2Expected = Uri(scheme: 'http', host: 'localhost', port: 8080); + expect(getValidRequestUri(url2, []), (uri2Expected, null)); + + // Test case for localhost with port and path + String url3 = "localhost:8080/hello"; + Uri uri3Expected = + Uri(scheme: 'http', host: 'localhost', port: 8080, path: '/hello'); + expect(getValidRequestUri(url3, []), (uri3Expected, null)); + }); test('Testing getValidRequestUri for null url value', () { const kvRow2 = NameValueModel(name: "code", value: "US"); expect(getValidRequestUri(null, [kvRow2]), (null, "URL is missing!")); From 4f09e1d28a5683d121c1998fd337ca1c18b3afc6 Mon Sep 17 00:00:00 2001 From: Ashita Prasad Date: Sat, 2 Nov 2024 19:48:18 +0530 Subject: [PATCH 2/4] unused import --- packages/apidash_core/lib/utils/uri_utils.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/apidash_core/lib/utils/uri_utils.dart b/packages/apidash_core/lib/utils/uri_utils.dart index 2ff4bbf8..f2b44f21 100644 --- a/packages/apidash_core/lib/utils/uri_utils.dart +++ b/packages/apidash_core/lib/utils/uri_utils.dart @@ -1,5 +1,4 @@ import 'package:collection/collection.dart' show mergeMaps; -import 'package:flutter/foundation.dart'; import '../consts.dart'; import '../models/name_value_model.dart'; import 'http_request_utils.dart'; From 44f9d10c8ca7411c8278d14d3a6a5e2349e5d5b7 Mon Sep 17 00:00:00 2001 From: Ashita Prasad Date: Sat, 2 Nov 2024 19:58:52 +0530 Subject: [PATCH 3/4] Restructure and add more tests --- test/utils/http_utils_test.dart | 42 +++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/test/utils/http_utils_test.dart b/test/utils/http_utils_test.dart index 6db69cfa..902692fa 100644 --- a/test/utils/http_utils_test.dart +++ b/test/utils/http_utils_test.dart @@ -183,23 +183,39 @@ void main() { queryParameters: {'code': 'US'}); expect(getValidRequestUri(url1, [kvRow1]), (uri1Expected, null)); }); - test('Testing getValidRequestUri with localhost URL', () { - // Test case for localhost without port or path + test('Testing getValidRequestUri with localhost URL without port or path', + () { String url1 = "localhost"; Uri uri1Expected = Uri(scheme: 'http', host: 'localhost'); expect(getValidRequestUri(url1, []), (uri1Expected, null)); - - // Test case for localhost with port - String url2 = "localhost:8080"; - Uri uri2Expected = Uri(scheme: 'http', host: 'localhost', port: 8080); - expect(getValidRequestUri(url2, []), (uri2Expected, null)); - - // Test case for localhost with port and path - String url3 = "localhost:8080/hello"; - Uri uri3Expected = - Uri(scheme: 'http', host: 'localhost', port: 8080, path: '/hello'); - expect(getValidRequestUri(url3, []), (uri3Expected, null)); }); + + test('Testing getValidRequestUri with localhost URL with port', () { + String url1 = "localhost:8080"; + Uri uri1Expected = Uri(scheme: 'http', host: 'localhost', port: 8080); + expect(getValidRequestUri(url1, []), (uri1Expected, null)); + }); + + test('Testing getValidRequestUri with localhost URL with port and path', + () { + String url1 = "localhost:8080/hello"; + Uri uri1Expected = + Uri(scheme: 'http', host: 'localhost', port: 8080, path: '/hello'); + expect(getValidRequestUri(url1, []), (uri1Expected, null)); + }); + + test('Testing getValidRequestUri with localhost URL with http prefix', () { + String url1 = "http://localhost:3080"; + Uri uri1Expected = Uri(scheme: 'http', host: 'localhost', port: 3080); + expect(getValidRequestUri(url1, []), (uri1Expected, null)); + }); + + test('Testing getValidRequestUri with localhost URL with https prefix', () { + String url1 = "https://localhost:8080"; + Uri uri1Expected = Uri(scheme: 'https', host: 'localhost', port: 8080); + expect(getValidRequestUri(url1, []), (uri1Expected, null)); + }); + test('Testing getValidRequestUri for null url value', () { const kvRow2 = NameValueModel(name: "code", value: "US"); expect(getValidRequestUri(null, [kvRow2]), (null, "URL is missing!")); From 69753a5f26a7ce052764a25a5a865081e3d81d64 Mon Sep 17 00:00:00 2001 From: Ashita Prasad Date: Sat, 2 Nov 2024 20:11:45 +0530 Subject: [PATCH 4/4] refactor code --- packages/apidash_core/lib/consts.dart | 7 ++++++- packages/apidash_core/lib/utils/uri_utils.dart | 6 +++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/apidash_core/lib/consts.dart b/packages/apidash_core/lib/consts.dart index 3493c40d..d2544d04 100644 --- a/packages/apidash_core/lib/consts.dart +++ b/packages/apidash_core/lib/consts.dart @@ -4,8 +4,13 @@ enum HTTPVerb { get, head, post, put, patch, delete } enum FormDataType { text, file } -const kSupportedUriSchemes = ["https", "http"]; +enum SupportedUriSchemes { https, http } + +final kSupportedUriSchemes = + SupportedUriSchemes.values.map((i) => i.name).toList(); const kDefaultUriScheme = "https"; +final kLocalhostRegex = RegExp(r'^localhost(:\d+)?(/.*)?$'); + const kMethodsWithBody = [ HTTPVerb.post, HTTPVerb.put, diff --git a/packages/apidash_core/lib/utils/uri_utils.dart b/packages/apidash_core/lib/utils/uri_utils.dart index f2b44f21..09c159ab 100644 --- a/packages/apidash_core/lib/utils/uri_utils.dart +++ b/packages/apidash_core/lib/utils/uri_utils.dart @@ -29,9 +29,9 @@ String stripUrlParams(String url) { if (url == null || url == "") { return (null, "URL is missing!"); } - final localhostRegex = RegExp(r'^localhost(:\d+)?(/.*)?$'); - if (localhostRegex.hasMatch(url)) { - url = 'http://$url'; + + if (kLocalhostRegex.hasMatch(url)) { + url = '${SupportedUriSchemes.http.name}://$url'; } Uri? uri = Uri.tryParse(url); if (uri == null) {