This commit is contained in:
Ankit Mahato
2025-06-29 06:09:33 +05:30
parent 39cd3a7e26
commit 4384557c18
2 changed files with 111 additions and 77 deletions

View File

@ -1,4 +1,3 @@
import 'package:better_networking/better_networking.dart'; import 'package:better_networking/better_networking.dart';
import 'package:better_networking/utils/string_utils.dart'; import 'package:better_networking/utils/string_utils.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
@ -8,8 +7,10 @@ void main() {
test('Testing getMediaTypeFromContentType for json type', () { test('Testing getMediaTypeFromContentType for json type', () {
String contentType1 = "application/json"; String contentType1 = "application/json";
MediaType mediaType1Expected = MediaType("application", "json"); MediaType mediaType1Expected = MediaType("application", "json");
expect(getMediaTypeFromContentType(contentType1).toString(), expect(
mediaType1Expected.toString()); getMediaTypeFromContentType(contentType1).toString(),
mediaType1Expected.toString(),
);
}); });
test('Testing getMediaTypeFromContentType for null', () { test('Testing getMediaTypeFromContentType for null', () {
expect(getMediaTypeFromContentType(null), null); expect(getMediaTypeFromContentType(null), null);
@ -17,8 +18,10 @@ void main() {
test('Testing getMediaTypeFromContentType for image svg+xml type', () { test('Testing getMediaTypeFromContentType for image svg+xml type', () {
String contentType3 = "image/svg+xml"; String contentType3 = "image/svg+xml";
MediaType mediaType3Expected = MediaType("image", "svg+xml"); MediaType mediaType3Expected = MediaType("image", "svg+xml");
expect(getMediaTypeFromContentType(contentType3).toString(), expect(
mediaType3Expected.toString()); getMediaTypeFromContentType(contentType3).toString(),
mediaType3Expected.toString(),
);
}); });
test('Testing getMediaTypeFromContentType for incorrect content type', () { test('Testing getMediaTypeFromContentType for incorrect content type', () {
String contentType4 = "text/html : charset=utf-8"; String contentType4 = "text/html : charset=utf-8";
@ -26,10 +29,13 @@ void main() {
}); });
test('Testing getMediaTypeFromContentType for text/css type', () { test('Testing getMediaTypeFromContentType for text/css type', () {
String contentType5 = "text/css; charset=utf-8"; String contentType5 = "text/css; charset=utf-8";
MediaType mediaType5Expected = MediaType mediaType5Expected = MediaType("text", "css", {
MediaType("text", "css", {"charset": "utf-8"}); "charset": "utf-8",
expect(getMediaTypeFromContentType(contentType5).toString(), });
mediaType5Expected.toString()); expect(
getMediaTypeFromContentType(contentType5).toString(),
mediaType5Expected.toString(),
);
}); });
test('Testing getMediaTypeFromContentType for incorrect with double ;', () { test('Testing getMediaTypeFromContentType for incorrect with double ;', () {
String contentType6 = String contentType6 =
@ -54,11 +60,13 @@ void main() {
Map<String, String> header1 = { Map<String, String> header1 = {
"content-length": "4506", "content-length": "4506",
"cache-control": "private", "cache-control": "private",
"content-type": "application/json" "content-type": "application/json",
}; };
MediaType mediaType1Expected = MediaType("application", "json"); MediaType mediaType1Expected = MediaType("application", "json");
expect(getMediaTypeFromHeaders(header1).toString(), expect(
mediaType1Expected.toString()); getMediaTypeFromHeaders(header1).toString(),
mediaType1Expected.toString(),
);
}); });
test('Testing getMediaTypeFromHeaders for null header', () { test('Testing getMediaTypeFromHeaders for null header', () {
expect(getMediaTypeFromHeaders(null), null); expect(getMediaTypeFromHeaders(null), null);
@ -72,22 +80,26 @@ void main() {
expect(getMediaTypeFromHeaders(header3), null); expect(getMediaTypeFromHeaders(header3), null);
}); });
test( test(
'Testing getMediaTypeFromHeaders for erroneous header value - missing type', 'Testing getMediaTypeFromHeaders for erroneous header value - missing type',
() { () {
Map<String, String> header4 = {"content-type": "/json"}; Map<String, String> header4 = {"content-type": "/json"};
expect(getMediaTypeFromHeaders(header4), null); expect(getMediaTypeFromHeaders(header4), null);
}); },
);
test( test(
'Testing getMediaTypeFromHeaders for erroneous header value - missing subtype', 'Testing getMediaTypeFromHeaders for erroneous header value - missing subtype',
() { () {
Map<String, String> header5 = {"content-type": "application"}; Map<String, String> header5 = {"content-type": "application"};
expect(getMediaTypeFromHeaders(header5), null); expect(getMediaTypeFromHeaders(header5), null);
}); },
);
test('Testing getMediaTypeFromHeaders for header6', () { test('Testing getMediaTypeFromHeaders for header6', () {
Map<String, String> header6 = {"content-type": "image/svg+xml"}; Map<String, String> header6 = {"content-type": "image/svg+xml"};
MediaType mediaType6Expected = MediaType("image", "svg+xml"); MediaType mediaType6Expected = MediaType("image", "svg+xml");
expect(getMediaTypeFromHeaders(header6).toString(), expect(
mediaType6Expected.toString()); getMediaTypeFromHeaders(header6).toString(),
mediaType6Expected.toString(),
);
}); });
}); });
@ -158,12 +170,13 @@ void main() {
expect(formatBody(body5, mediaTypeHtml), null); expect(formatBody(body5, mediaTypeHtml), null);
}); });
test( test(
'Testing formatBody for html subtype values with random values within limit', 'Testing formatBody for html subtype values with random values within limit',
() { () {
String body6 = String body6 =
'''<html>${RandomStringGenerator.getRandomStringLines(100, 190)}</html>'''; '''<html>${RandomStringGenerator.getRandomStringLines(100, 190)}</html>''';
expect(formatBody(body6, mediaTypeHtml), body6); expect(formatBody(body6, mediaTypeHtml), body6);
}); },
);
}); });
}); });
} }

View File

@ -1,4 +1,3 @@
import 'package:better_networking/better_networking.dart'; import 'package:better_networking/better_networking.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
@ -6,10 +5,11 @@ void main() {
group("Testing getUriScheme", () { group("Testing getUriScheme", () {
test('Testing getUriScheme for https', () { test('Testing getUriScheme for https', () {
Uri uri1 = Uri( Uri uri1 = Uri(
scheme: 'https', scheme: 'https',
host: 'dart.dev', host: 'dart.dev',
path: 'guides/libraries/library-tour', path: 'guides/libraries/library-tour',
fragment: 'numbers'); fragment: 'numbers',
);
String uriScheme1Expected = 'https'; String uriScheme1Expected = 'https';
expect(getUriScheme(uri1), (uriScheme1Expected, true)); expect(getUriScheme(uri1), (uriScheme1Expected, true));
}); });
@ -29,12 +29,14 @@ void main() {
}); });
group("Testing getValidRequestUri", () { group("Testing getValidRequestUri", () {
test('Testing getValidRequestUri with localhost URL without port or path', test(
() { 'Testing getValidRequestUri with localhost URL without port or path',
String url1 = "localhost"; () {
Uri uri1Expected = Uri(scheme: 'http', host: 'localhost'); String url1 = "localhost";
expect(getValidRequestUri(url1, []), (uri1Expected, null)); Uri uri1Expected = Uri(scheme: 'http', host: 'localhost');
}); expect(getValidRequestUri(url1, []), (uri1Expected, null));
},
);
test('Testing getValidRequestUri with localhost URL with port', () { test('Testing getValidRequestUri with localhost URL with port', () {
String url1 = "localhost:8080"; String url1 = "localhost:8080";
@ -42,13 +44,19 @@ void main() {
expect(getValidRequestUri(url1, []), (uri1Expected, null)); expect(getValidRequestUri(url1, []), (uri1Expected, null));
}); });
test('Testing getValidRequestUri with localhost URL with port and path', test(
() { 'Testing getValidRequestUri with localhost URL with port and path',
String url1 = "localhost:8080/hello"; () {
Uri uri1Expected = String url1 = "localhost:8080/hello";
Uri(scheme: 'http', host: 'localhost', port: 8080, path: '/hello'); Uri uri1Expected = Uri(
expect(getValidRequestUri(url1, []), (uri1Expected, null)); scheme: 'http',
}); host: 'localhost',
port: 8080,
path: '/hello',
);
expect(getValidRequestUri(url1, []), (uri1Expected, null));
},
);
test('Testing getValidRequestUri with localhost URL with http prefix', () { test('Testing getValidRequestUri with localhost URL with http prefix', () {
String url1 = "http://localhost:3080"; String url1 = "http://localhost:3080";
@ -76,8 +84,12 @@ void main() {
test('Testing getValidRequestUri with IP URL with port and path', () { test('Testing getValidRequestUri with IP URL with port and path', () {
String url1 = "8.8.8.8:8080/hello"; String url1 = "8.8.8.8:8080/hello";
Uri uri1Expected = Uri uri1Expected = Uri(
Uri(scheme: 'http', host: '8.8.8.8', port: 8080, path: '/hello'); scheme: 'http',
host: '8.8.8.8',
port: 8080,
path: '/hello',
);
expect(getValidRequestUri(url1, []), (uri1Expected, null)); expect(getValidRequestUri(url1, []), (uri1Expected, null));
}); });
@ -97,10 +109,11 @@ void main() {
String url1 = "https://api.apidash.dev/country/data"; String url1 = "https://api.apidash.dev/country/data";
const kvRow1 = NameValueModel(name: "code", value: "US"); const kvRow1 = NameValueModel(name: "code", value: "US");
Uri uri1Expected = Uri( Uri uri1Expected = Uri(
scheme: 'https', scheme: 'https',
host: 'api.apidash.dev', host: 'api.apidash.dev',
path: 'country/data', path: 'country/data',
queryParameters: {'code': 'US'}); queryParameters: {'code': 'US'},
);
expect(getValidRequestUri(url1, [kvRow1]), (uri1Expected, null)); expect(getValidRequestUri(url1, [kvRow1]), (uri1Expected, null));
}); });
test('Testing getValidRequestUri for null url value', () { test('Testing getValidRequestUri for null url value', () {
@ -115,44 +128,52 @@ void main() {
String url4 = "api.apidash.dev/country/data"; String url4 = "api.apidash.dev/country/data";
const kvRow4 = NameValueModel(name: "code", value: "US"); const kvRow4 = NameValueModel(name: "code", value: "US");
Uri uri4Expected = Uri( Uri uri4Expected = Uri(
scheme: 'https', scheme: 'https',
host: 'api.apidash.dev', host: 'api.apidash.dev',
path: 'country/data', path: 'country/data',
queryParameters: {'code': 'US'}); queryParameters: {'code': 'US'},
);
expect(getValidRequestUri(url4, [kvRow4]), (uri4Expected, null)); expect(getValidRequestUri(url4, [kvRow4]), (uri4Expected, null));
}); });
test('Testing getValidRequestUri when url has fragment', () { test('Testing getValidRequestUri when url has fragment', () {
String url5 = "https://dart.dev/guides/libraries/library-tour#numbers"; String url5 = "https://dart.dev/guides/libraries/library-tour#numbers";
Uri uri5Expected = Uri( Uri uri5Expected = Uri(
scheme: 'https', scheme: 'https',
host: 'dart.dev', host: 'dart.dev',
path: '/guides/libraries/library-tour'); path: '/guides/libraries/library-tour',
);
expect(getValidRequestUri(url5, null), (uri5Expected, null)); expect(getValidRequestUri(url5, null), (uri5Expected, null));
}); });
test('Testing getValidRequestUri when uri scheme is not supported', () { test('Testing getValidRequestUri when uri scheme is not supported', () {
String url5 = "mailto:someone@example.com"; String url5 = "mailto:someone@example.com";
expect(getValidRequestUri(url5, null), expect(getValidRequestUri(url5, null), (
(null, "Unsupported URL Scheme (mailto)")); null,
"Unsupported URL Scheme (mailto)",
));
}); });
test('Testing getValidRequestUri when query params in both url and kvrow', 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"); String url6 = "api.apidash.dev/country/data?code=IND";
Uri uri6Expected = Uri( const kvRow6 = NameValueModel(name: "code", value: "US");
Uri uri6Expected = Uri(
scheme: 'https', scheme: 'https',
host: 'api.apidash.dev', host: 'api.apidash.dev',
path: 'country/data', path: 'country/data',
queryParameters: {'code': 'US'}); queryParameters: {'code': 'US'},
expect(getValidRequestUri(url6, [kvRow6]), (uri6Expected, null)); );
}); expect(getValidRequestUri(url6, [kvRow6]), (uri6Expected, null));
},
);
test('Testing getValidRequestUri when kvrow is null', () { test('Testing getValidRequestUri when kvrow is null', () {
String url7 = "api.apidash.dev/country/data?code=US"; String url7 = "api.apidash.dev/country/data?code=US";
Uri uri7Expected = Uri( Uri uri7Expected = Uri(
scheme: 'https', scheme: 'https',
host: 'api.apidash.dev', host: 'api.apidash.dev',
path: 'country/data', path: 'country/data',
queryParameters: {'code': 'US'}); queryParameters: {'code': 'US'},
);
expect(getValidRequestUri(url7, null), (uri7Expected, null)); expect(getValidRequestUri(url7, null), (uri7Expected, null));
}); });
}); });
} }