platform/packages/body_parser/test/form_data_test.dart

157 lines
4.5 KiB
Dart
Raw Normal View History

import 'dart:io';
2021-02-14 05:22:25 +00:00
import 'dart:convert';
2021-06-20 13:29:23 +00:00
import 'package:angel3_body_parser/angel3_body_parser.dart';
import 'package:http/http.dart' as http;
2021-07-10 02:17:05 +00:00
import 'package:http_parser/http_parser.dart';
import 'package:test/test.dart';
2018-08-11 02:08:44 +00:00
import 'server_test.dart';
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: false);
}
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;
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 {
//Server will simply return a JSON representation of the parsed body
2018-08-11 02:08:44 +00:00
// ignore: deprecated_member_use
2021-07-10 02:17:05 +00:00
request.response.write(jsonEncodeBody(await _parseBody(request)));
await request.response.close();
});
2021-06-20 12:37:20 +00:00
url = 'http://localhost:${server!.port}';
print('Test server listening on $url');
2021-02-21 02:47:23 +00:00
client = http.Client();
});
tearDown(() async {
2021-06-20 12:37:20 +00:00
await server!.close(force: true);
client!.close();
server = null;
url = null;
client = null;
});
test('No upload', () async {
2021-06-20 12:37:20 +00:00
var boundary = 'myBoundary';
var headers = <String, String>{
2018-08-11 02:08:44 +00:00
'content-type': 'multipart/form-data; boundary=$boundary'
};
2021-06-20 12:37:20 +00:00
var postData = '''
--$boundary
Content-Disposition: form-data; name="hello"
world
--$boundary--
'''
2021-06-20 12:37:20 +00:00
.replaceAll('\n', '\r\n');
print(
'Form Data: \n${postData.replaceAll("\r", "\\r").replaceAll("\n", "\\n")}');
2021-06-20 12:37:20 +00:00
var response =
await client!.post(Uri.parse(url!), headers: headers, body: postData);
print('Response: ${response.body}');
2021-02-14 05:22:25 +00:00
var jsons = json.decode(response.body);
2018-08-11 02:08:44 +00:00
var files = jsons['files'].map((map) {
return map == null
? null
: map.keys.fold<Map<String, dynamic>>(
<String, dynamic>{}, (out, k) => out..[k.toString()] = map[k]);
});
expect(files.length, equals(0));
2018-08-11 02:08:44 +00:00
expect(jsons['body']['hello'], equals('world'));
});
test('Single upload', () async {
2021-06-20 12:37:20 +00:00
var boundary = 'myBoundary';
var headers = <String, String>{
'content-type': ContentType('multipart', 'form-data',
parameters: {'boundary': boundary}).toString()
};
2021-06-20 12:37:20 +00:00
var postData = '''
--$boundary
Content-Disposition: form-data; name="hello"
world
--$boundary
Content-Disposition: form-data; name="file"; filename="app.dart"
Content-Type: application/dart
Hello world
--$boundary--
'''
2021-06-20 12:37:20 +00:00
.replaceAll('\n', '\r\n');
print(
'Form Data: \n${postData.replaceAll("\r", "\\r").replaceAll("\n", "\\n")}');
2021-06-20 12:37:20 +00:00
var response =
await client!.post(Uri.parse(url!), headers: headers, body: postData);
print('Response: ${response.body}');
2021-02-14 05:22:25 +00:00
var jsons = json.decode(response.body);
2018-08-11 02:08:44 +00:00
var files = jsons['files'];
expect(files.length, equals(1));
expect(files[0]['name'], equals('file'));
expect(files[0]['mimeType'], equals('application/dart'));
expect(files[0]['data'].length, equals(11));
expect(files[0]['filename'], equals('app.dart'));
2018-08-11 02:08:44 +00:00
expect(jsons['body']['hello'], equals('world'));
});
test('Multiple upload', () async {
2021-06-20 12:37:20 +00:00
var boundary = 'myBoundary';
var headers = <String, String>{
2018-08-11 02:08:44 +00:00
'content-type': 'multipart/form-data; boundary=$boundary'
};
2021-06-20 12:37:20 +00:00
var postData = '''
--$boundary
Content-Disposition: form-data; name="json"
god
--$boundary
Content-Disposition: form-data; name="num"
14.50000
--$boundary
Content-Disposition: form-data; name="file"; filename="app.dart"
Content-Type: text/plain
Hello world
--$boundary
Content-Disposition: form-data; name="entry-point"; filename="main.js"
Content-Type: text/javascript
function main() {
console.log("Hello, world!");
}
--$boundary--
'''
2021-06-20 12:37:20 +00:00
.replaceAll('\n', '\r\n');
print(
'Form Data: \n${postData.replaceAll("\r", "\\r").replaceAll("\n", "\\n")}');
2021-06-20 12:37:20 +00:00
var response =
await client!.post(Uri.parse(url!), headers: headers, body: postData);
print('Response: ${response.body}');
2021-02-14 05:22:25 +00:00
var jsons = json.decode(response.body);
2018-08-11 02:08:44 +00:00
var files = jsons['files'];
expect(files.length, equals(2));
expect(files[0]['name'], equals('file'));
expect(files[0]['mimeType'], equals('text/plain'));
expect(files[0]['data'].length, equals(11));
expect(files[1]['name'], equals('entry-point'));
expect(files[1]['mimeType'], equals('text/javascript'));
2018-08-11 02:08:44 +00:00
expect(jsons['body']['json'], equals('god'));
expect(jsons['body']['num'], equals(14.5));
});
}