OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library multipart_test; | 5 library multipart_test; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | |
9 import 'dart:io'; | |
10 | 8 |
11 import 'package:http/http.dart' as http; | 9 import 'package:http/http.dart' as http; |
12 import 'package:http_parser/http_parser.dart'; | 10 import 'package:http_parser/http_parser.dart'; |
13 import 'package:path/path.dart' as path; | |
14 import 'package:unittest/unittest.dart'; | 11 import 'package:unittest/unittest.dart'; |
15 | 12 |
16 import 'utils.dart'; | 13 import 'utils.dart'; |
17 | 14 |
18 /// A matcher that validates the body of a multipart request after finalization. | |
19 /// The string "{{boundary}}" in [pattern] will be replaced by the boundary | |
20 /// string for the request, and LF newlines will be replaced with CRLF. | |
21 /// Indentation will be normalized. | |
22 Matcher bodyMatches(String pattern) => new _BodyMatches(pattern); | |
23 | |
24 class _BodyMatches extends Matcher { | |
25 final String _pattern; | |
26 | |
27 _BodyMatches(this._pattern); | |
28 | |
29 bool matches(item, Map matchState) { | |
30 if (item is! http.MultipartRequest) return false; | |
31 | |
32 var future = item.finalize().toBytes().then((bodyBytes) { | |
33 var body = UTF8.decode(bodyBytes); | |
34 var contentType = new MediaType.parse(item.headers['content-type']); | |
35 var boundary = contentType.parameters['boundary']; | |
36 var expected = cleanUpLiteral(_pattern) | |
37 .replaceAll("\n", "\r\n") | |
38 .replaceAll("{{boundary}}", boundary); | |
39 | |
40 expect(body, equals(expected)); | |
41 expect(item.contentLength, equals(bodyBytes.length)); | |
42 }); | |
43 | |
44 return completes.matches(future, matchState); | |
45 } | |
46 | |
47 Description describe(Description description) { | |
48 return description.add('has a body that matches "$_pattern"'); | |
49 } | |
50 } | |
51 | |
52 void main() { | 15 void main() { |
53 test('empty', () { | 16 test('empty', () { |
54 var request = new http.MultipartRequest('POST', dummyUrl); | 17 var request = new http.MultipartRequest('POST', dummyUrl); |
55 expect(request, bodyMatches(''' | 18 expect(request, bodyMatches(''' |
56 --{{boundary}}-- | 19 --{{boundary}}-- |
57 ''')); | 20 ''')); |
58 }); | 21 }); |
59 | 22 |
60 test('with fields and files', () { | 23 test('with fields and files', () { |
61 var request = new http.MultipartRequest('POST', dummyUrl); | 24 var request = new http.MultipartRequest('POST', dummyUrl); |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 | 225 |
263 expect(request, bodyMatches(''' | 226 expect(request, bodyMatches(''' |
264 --{{boundary}} | 227 --{{boundary}} |
265 content-type: application/octet-stream | 228 content-type: application/octet-stream |
266 content-disposition: form-data; name="file" | 229 content-disposition: form-data; name="file" |
267 | 230 |
268 hello | 231 hello |
269 --{{boundary}}-- | 232 --{{boundary}}-- |
270 ''')); | 233 ''')); |
271 }); | 234 }); |
272 | |
273 group('in a temp directory', () { | |
274 var tempDir; | |
275 setUp(() { | |
276 tempDir = Directory.systemTemp.createTempSync('http_test_'); | |
277 }); | |
278 | |
279 tearDown(() => tempDir.deleteSync(recursive: true)); | |
280 | |
281 test('with a file from disk', () { | |
282 expect(new Future.sync(() { | |
283 var filePath = path.join(tempDir.path, 'test-file'); | |
284 new File(filePath).writeAsStringSync('hello'); | |
285 return http.MultipartFile.fromPath('file', filePath); | |
286 }).then((file) { | |
287 var request = new http.MultipartRequest('POST', dummyUrl); | |
288 request.files.add(file); | |
289 | |
290 expect(request, bodyMatches(''' | |
291 --{{boundary}} | |
292 content-type: application/octet-stream | |
293 content-disposition: form-data; name="file"; filename="test-file" | |
294 | |
295 hello | |
296 --{{boundary}}-- | |
297 ''')); | |
298 }), completes); | |
299 }); | |
300 }); | |
301 } | 235 } |
OLD | NEW |