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

View File

@ -1,4 +1,3 @@
import 'package:better_networking/better_networking.dart';
import 'package:test/test.dart';
@ -6,10 +5,11 @@ 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');
scheme: 'https',
host: 'dart.dev',
path: 'guides/libraries/library-tour',
fragment: 'numbers',
);
String uriScheme1Expected = 'https';
expect(getUriScheme(uri1), (uriScheme1Expected, true));
});
@ -29,12 +29,14 @@ void main() {
});
group("Testing getValidRequestUri", () {
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(
'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('Testing getValidRequestUri with localhost URL with port', () {
String url1 = "localhost:8080";
@ -42,13 +44,19 @@ void main() {
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 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";
@ -76,8 +84,12 @@ void main() {
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');
Uri uri1Expected = Uri(
scheme: 'http',
host: '8.8.8.8',
port: 8080,
path: '/hello',
);
expect(getValidRequestUri(url1, []), (uri1Expected, null));
});
@ -97,10 +109,11 @@ void main() {
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'});
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', () {
@ -115,43 +128,51 @@ void main() {
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'});
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');
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)"));
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(
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));
});
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'});
scheme: 'https',
host: 'api.apidash.dev',
path: 'country/data',
queryParameters: {'code': 'US'},
);
expect(getValidRequestUri(url7, null), (uri7Expected, null));
});
});