| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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:io'; | 8 import 'dart:io'; |
| 8 import 'dart:utf'; | 9 import 'dart:utf'; |
| 9 | 10 |
| 10 // TODO(nweiz): get rid of this import before packaging this | 11 // TODO(nweiz): get rid of this import before packaging this |
| 11 import '../../../tests/utils/test_utils.dart'; | 12 import '../../../tests/utils/test_utils.dart'; |
| 12 import 'package:unittest/unittest.dart'; | 13 import 'package:unittest/unittest.dart'; |
| 13 import 'package:http/http.dart' as http; | 14 import 'package:http/http.dart' as http; |
| 14 import 'package:http/src/utils.dart'; | 15 import 'package:http/src/utils.dart'; |
| 15 | 16 |
| 16 import 'utils.dart'; | 17 import 'utils.dart'; |
| 17 | 18 |
| 18 /// A matcher that validates the body of a multipart request after finalization. | 19 /// 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 /// 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 /// string for the request, and LF newlines will be replaced with CRLF. |
| 21 /// Indentation will be normalized. | 22 /// Indentation will be normalized. |
| 22 Matcher bodyMatches(String pattern) => new _BodyMatches(pattern); | 23 Matcher bodyMatches(String pattern) => new _BodyMatches(pattern); |
| 23 | 24 |
| 24 class _BodyMatches extends BaseMatcher { | 25 class _BodyMatches extends BaseMatcher { |
| 25 final String _pattern; | 26 final String _pattern; |
| 26 | 27 |
| 27 _BodyMatches(this._pattern); | 28 _BodyMatches(this._pattern); |
| 28 | 29 |
| 29 bool matches(item, MatchState matchState) { | 30 bool matches(item, MatchState matchState) { |
| 30 if (item is! http.MultipartRequest) return false; | 31 if (item is! http.MultipartRequest) return false; |
| 31 | 32 |
| 32 var future = consumeInputStream(item.finalize()).then((bodyBytes) { | 33 var future = item.finalize().toBytes().then((bodyBytes) { |
| 33 var body = decodeUtf8(bodyBytes); | 34 var body = decodeUtf8(bodyBytes); |
| 34 var contentType = new ContentType.fromString( | 35 var contentType = new ContentType.fromString( |
| 35 item.headers['content-type']); | 36 item.headers['content-type']); |
| 36 var boundary = contentType.parameters['boundary']; | 37 var boundary = contentType.parameters['boundary']; |
| 37 var expected = cleanUpLiteral(_pattern) | 38 var expected = cleanUpLiteral(_pattern) |
| 38 .replaceAll("\n", "\r\n") | 39 .replaceAll("\n", "\r\n") |
| 39 .replaceAll("{{boundary}}", boundary); | 40 .replaceAll("{{boundary}}", boundary); |
| 40 | 41 |
| 41 expect(body, equals(expected)); | 42 expect(body, equals(expected)); |
| 42 expect(item.contentLength, equals(bodyBytes.length)); | 43 expect(item.contentLength, equals(bodyBytes.length)); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 }); | 149 }); |
| 149 | 150 |
| 150 // TODO(nweiz): test creating a multipart file with a charset other than UTF-8 | 151 // TODO(nweiz): test creating a multipart file with a charset other than UTF-8 |
| 151 // once issue 6284 is fixed. | 152 // once issue 6284 is fixed. |
| 152 | 153 |
| 153 // TODO(nweiz): test creating a string with a unicode body once issue 6284 is | 154 // TODO(nweiz): test creating a string with a unicode body once issue 6284 is |
| 154 // fixed. | 155 // fixed. |
| 155 | 156 |
| 156 test('with a stream file', () { | 157 test('with a stream file', () { |
| 157 var request = new http.MultipartRequest('POST', dummyUrl); | 158 var request = new http.MultipartRequest('POST', dummyUrl); |
| 158 var stream = new ListInputStream(); | 159 var stream = new StreamController.singleSubscription(); |
| 159 request.files.add(new http.MultipartFile('file', stream, 5)); | 160 request.files.add(new http.MultipartFile('file', stream, 5)); |
| 160 | 161 |
| 161 expect(request, bodyMatches(''' | 162 expect(request, bodyMatches(''' |
| 162 --{{boundary}} | 163 --{{boundary}} |
| 163 content-type: application/octet-stream | 164 content-type: application/octet-stream |
| 164 content-disposition: form-data; name="file" | 165 content-disposition: form-data; name="file" |
| 165 | 166 |
| 166 hello | 167 hello |
| 167 --{{boundary}}-- | 168 --{{boundary}}-- |
| 168 ''')); | 169 ''')); |
| 169 | 170 |
| 170 stream.write([104, 101, 108, 108, 111]); | 171 stream.add([104, 101, 108, 108, 111]); |
| 171 stream.markEndOfStream(); | 172 stream.close(); |
| 172 }); | 173 }); |
| 173 | 174 |
| 174 test('with an empty stream file', () { | 175 test('with an empty stream file', () { |
| 175 var request = new http.MultipartRequest('POST', dummyUrl); | 176 var request = new http.MultipartRequest('POST', dummyUrl); |
| 176 var stream = new ListInputStream(); | 177 var stream = new StreamController.singleSubscription(); |
| 177 stream.markEndOfStream(); | |
| 178 request.files.add(new http.MultipartFile('file', stream, 0)); | 178 request.files.add(new http.MultipartFile('file', stream, 0)); |
| 179 | 179 |
| 180 expect(request, bodyMatches(''' | 180 expect(request, bodyMatches(''' |
| 181 --{{boundary}} | 181 --{{boundary}} |
| 182 content-type: application/octet-stream | 182 content-type: application/octet-stream |
| 183 content-disposition: form-data; name="file" | 183 content-disposition: form-data; name="file" |
| 184 | 184 |
| 185 | 185 |
| 186 --{{boundary}}-- | 186 --{{boundary}}-- |
| 187 ''')); | 187 ''')); |
| 188 |
| 189 stream.close(); |
| 188 }); | 190 }); |
| 189 | 191 |
| 190 test('with a byte file', () { | 192 test('with a byte file', () { |
| 191 var request = new http.MultipartRequest('POST', dummyUrl); | 193 var request = new http.MultipartRequest('POST', dummyUrl); |
| 192 var file = new http.MultipartFile.fromBytes( | 194 var file = new http.MultipartFile.fromBytes( |
| 193 'file', [104, 101, 108, 108, 111]); | 195 'file', [104, 101, 108, 108, 111]); |
| 194 request.files.add(file); | 196 request.files.add(file); |
| 195 | 197 |
| 196 expect(request, bodyMatches(''' | 198 expect(request, bodyMatches(''' |
| 197 --{{boundary}} | 199 --{{boundary}} |
| 198 content-type: application/octet-stream | 200 content-type: application/octet-stream |
| 199 content-disposition: form-data; name="file" | 201 content-disposition: form-data; name="file" |
| 200 | 202 |
| 201 hello | 203 hello |
| 202 --{{boundary}}-- | 204 --{{boundary}}-- |
| 203 ''')); | 205 ''')); |
| 204 }); | 206 }); |
| 205 } | 207 } |
| OLD | NEW |