diff --git a/test/extensions/string_extensions_test.dart b/packages/apidash_core/test/extensions/string_extensions_test.dart similarity index 100% rename from test/extensions/string_extensions_test.dart rename to packages/apidash_core/test/extensions/string_extensions_test.dart diff --git a/test/models/form_data_model_test.dart b/packages/apidash_core/test/models/form_data_model_test.dart similarity index 100% rename from test/models/form_data_model_test.dart rename to packages/apidash_core/test/models/form_data_model_test.dart diff --git a/test/models/name_value_model_test.dart b/packages/apidash_core/test/models/name_value_model_test.dart similarity index 100% rename from test/models/name_value_model_test.dart rename to packages/apidash_core/test/models/name_value_model_test.dart diff --git a/packages/apidash_core/test/utils/https_response_utils.dart b/packages/apidash_core/test/utils/https_response_utils.dart new file mode 100644 index 00000000..07ff4336 --- /dev/null +++ b/packages/apidash_core/test/utils/https_response_utils.dart @@ -0,0 +1,190 @@ +import 'package:apidash_core/utils/http_response_utils.dart'; +import 'package:apidash_core/utils/string_utils.dart'; +import 'package:http_parser/http_parser.dart'; +import 'package:test/test.dart'; + +void main() { + group("Testing getContentTypeFromHeaders function", () { + test('Testing getContentTypeFromHeaders for header1', () { + Map header1 = { + "content-type": "application/json", + }; + String contentType1Expected = "application/json"; + expect(getContentTypeFromHeaders(header1), contentType1Expected); + }); + test('Testing getContentTypeFromHeaders for null headers', () { + expect(getContentTypeFromHeaders(null), null); + }); + test( + 'Testing getContentTypeFromHeaders when header keys are in header case', + () { + Map header2 = { + "Content-Type": "application/json", + }; + expect(getContentTypeFromHeaders(header2), null); + }); + }); + + group('Testing getMediaTypeFromContentType function', () { + test('Testing getMediaTypeFromContentType for json type', () { + String contentType1 = "application/json"; + MediaType mediaType1Expected = MediaType("application", "json"); + expect(getMediaTypeFromContentType(contentType1).toString(), + mediaType1Expected.toString()); + }); + test('Testing getMediaTypeFromContentType for null', () { + expect(getMediaTypeFromContentType(null), null); + }); + test('Testing getMediaTypeFromContentType for image svg+xml type', () { + String contentType3 = "image/svg+xml"; + MediaType mediaType3Expected = MediaType("image", "svg+xml"); + expect(getMediaTypeFromContentType(contentType3).toString(), + mediaType3Expected.toString()); + }); + test('Testing getMediaTypeFromContentType for incorrect content type', () { + String contentType4 = "text/html : charset=utf-8"; + expect(getMediaTypeFromContentType(contentType4), null); + }); + test('Testing getMediaTypeFromContentType for text/css type', () { + String contentType5 = "text/css; charset=utf-8"; + MediaType mediaType5Expected = + MediaType("text", "css", {"charset": "utf-8"}); + expect(getMediaTypeFromContentType(contentType5).toString(), + mediaType5Expected.toString()); + }); + test('Testing getMediaTypeFromContentType for incorrect with double ;', () { + String contentType6 = + "application/xml; charset=utf-16be ; date=21/03/2023"; + expect(getMediaTypeFromContentType(contentType6), null); + }); + test('Testing getMediaTypeFromContentType for empty content type', () { + expect(getMediaTypeFromContentType(""), null); + }); + test('Testing getMediaTypeFromContentType for missing subtype', () { + String contentType7 = "audio"; + expect(getMediaTypeFromContentType(contentType7), null); + }); + test('Testing getMediaTypeFromContentType for missing Type', () { + String contentType8 = "/html"; + expect(getMediaTypeFromContentType(contentType8), null); + }); + }); + + group("Testing getMediaTypeFromHeaders", () { + test('Testing getMediaTypeFromHeaders for basic case', () { + Map header1 = { + "content-length": "4506", + "cache-control": "private", + "content-type": "application/json" + }; + MediaType mediaType1Expected = MediaType("application", "json"); + expect(getMediaTypeFromHeaders(header1).toString(), + mediaType1Expected.toString()); + }); + test('Testing getMediaTypeFromHeaders for null header', () { + expect(getMediaTypeFromHeaders(null), null); + }); + test('Testing getMediaTypeFromHeaders for incomplete header value', () { + Map header2 = {"content-length": "4506"}; + expect(getMediaTypeFromHeaders(header2), null); + }); + test('Testing getMediaTypeFromHeaders for empty header value', () { + Map header3 = {"content-type": ""}; + expect(getMediaTypeFromHeaders(header3), null); + }); + test( + 'Testing getMediaTypeFromHeaders for erroneous header value - missing type', + () { + Map header4 = {"content-type": "/json"}; + expect(getMediaTypeFromHeaders(header4), null); + }); + test( + 'Testing getMediaTypeFromHeaders for erroneous header value - missing subtype', + () { + Map header5 = {"content-type": "application"}; + expect(getMediaTypeFromHeaders(header5), null); + }); + test('Testing getMediaTypeFromHeaders for header6', () { + Map header6 = {"content-type": "image/svg+xml"}; + MediaType mediaType6Expected = MediaType("image", "svg+xml"); + expect(getMediaTypeFromHeaders(header6).toString(), + mediaType6Expected.toString()); + }); + }); + + group("Testing formatBody", () { + test('Testing formatBody for null values', () { + expect(formatBody(null, null), null); + }); + test('Testing formatBody for null body values', () { + MediaType mediaType1 = MediaType("application", "xml"); + expect(formatBody(null, mediaType1), null); + }); + test('Testing formatBody for null MediaType values', () { + String body1 = ''' + { + "text":"The Chosen One"; + } +'''; + expect(formatBody(body1, null), null); + }); + test('Testing formatBody for json subtype values', () { + String body2 = '''{"data":{"area":9831510.0,"population":331893745}}'''; + MediaType mediaType2 = MediaType("application", "json"); + String result2Expected = '''{ + "data": { + "area": 9831510.0, + "population": 331893745 + } +}'''; + expect(formatBody(body2, mediaType2), result2Expected); + }); + test('Testing formatBody for xml subtype values', () { + String body3 = ''' + + +Belgian Waffles +5.95 USD +Two of our famous Belgian Waffles with plenty of real maple syrup +650 + + +'''; + MediaType mediaType3 = MediaType("application", "xml"); + String result3Expected = ''' + + Belgian Waffles + 5.95 USD + Two of our famous Belgian Waffles with plenty of real maple syrup + 650 + +'''; + expect(formatBody(body3, mediaType3), result3Expected); + }); + group("Testing formatBody for html", () { + MediaType mediaTypeHtml = MediaType("text", "html"); + test('Testing formatBody for html subtype values', () { + String body4 = ''' + +

My First Heading

+

My first paragraph.

+ +'''; + expect(formatBody(body4, mediaTypeHtml), body4); + }); + + test('Testing formatBody for html subtype values with random values', () { + String body5 = + '''${RandomStringGenerator.getRandomStringLines(100, 10000)}'''; + expect(formatBody(body5, mediaTypeHtml), null); + }); + test( + 'Testing formatBody for html subtype values with random values within limit', + () { + String body6 = + '''${RandomStringGenerator.getRandomStringLines(100, 190)}'''; + expect(formatBody(body6, mediaTypeHtml), body6); + }); + }); + }); +} diff --git a/packages/apidash_core/test/utils/uri_utils_test.dart b/packages/apidash_core/test/utils/uri_utils_test.dart new file mode 100644 index 00000000..6b8987dd --- /dev/null +++ b/packages/apidash_core/test/utils/uri_utils_test.dart @@ -0,0 +1,94 @@ +import 'package:apidash_core/models/models.dart'; +import 'package:apidash_core/utils/uri_utils.dart'; +import 'package:test/test.dart'; + +void main() { + group("Testing getUriScheme", () { + test('Testing getUriScheme for https', () { + Uri uri1 = Uri( + scheme: 'https', + host: 'dart.dev', + path: 'guides/libraries/library-tour', + fragment: 'numbers'); + String uriScheme1Expected = 'https'; + expect(getUriScheme(uri1), (uriScheme1Expected, true)); + }); + test('Testing getUriScheme for mailto scheme value', () { + Uri uri2 = Uri(scheme: 'mailto'); + String uriScheme2Expected = 'mailto'; + expect(getUriScheme(uri2), (uriScheme2Expected, false)); + }); + test('Testing getUriScheme for empty scheme value', () { + Uri uri3 = Uri(scheme: ''); + expect(getUriScheme(uri3), (null, false)); + }); + test('Testing getUriScheme for null scheme value', () { + Uri uri4 = Uri(scheme: null); + expect(getUriScheme(uri4), (null, false)); + }); + }); + + group("Testing getValidRequestUri", () { + test('Testing getValidRequestUri for normal values', () { + String url1 = "https://api.apidash.dev/country/data"; + const kvRow1 = NameValueModel(name: "code", value: "US"); + Uri uri1Expected = Uri( + scheme: 'https', + host: 'api.apidash.dev', + path: 'country/data', + queryParameters: {'code': 'US'}); + expect(getValidRequestUri(url1, [kvRow1]), (uri1Expected, null)); + }); + test('Testing getValidRequestUri for null url value', () { + const kvRow2 = NameValueModel(name: "code", value: "US"); + expect(getValidRequestUri(null, [kvRow2]), (null, "URL is missing!")); + }); + test('Testing getValidRequestUri for empty url value', () { + const kvRow3 = NameValueModel(name: "", value: ""); + expect(getValidRequestUri("", [kvRow3]), (null, "URL is missing!")); + }); + test('Testing getValidRequestUri when https is not provided in url', () { + String url4 = "api.apidash.dev/country/data"; + const kvRow4 = NameValueModel(name: "code", value: "US"); + Uri uri4Expected = Uri( + scheme: 'https', + host: 'api.apidash.dev', + path: 'country/data', + queryParameters: {'code': 'US'}); + expect(getValidRequestUri(url4, [kvRow4]), (uri4Expected, null)); + }); + test('Testing getValidRequestUri when url has fragment', () { + String url5 = "https://dart.dev/guides/libraries/library-tour#numbers"; + Uri uri5Expected = Uri( + scheme: 'https', + host: 'dart.dev', + path: '/guides/libraries/library-tour'); + expect(getValidRequestUri(url5, null), (uri5Expected, null)); + }); + test('Testing getValidRequestUri when uri scheme is not supported', () { + String url5 = "mailto:someone@example.com"; + expect(getValidRequestUri(url5, null), + (null, "Unsupported URL Scheme (mailto)")); + }); + test('Testing getValidRequestUri when query params in both url and kvrow', + () { + String url6 = "api.apidash.dev/country/data?code=IND"; + const kvRow6 = NameValueModel(name: "code", value: "US"); + Uri uri6Expected = Uri( + scheme: 'https', + host: 'api.apidash.dev', + path: 'country/data', + queryParameters: {'code': 'US'}); + expect(getValidRequestUri(url6, [kvRow6]), (uri6Expected, null)); + }); + test('Testing getValidRequestUri when kvrow is null', () { + String url7 = "api.apidash.dev/country/data?code=US"; + Uri uri7Expected = Uri( + scheme: 'https', + host: 'api.apidash.dev', + path: 'country/data', + queryParameters: {'code': 'US'}); + expect(getValidRequestUri(url7, null), (uri7Expected, null)); + }); + }); +} diff --git a/test/test_utilities.dart b/test/test_utilities.dart deleted file mode 100644 index 3f5c3fc1..00000000 --- a/test/test_utilities.dart +++ /dev/null @@ -1,19 +0,0 @@ -import 'dart:math'; - -class RandomStringGenerator { - static const _chars = - 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890'; - static Random rnd = Random(); - - static String getRandomString(int length) => - String.fromCharCodes(Iterable.generate( - length, (_) => _chars.codeUnitAt(rnd.nextInt(_chars.length)))); - - static String getRandomStringLines(int lines, int length) { - List result = []; - for (var i = 0; i < lines; i++) { - result.add(getRandomString(length)); - } - return result.join('\n'); - } -} diff --git a/test/utils/http_utils_test.dart b/test/utils/http_utils_test.dart index 6ff34757..0f777ab1 100644 --- a/test/utils/http_utils_test.dart +++ b/test/utils/http_utils_test.dart @@ -1,8 +1,7 @@ -import 'package:apidash_core/apidash_core.dart'; +import 'package:apidash_core/apidash_core.dart' show MediaType; import 'package:test/test.dart'; import 'package:apidash/utils/http_utils.dart'; import 'package:apidash/consts.dart'; -import '../test_utilities.dart'; void main() { group("Testing getRequestTitleFromUrl function", () { @@ -39,203 +38,6 @@ void main() { }); }); - group("Testing getContentTypeFromHeaders function", () { - test('Testing getContentTypeFromHeaders for header1', () { - Map header1 = { - "content-type": "application/json", - }; - String contentType1Expected = "application/json"; - expect(getContentTypeFromHeaders(header1), contentType1Expected); - }); - test('Testing getContentTypeFromHeaders for null headers', () { - expect(getContentTypeFromHeaders(null), null); - }); - test( - 'Testing getContentTypeFromHeaders when header keys are in header case', - () { - Map header2 = { - "Content-Type": "application/json", - }; - expect(getContentTypeFromHeaders(header2), null); - }); - }); - - group('Testing getMediaTypeFromContentType function', () { - test('Testing getMediaTypeFromContentType for json type', () { - String contentType1 = "application/json"; - MediaType mediaType1Expected = MediaType("application", "json"); - expect(getMediaTypeFromContentType(contentType1).toString(), - mediaType1Expected.toString()); - }); - test('Testing getMediaTypeFromContentType for null', () { - expect(getMediaTypeFromContentType(null), null); - }); - test('Testing getMediaTypeFromContentType for image svg+xml type', () { - String contentType3 = "image/svg+xml"; - MediaType mediaType3Expected = MediaType("image", "svg+xml"); - expect(getMediaTypeFromContentType(contentType3).toString(), - mediaType3Expected.toString()); - }); - test('Testing getMediaTypeFromContentType for incorrect content type', () { - String contentType4 = "text/html : charset=utf-8"; - expect(getMediaTypeFromContentType(contentType4), null); - }); - test('Testing getMediaTypeFromContentType for text/css type', () { - String contentType5 = "text/css; charset=utf-8"; - MediaType mediaType5Expected = - MediaType("text", "css", {"charset": "utf-8"}); - expect(getMediaTypeFromContentType(contentType5).toString(), - mediaType5Expected.toString()); - }); - test('Testing getMediaTypeFromContentType for incorrect with double ;', () { - String contentType6 = - "application/xml; charset=utf-16be ; date=21/03/2023"; - expect(getMediaTypeFromContentType(contentType6), null); - }); - test('Testing getMediaTypeFromContentType for empty content type', () { - expect(getMediaTypeFromContentType(""), null); - }); - test('Testing getMediaTypeFromContentType for missing subtype', () { - String contentType7 = "audio"; - expect(getMediaTypeFromContentType(contentType7), null); - }); - test('Testing getMediaTypeFromContentType for missing Type', () { - String contentType8 = "/html"; - expect(getMediaTypeFromContentType(contentType8), null); - }); - }); - - group("Testing getMediaTypeFromHeaders", () { - test('Testing getMediaTypeFromHeaders for basic case', () { - Map header1 = { - "content-length": "4506", - "cache-control": "private", - "content-type": "application/json" - }; - MediaType mediaType1Expected = MediaType("application", "json"); - expect(getMediaTypeFromHeaders(header1).toString(), - mediaType1Expected.toString()); - }); - test('Testing getMediaTypeFromHeaders for null header', () { - expect(getMediaTypeFromHeaders(null), null); - }); - test('Testing getMediaTypeFromHeaders for incomplete header value', () { - Map header2 = {"content-length": "4506"}; - expect(getMediaTypeFromHeaders(header2), null); - }); - test('Testing getMediaTypeFromHeaders for empty header value', () { - Map header3 = {"content-type": ""}; - expect(getMediaTypeFromHeaders(header3), null); - }); - test( - 'Testing getMediaTypeFromHeaders for erroneous header value - missing type', - () { - Map header4 = {"content-type": "/json"}; - expect(getMediaTypeFromHeaders(header4), null); - }); - test( - 'Testing getMediaTypeFromHeaders for erroneous header value - missing subtype', - () { - Map header5 = {"content-type": "application"}; - expect(getMediaTypeFromHeaders(header5), null); - }); - test('Testing getMediaTypeFromHeaders for header6', () { - Map header6 = {"content-type": "image/svg+xml"}; - MediaType mediaType6Expected = MediaType("image", "svg+xml"); - expect(getMediaTypeFromHeaders(header6).toString(), - mediaType6Expected.toString()); - }); - }); - - group("Testing getUriScheme", () { - test('Testing getUriScheme for https', () { - Uri uri1 = Uri( - scheme: 'https', - host: 'dart.dev', - path: 'guides/libraries/library-tour', - fragment: 'numbers'); - String uriScheme1Expected = 'https'; - expect(getUriScheme(uri1), (uriScheme1Expected, true)); - }); - test('Testing getUriScheme for mailto scheme value', () { - Uri uri2 = Uri(scheme: 'mailto'); - String uriScheme2Expected = 'mailto'; - expect(getUriScheme(uri2), (uriScheme2Expected, false)); - }); - test('Testing getUriScheme for empty scheme value', () { - Uri uri3 = Uri(scheme: ''); - expect(getUriScheme(uri3), (null, false)); - }); - test('Testing getUriScheme for null scheme value', () { - Uri uri4 = Uri(scheme: null); - expect(getUriScheme(uri4), (null, false)); - }); - }); - - group("Testing getValidRequestUri", () { - test('Testing getValidRequestUri for normal values', () { - String url1 = "https://api.apidash.dev/country/data"; - const kvRow1 = NameValueModel(name: "code", value: "US"); - Uri uri1Expected = Uri( - scheme: 'https', - host: 'api.apidash.dev', - path: 'country/data', - queryParameters: {'code': 'US'}); - expect(getValidRequestUri(url1, [kvRow1]), (uri1Expected, null)); - }); - test('Testing getValidRequestUri for null url value', () { - const kvRow2 = NameValueModel(name: "code", value: "US"); - expect(getValidRequestUri(null, [kvRow2]), (null, "URL is missing!")); - }); - test('Testing getValidRequestUri for empty url value', () { - const kvRow3 = NameValueModel(name: "", value: ""); - expect(getValidRequestUri("", [kvRow3]), (null, "URL is missing!")); - }); - test('Testing getValidRequestUri when https is not provided in url', () { - String url4 = "api.apidash.dev/country/data"; - const kvRow4 = NameValueModel(name: "code", value: "US"); - Uri uri4Expected = Uri( - scheme: 'https', - host: 'api.apidash.dev', - path: 'country/data', - queryParameters: {'code': 'US'}); - expect(getValidRequestUri(url4, [kvRow4]), (uri4Expected, null)); - }); - test('Testing getValidRequestUri when url has fragment', () { - String url5 = "https://dart.dev/guides/libraries/library-tour#numbers"; - Uri uri5Expected = Uri( - scheme: 'https', - host: 'dart.dev', - path: '/guides/libraries/library-tour'); - expect(getValidRequestUri(url5, null), (uri5Expected, null)); - }); - test('Testing getValidRequestUri when uri scheme is not supported', () { - String url5 = "mailto:someone@example.com"; - expect(getValidRequestUri(url5, null), - (null, "Unsupported URL Scheme (mailto)")); - }); - test('Testing getValidRequestUri when query params in both url and kvrow', - () { - String url6 = "api.apidash.dev/country/data?code=IND"; - const kvRow6 = NameValueModel(name: "code", value: "US"); - Uri uri6Expected = Uri( - scheme: 'https', - host: 'api.apidash.dev', - path: 'country/data', - queryParameters: {'code': 'US'}); - expect(getValidRequestUri(url6, [kvRow6]), (uri6Expected, null)); - }); - test('Testing getValidRequestUri when kvrow is null', () { - String url7 = "api.apidash.dev/country/data?code=US"; - Uri uri7Expected = Uri( - scheme: 'https', - host: 'api.apidash.dev', - path: 'country/data', - queryParameters: {'code': 'US'}); - expect(getValidRequestUri(url7, null), (uri7Expected, null)); - }); - }); - group("Testing getResponseBodyViewOptions", () { test('Testing getResponseBodyViewOptions for application/json', () { MediaType mediaType1 = MediaType("application", "json"); @@ -304,80 +106,4 @@ void main() { expect(result10.$2, "calendar"); }); }); - - group("Testing formatBody", () { - test('Testing formatBody for null values', () { - expect(formatBody(null, null), null); - }); - test('Testing formatBody for null body values', () { - MediaType mediaType1 = MediaType("application", "xml"); - expect(formatBody(null, mediaType1), null); - }); - test('Testing formatBody for null MediaType values', () { - String body1 = ''' - { - "text":"The Chosen One"; - } -'''; - expect(formatBody(body1, null), null); - }); - test('Testing formatBody for json subtype values', () { - String body2 = '''{"data":{"area":9831510.0,"population":331893745}}'''; - MediaType mediaType2 = MediaType("application", "json"); - String result2Expected = '''{ - "data": { - "area": 9831510.0, - "population": 331893745 - } -}'''; - expect(formatBody(body2, mediaType2), result2Expected); - }); - test('Testing formatBody for xml subtype values', () { - String body3 = ''' - - -Belgian Waffles -5.95 USD -Two of our famous Belgian Waffles with plenty of real maple syrup -650 - - -'''; - MediaType mediaType3 = MediaType("application", "xml"); - String result3Expected = ''' - - Belgian Waffles - 5.95 USD - Two of our famous Belgian Waffles with plenty of real maple syrup - 650 - -'''; - expect(formatBody(body3, mediaType3), result3Expected); - }); - group("Testing formatBody for html", () { - MediaType mediaTypeHtml = MediaType("text", "html"); - test('Testing formatBody for html subtype values', () { - String body4 = ''' - -

My First Heading

-

My first paragraph.

- -'''; - expect(formatBody(body4, mediaTypeHtml), body4); - }); - - test('Testing formatBody for html subtype values with random values', () { - String body5 = - '''${RandomStringGenerator.getRandomStringLines(100, 10000)}'''; - expect(formatBody(body5, mediaTypeHtml), null); - }); - test( - 'Testing formatBody for html subtype values with random values within limit', - () { - String body6 = - '''${RandomStringGenerator.getRandomStringLines(100, 190)}'''; - expect(formatBody(body6, mediaTypeHtml), body6); - }); - }); - }); }