platform/packages/body_parser/test/form_data_test.dart

144 lines
4.2 KiB
Dart
Raw Permalink Normal View History

import 'dart:io';
2021-02-14 05:22:25 +00:00
import 'dart:convert';
import 'package:body_parser/body_parser.dart';
2018-08-11 02:08:44 +00:00
import 'package:dart2_constant/convert.dart';
import 'package:http/http.dart' as http;
import 'package:test/test.dart';
2018-08-11 02:08:44 +00:00
import 'server_test.dart';
2021-02-21 02:47:23 +00:00
void main() {
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);
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
request.response.write(jsonEncodeBody(await parseBody(request)));
await request.response.close();
});
url = 'http://localhost:${server.port}';
print('Test server listening on $url');
2021-02-21 02:47:23 +00:00
client = http.Client();
});
tearDown(() async {
await server.close(force: true);
client.close();
server = null;
url = null;
client = null;
});
test('No upload', () async {
String boundary = 'myBoundary';
2018-08-11 02:08:44 +00:00
Map<String, String> headers = {
'content-type': 'multipart/form-data; boundary=$boundary'
};
String postData = '''
--$boundary
Content-Disposition: form-data; name="hello"
world
--$boundary--
'''
.replaceAll("\n", "\r\n");
print(
'Form Data: \n${postData.replaceAll("\r", "\\r").replaceAll("\n", "\\n")}');
var response = await client.post(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 {
String boundary = 'myBoundary';
2018-08-11 02:08:44 +00:00
Map<String, String> headers = {
2021-02-21 02:47:23 +00:00
'content-type': ContentType("multipart", "form-data",
parameters: {"boundary": boundary}).toString()
};
String 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--
'''
.replaceAll("\n", "\r\n");
print(
'Form Data: \n${postData.replaceAll("\r", "\\r").replaceAll("\n", "\\n")}');
var response = await client.post(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 {
String boundary = 'myBoundary';
2018-08-11 02:08:44 +00:00
Map<String, String> headers = {
'content-type': 'multipart/form-data; boundary=$boundary'
};
String 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--
'''
.replaceAll("\n", "\r\n");
print(
'Form Data: \n${postData.replaceAll("\r", "\\r").replaceAll("\n", "\\n")}');
var response = await client.post(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));
});
}