mirror of
https://github.com/foss42/apidash.git
synced 2025-06-28 03:19:40 +08:00
Adding new tests for http_utils
This commit is contained in:
15
test/test_utilities.dart
Normal file
15
test/test_utilities.dart
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import 'dart:math';
|
||||||
|
|
||||||
|
const _chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
|
||||||
|
Random _rnd = Random();
|
||||||
|
|
||||||
|
String getRandomString(int length) => String.fromCharCodes(Iterable.generate(
|
||||||
|
length, (_) => _chars.codeUnitAt(_rnd.nextInt(_chars.length))));
|
||||||
|
|
||||||
|
String getRandomStringLines(int lines, int length) {
|
||||||
|
List<String> result = [];
|
||||||
|
for (var i = 0; i < lines; i++) {
|
||||||
|
result.add(getRandomString(length));
|
||||||
|
}
|
||||||
|
return result.join('\n');
|
||||||
|
}
|
@ -3,6 +3,7 @@ import 'package:http_parser/http_parser.dart';
|
|||||||
import 'package:apidash/utils/http_utils.dart';
|
import 'package:apidash/utils/http_utils.dart';
|
||||||
import 'package:apidash/models/kvrow_model.dart';
|
import 'package:apidash/models/kvrow_model.dart';
|
||||||
import 'package:apidash/consts.dart';
|
import 'package:apidash/consts.dart';
|
||||||
|
import '../test_utilities.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
group("Testing getRequestTitleFromUrl function", () {
|
group("Testing getRequestTitleFromUrl function", () {
|
||||||
@ -149,28 +150,28 @@ void main() {
|
|||||||
|
|
||||||
group("Testing getUriScheme", () {
|
group("Testing getUriScheme", () {
|
||||||
test('Testing getUriScheme for https', () {
|
test('Testing getUriScheme for https', () {
|
||||||
Uri uriScheme1 = 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(uriScheme1), (uriScheme1Expected,true));
|
expect(getUriScheme(uri1), (uriScheme1Expected,true));
|
||||||
});
|
});
|
||||||
test('Testing getUriScheme for mailto scheme value', () {
|
test('Testing getUriScheme for mailto scheme value', () {
|
||||||
Uri uriScheme2 = Uri(scheme: 'mailto');
|
Uri uri2 = Uri(scheme: 'mailto');
|
||||||
String uriScheme2Expected = 'mailto';
|
String uriScheme2Expected = 'mailto';
|
||||||
expect(getUriScheme(uriScheme2), (uriScheme2Expected,false));
|
expect(getUriScheme(uri2), (uriScheme2Expected,false));
|
||||||
});
|
});
|
||||||
test('Testing getUriScheme for empty scheme value', () {
|
test('Testing getUriScheme for empty scheme value', () {
|
||||||
Uri uriScheme3 = Uri(
|
Uri uri3 = Uri(
|
||||||
scheme: '');
|
scheme: '');
|
||||||
expect(getUriScheme(uriScheme3), (null,false));
|
expect(getUriScheme(uri3), (null,false));
|
||||||
});
|
});
|
||||||
test('Testing getUriScheme for null scheme value', () {
|
test('Testing getUriScheme for null scheme value', () {
|
||||||
Uri uriScheme4 = Uri(
|
Uri uri4 = Uri(
|
||||||
scheme: null);
|
scheme: null);
|
||||||
expect(getUriScheme(uriScheme4), (null,false));
|
expect(getUriScheme(uri4), (null,false));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -238,66 +239,136 @@ void main() {
|
|||||||
|
|
||||||
group("Testing getResponseBodyViewOptions", () {
|
group("Testing getResponseBodyViewOptions", () {
|
||||||
test('Testing getResponseBodyViewOptions for application/json', () {
|
test('Testing getResponseBodyViewOptions for application/json', () {
|
||||||
MediaType mediaType1Expected = MediaType("application", "json");
|
MediaType mediaType1 = MediaType("application", "json");
|
||||||
var result1 = getResponseBodyViewOptions(mediaType1Expected);
|
var result1 = getResponseBodyViewOptions(mediaType1);
|
||||||
expect(result1.$0,kCodeRawBodyViewOptions);
|
expect(result1.$0,kCodeRawBodyViewOptions);
|
||||||
expect(result1.$1, "json");
|
expect(result1.$1, "json");
|
||||||
});
|
});
|
||||||
test('Testing getResponseBodyViewOptions for application/xml', () {
|
test('Testing getResponseBodyViewOptions for application/xml', () {
|
||||||
MediaType mediaType2Expected = MediaType("application", "xml");
|
MediaType mediaType2 = MediaType("application", "xml");
|
||||||
var result2 = getResponseBodyViewOptions(mediaType2Expected);
|
var result2 = getResponseBodyViewOptions(mediaType2);
|
||||||
expect(result2.$0, kCodeRawBodyViewOptions);
|
expect(result2.$0, kCodeRawBodyViewOptions);
|
||||||
expect(result2.$1,"xml");
|
expect(result2.$1,"xml");
|
||||||
});
|
});
|
||||||
test('Testing getResponseBodyViewOptions for message/news a format currently not supported', () {
|
test('Testing getResponseBodyViewOptions for message/news a format currently not supported', () {
|
||||||
MediaType mediaType3Expected = MediaType("message", "news");
|
MediaType mediaType3 = MediaType("message", "news");
|
||||||
var result3 = getResponseBodyViewOptions(mediaType3Expected);
|
var result3 = getResponseBodyViewOptions(mediaType3);
|
||||||
expect(result3.$0,kNoBodyViewOptions);
|
expect(result3.$0,kNoBodyViewOptions);
|
||||||
expect(result3.$1,null);
|
expect(result3.$1,null);
|
||||||
});
|
});
|
||||||
test('Testing getResponseBodyViewOptions for application/calendar+json', () {
|
test('Testing getResponseBodyViewOptions for application/calendar+json', () {
|
||||||
MediaType mediaType4Expected = MediaType("application", "calendar+json");
|
MediaType mediaType4 = MediaType("application", "calendar+json");
|
||||||
var result4 = getResponseBodyViewOptions(mediaType4Expected);
|
var result4 = getResponseBodyViewOptions(mediaType4);
|
||||||
expect(result4.$0,kCodeRawBodyViewOptions);
|
expect(result4.$0,kCodeRawBodyViewOptions);
|
||||||
expect(result4.$1, "json");
|
expect(result4.$1, "json");
|
||||||
});
|
});
|
||||||
test('Testing getResponseBodyViewOptions for image/svg+xml', () {
|
test('Testing getResponseBodyViewOptions for image/svg+xml', () {
|
||||||
MediaType mediaType5Expected = MediaType("image", "svg+xml");
|
MediaType mediaType5 = MediaType("image", "svg+xml");
|
||||||
var result5 = getResponseBodyViewOptions(mediaType5Expected);
|
var result5 = getResponseBodyViewOptions(mediaType5);
|
||||||
expect(result5.$0,kCodeRawBodyViewOptions);
|
expect(result5.$0,kCodeRawBodyViewOptions);
|
||||||
expect(result5.$1, "xml");
|
expect(result5.$1, "xml");
|
||||||
});
|
});
|
||||||
test('Testing getResponseBodyViewOptions for application/xhtml+xml', () {
|
test('Testing getResponseBodyViewOptions for application/xhtml+xml', () {
|
||||||
MediaType mediaType6Expected = MediaType("application", "xhtml+xml");
|
MediaType mediaType6 = MediaType("application", "xhtml+xml");
|
||||||
var result6 = getResponseBodyViewOptions(mediaType6Expected);
|
var result6 = getResponseBodyViewOptions(mediaType6);
|
||||||
expect(result6.$0,kCodeRawBodyViewOptions);
|
expect(result6.$0,kCodeRawBodyViewOptions);
|
||||||
expect(result6.$1, "xml");
|
expect(result6.$1, "xml");
|
||||||
});
|
});
|
||||||
test('Testing getResponseBodyViewOptions for application/xml-external-parsed-entity', () {
|
test('Testing getResponseBodyViewOptions for application/xml-external-parsed-entity', () {
|
||||||
MediaType mediaType7Expected = MediaType("application", "xml-external-parsed-entity");
|
MediaType mediaType7 = MediaType("application", "xml-external-parsed-entity");
|
||||||
var result7 = getResponseBodyViewOptions(mediaType7Expected);
|
var result7 = getResponseBodyViewOptions(mediaType7);
|
||||||
expect(result7.$0,kCodeRawBodyViewOptions);
|
expect(result7.$0,kCodeRawBodyViewOptions);
|
||||||
expect(result7.$1, "xml");
|
expect(result7.$1, "xml");
|
||||||
});
|
});
|
||||||
test('Testing getResponseBodyViewOptions for text/html', () {
|
test('Testing getResponseBodyViewOptions for text/html', () {
|
||||||
MediaType mediaType8Expected = MediaType("text", "html");
|
MediaType mediaType8 = MediaType("text", "html");
|
||||||
var result8 = getResponseBodyViewOptions(mediaType8Expected);
|
var result8 = getResponseBodyViewOptions(mediaType8);
|
||||||
expect(result8.$0,kCodeRawBodyViewOptions);
|
expect(result8.$0,kCodeRawBodyViewOptions);
|
||||||
expect(result8.$1, "xml");
|
expect(result8.$1, "xml");
|
||||||
});
|
});
|
||||||
test('Testing getResponseBodyViewOptions for application/pdf', () {
|
test('Testing getResponseBodyViewOptions for application/pdf', () {
|
||||||
MediaType mediaType9Expected = MediaType("application", "pdf");
|
MediaType mediaType9 = MediaType("application", "pdf");
|
||||||
var result9 = getResponseBodyViewOptions(mediaType9Expected);
|
var result9 = getResponseBodyViewOptions(mediaType9);
|
||||||
expect(result9.$0,kPreviewBodyViewOptions);
|
expect(result9.$0,kPreviewBodyViewOptions);
|
||||||
expect(result9.$1, "pdf");
|
expect(result9.$1, "pdf");
|
||||||
});
|
});
|
||||||
test('Testing getResponseBodyViewOptions for text/calendar', () {
|
test('Testing getResponseBodyViewOptions for text/calendar', () {
|
||||||
MediaType mediaType10Expected = MediaType("text", "calendar");
|
MediaType mediaType10 = MediaType("text", "calendar");
|
||||||
var result10 = getResponseBodyViewOptions(mediaType10Expected);
|
var result10 = getResponseBodyViewOptions(mediaType10);
|
||||||
expect(result10.$0,kRawBodyViewOptions);
|
expect(result10.$0,kRawBodyViewOptions);
|
||||||
expect(result10.$1, "calendar");
|
expect(result10.$1, "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 = '''
|
||||||
|
<breakfast_menu>
|
||||||
|
<food>
|
||||||
|
<name>Belgian Waffles</name>
|
||||||
|
<price>5.95 USD</price>
|
||||||
|
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
|
||||||
|
<calories>650</calories>
|
||||||
|
</food>
|
||||||
|
</breakfast_menu>
|
||||||
|
''';
|
||||||
|
MediaType mediaType3 = MediaType("application", "xml");
|
||||||
|
String result3Expected = '''<breakfast_menu>
|
||||||
|
<food>
|
||||||
|
<name>Belgian Waffles</name>
|
||||||
|
<price>5.95 USD</price>
|
||||||
|
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
|
||||||
|
<calories>650</calories>
|
||||||
|
</food>
|
||||||
|
</breakfast_menu>''';
|
||||||
|
expect(formatBody(body3, mediaType3),result3Expected);
|
||||||
|
});
|
||||||
|
group("Testing formatBody for html", () {
|
||||||
|
MediaType mediaTypeHtml = MediaType("text", "html");
|
||||||
|
test('Testing formatBody for html subtype values', () {
|
||||||
|
String body4 = '''<html>
|
||||||
|
<body>
|
||||||
|
<h1>My First Heading</h1>
|
||||||
|
<p>My first paragraph.</p>
|
||||||
|
</body>
|
||||||
|
</html>''';
|
||||||
|
expect(formatBody(body4, mediaTypeHtml),body4);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Testing formatBody for html subtype values with random values', () {
|
||||||
|
String body5 = '''<html>${getRandomStringLines(100, 10000)}</html>''';
|
||||||
|
expect(formatBody(body5, mediaTypeHtml),null);
|
||||||
|
});
|
||||||
|
test('Testing formatBody for html subtype values with random values within limit', () {
|
||||||
|
String body6 = '''<html>${getRandomStringLines(100, 190)}</html>''';
|
||||||
|
expect(formatBody(body6, mediaTypeHtml),body6);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user