2021-06-20 12:37:20 +00:00
|
|
|
import 'dart:convert';
|
2018-08-11 02:08:44 +00:00
|
|
|
import 'dart:io' show HttpRequest, HttpServer;
|
|
|
|
|
2021-06-20 13:29:23 +00:00
|
|
|
import 'package:angel3_body_parser/angel3_body_parser.dart';
|
2016-09-24 18:30:01 +00:00
|
|
|
import 'package:http/http.dart' as http;
|
2021-07-10 02:17:05 +00:00
|
|
|
import 'package:http_parser/http_parser.dart';
|
2016-09-24 18:30:01 +00:00
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
2016-12-22 18:08:45 +00:00
|
|
|
const TOKEN =
|
|
|
|
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiIxMjcuMC4wLjEiLCJleHAiOi0xLCJpYXQiOiIyMDE2LTEyLTIyVDEyOjQ5OjUwLjM2MTQ0NiIsImlzcyI6ImFuZ2VsX2F1dGgiLCJzdWIiOiIxMDY2OTQ4Mzk2MDIwMjg5ODM2NTYifQ==.PYw7yUb-cFWD7N0sSLztP7eeRvO44nu1J2OgDNyT060=';
|
|
|
|
|
2018-08-11 02:08:44 +00:00
|
|
|
String jsonEncodeBody(BodyParseResult result) {
|
|
|
|
return json.encode({
|
|
|
|
'query': result.query,
|
|
|
|
'body': result.body,
|
|
|
|
'error': result.error?.toString(),
|
|
|
|
'files': result.files.map((f) {
|
|
|
|
return {
|
|
|
|
'name': f.name,
|
|
|
|
'mimeType': f.mimeType,
|
|
|
|
'filename': f.filename,
|
|
|
|
'data': f.data,
|
|
|
|
};
|
|
|
|
}).toList(),
|
|
|
|
'originalBuffer': result.originalBuffer,
|
|
|
|
'stack': null, //result.stack.toString(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-10 02:17:05 +00:00
|
|
|
Future<BodyParseResult> _parseBody(HttpRequest request) {
|
|
|
|
return parseBodyFromStream(
|
|
|
|
request,
|
|
|
|
request.headers.contentType != null
|
|
|
|
? MediaType.parse(request.headers.contentType.toString())
|
|
|
|
: null,
|
|
|
|
request.uri,
|
|
|
|
storeOriginalBuffer: true);
|
|
|
|
}
|
|
|
|
|
2021-02-21 02:47:23 +00:00
|
|
|
void main() {
|
2021-06-20 12:37:20 +00:00
|
|
|
HttpServer? server;
|
|
|
|
String? url;
|
|
|
|
http.Client? client;
|
2016-09-24 18:30:01 +00:00
|
|
|
|
|
|
|
setUp(() async {
|
2018-08-11 02:08:44 +00:00
|
|
|
server = await HttpServer.bind('127.0.0.1', 0);
|
2021-06-20 12:37:20 +00:00
|
|
|
server!.listen((HttpRequest request) async {
|
2016-09-24 18:30:01 +00:00
|
|
|
//Server will simply return a JSON representation of the parsed body
|
2017-01-14 13:50:02 +00:00
|
|
|
request.response.write(
|
2018-08-11 02:08:44 +00:00
|
|
|
// ignore: deprecated_member_use
|
2021-07-10 02:17:05 +00:00
|
|
|
jsonEncodeBody(await _parseBody(request)));
|
2016-09-24 18:30:01 +00:00
|
|
|
await request.response.close();
|
|
|
|
});
|
2021-06-20 12:37:20 +00:00
|
|
|
url = 'http://localhost:${server!.port}';
|
2016-09-24 18:30:01 +00:00
|
|
|
print('Test server listening on $url');
|
2021-02-21 02:47:23 +00:00
|
|
|
client = http.Client();
|
2016-09-24 18:30:01 +00:00
|
|
|
});
|
|
|
|
tearDown(() async {
|
2021-06-20 12:37:20 +00:00
|
|
|
await server!.close(force: true);
|
|
|
|
client!.close();
|
2016-09-24 18:30:01 +00:00
|
|
|
server = null;
|
|
|
|
url = null;
|
|
|
|
client = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
group('query string', () {
|
|
|
|
test('GET Simple', () async {
|
|
|
|
print('GET $url/?hello=world');
|
2021-06-20 12:37:20 +00:00
|
|
|
var response = await client!.get(Uri.parse('$url/?hello=world'));
|
2016-09-24 18:30:01 +00:00
|
|
|
print('Response: ${response.body}');
|
2018-08-11 02:08:44 +00:00
|
|
|
var result = json.decode(response.body);
|
2017-01-14 13:50:02 +00:00
|
|
|
expect(result['body'], equals({}));
|
|
|
|
expect(result['query'], equals({'hello': 'world'}));
|
|
|
|
expect(result['files'], equals([]));
|
2017-07-19 21:20:57 +00:00
|
|
|
//expect(result['originalBuffer'], isNull);
|
2016-09-24 18:30:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('GET Complex', () async {
|
2018-08-11 02:08:44 +00:00
|
|
|
var postData =
|
|
|
|
'hello=world&nums%5B%5D=1&nums%5B%5D=2.0&nums%5B%5D=${3 - 1}&map.foo.bar=baz';
|
2016-09-24 18:30:01 +00:00
|
|
|
print('Body: $postData');
|
2021-06-20 12:37:20 +00:00
|
|
|
var response = await client!.get(Uri.parse('$url/?$postData'));
|
2016-09-24 18:30:01 +00:00
|
|
|
print('Response: ${response.body}');
|
2018-08-11 02:08:44 +00:00
|
|
|
var query = json.decode(response.body)['query'];
|
2016-09-24 18:30:01 +00:00
|
|
|
expect(query['hello'], equals('world'));
|
|
|
|
expect(query['nums'][2], equals(2));
|
|
|
|
expect(query['map'] is Map, equals(true));
|
|
|
|
expect(query['map']['foo'], equals({'bar': 'baz'}));
|
|
|
|
});
|
2016-12-22 18:08:45 +00:00
|
|
|
|
|
|
|
test('JWT', () async {
|
|
|
|
var postData = 'token=$TOKEN';
|
|
|
|
print('Body: $postData');
|
2021-06-20 12:37:20 +00:00
|
|
|
var response = await client!.get(Uri.parse('$url/?$postData'));
|
2016-12-22 18:08:45 +00:00
|
|
|
print('Response: ${response.body}');
|
2018-08-11 02:08:44 +00:00
|
|
|
var query = json.decode(response.body)['query'];
|
2016-12-22 18:08:45 +00:00
|
|
|
expect(query['token'], equals(TOKEN));
|
|
|
|
});
|
2016-09-24 18:30:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
group('urlencoded', () {
|
2021-06-20 12:37:20 +00:00
|
|
|
var headers = <String, String>{
|
2018-08-11 02:08:44 +00:00
|
|
|
'content-type': 'application/x-www-form-urlencoded'
|
2016-09-24 18:30:01 +00:00
|
|
|
};
|
|
|
|
test('POST Simple', () async {
|
|
|
|
print('Body: hello=world');
|
2021-06-20 13:29:23 +00:00
|
|
|
var response = await client!
|
|
|
|
.post(Uri.parse(url!), headers: headers, body: 'hello=world');
|
2016-09-24 18:30:01 +00:00
|
|
|
print('Response: ${response.body}');
|
2018-08-11 02:08:44 +00:00
|
|
|
var result = json.decode(response.body);
|
2017-01-14 13:50:02 +00:00
|
|
|
expect(result['query'], equals({}));
|
|
|
|
expect(result['body'], equals({'hello': 'world'}));
|
|
|
|
expect(result['files'], equals([]));
|
|
|
|
expect(result['originalBuffer'], isList);
|
|
|
|
expect(result['originalBuffer'], isNotEmpty);
|
2016-09-24 18:30:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Post Complex', () async {
|
2018-08-11 02:08:44 +00:00
|
|
|
var postData =
|
|
|
|
'hello=world&nums%5B%5D=1&nums%5B%5D=2.0&nums%5B%5D=${3 - 1}&map.foo.bar=baz';
|
2021-06-20 12:37:20 +00:00
|
|
|
var response =
|
|
|
|
await client!.post(Uri.parse(url!), headers: headers, body: postData);
|
2018-08-11 02:08:44 +00:00
|
|
|
print('Response: ${response.body}');
|
|
|
|
var body = json.decode(response.body)['body'];
|
2016-09-24 18:30:01 +00:00
|
|
|
expect(body['hello'], equals('world'));
|
|
|
|
expect(body['nums'][2], equals(2));
|
|
|
|
expect(body['map'] is Map, equals(true));
|
|
|
|
expect(body['map']['foo'], equals({'bar': 'baz'}));
|
|
|
|
});
|
2016-12-22 18:08:45 +00:00
|
|
|
|
|
|
|
test('JWT', () async {
|
|
|
|
var postData = 'token=$TOKEN';
|
2021-06-20 12:37:20 +00:00
|
|
|
var response =
|
|
|
|
await client!.post(Uri.parse(url!), headers: headers, body: postData);
|
2018-08-11 02:08:44 +00:00
|
|
|
var body = json.decode(response.body)['body'];
|
2016-12-22 18:08:45 +00:00
|
|
|
expect(body['token'], equals(TOKEN));
|
|
|
|
});
|
2016-09-24 18:30:01 +00:00
|
|
|
});
|
|
|
|
|
2018-08-11 02:08:44 +00:00
|
|
|
group('json', () {
|
2021-06-20 12:37:20 +00:00
|
|
|
var headers = <String, String>{'content-type': 'application/json'};
|
2016-09-24 18:30:01 +00:00
|
|
|
test('Post Simple', () async {
|
2018-08-11 02:08:44 +00:00
|
|
|
var postData = json.encode({'hello': 'world'});
|
2016-09-24 18:30:01 +00:00
|
|
|
print('Body: $postData');
|
2021-06-20 12:37:20 +00:00
|
|
|
var response =
|
|
|
|
await client!.post(Uri.parse(url!), headers: headers, body: postData);
|
2016-09-24 18:30:01 +00:00
|
|
|
print('Response: ${response.body}');
|
2018-08-11 02:08:44 +00:00
|
|
|
var result = json.decode(response.body);
|
2017-01-14 13:50:02 +00:00
|
|
|
expect(result['body'], equals({'hello': 'world'}));
|
|
|
|
expect(result['query'], equals({}));
|
|
|
|
expect(result['files'], equals([]));
|
|
|
|
expect(result['originalBuffer'], allOf(isList, isNotEmpty));
|
2016-09-24 18:30:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Post Complex', () async {
|
2018-08-11 02:08:44 +00:00
|
|
|
var postData = json.encode({
|
2016-09-24 18:30:01 +00:00
|
|
|
'hello': 'world',
|
|
|
|
'nums': [1, 2.0, 3 - 1],
|
|
|
|
'map': {
|
|
|
|
'foo': {'bar': 'baz'}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
print('Body: $postData');
|
2021-06-20 12:37:20 +00:00
|
|
|
var response =
|
|
|
|
await client!.post(Uri.parse(url!), headers: headers, body: postData);
|
2016-09-24 18:30:01 +00:00
|
|
|
print('Response: ${response.body}');
|
2018-08-11 02:08:44 +00:00
|
|
|
var body = json.decode(response.body)['body'];
|
2016-09-24 18:30:01 +00:00
|
|
|
expect(body['hello'], equals('world'));
|
|
|
|
expect(body['nums'][2], equals(2));
|
|
|
|
expect(body['map'] is Map, equals(true));
|
|
|
|
expect(body['map']['foo'], equals({'bar': 'baz'}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|