Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
|
Søren Gjesse
2013/05/06 12:28:10
Add testing with short read/write.
// VMOptions=
Anders Johnsen
2013/05/06 13:38:16
Done.
| |
| 4 | |
| 5 import "package:expect/expect.dart"; | |
| 6 import 'dart:async'; | |
| 7 import 'dart:io'; | |
| 8 | |
| 9 class FormField { | |
| 10 final String name; | |
| 11 final value; | |
| 12 final String contentType; | |
| 13 final String filename; | |
| 14 | |
| 15 FormField(String this.name, | |
| 16 this.value, | |
| 17 {String this.contentType, | |
| 18 String this.filename}); | |
| 19 | |
| 20 bool operator==(other) { | |
| 21 if (value.length != other.value.length) return false; | |
| 22 for (int i = 0; i < value.length; i++) { | |
| 23 if (value[i] != other.value[i]) { | |
| 24 return false; | |
| 25 } | |
| 26 } | |
| 27 return name == other.name && | |
| 28 contentType == other.contentType && | |
| 29 filename == other.filename; | |
| 30 } | |
| 31 | |
| 32 String toString() { | |
| 33 return "FormField('$name', '$value', '$contentType', '$filename')"; | |
| 34 } | |
| 35 } | |
| 36 | |
| 37 void postDataTest(List<int> message, | |
| 38 String contentType, | |
| 39 String boundary, | |
| 40 List<FormField> expectedFields) { | |
| 41 HttpServer.bind("127.0.0.1", 0).then((server) { | |
| 42 server.listen((request) { | |
| 43 String boundary = request.headers.contentType.parameters['boundary']; | |
| 44 request | |
| 45 .transform(new MimeMultipartTransformer(boundary)) | |
| 46 .map(HttpMultipartFormData.parse) | |
| 47 .map((multipart) { | |
| 48 var future; | |
| 49 if (multipart.isText) { | |
| 50 future = multipart | |
| 51 .fold(new StringBuffer(), (b, s) => b..write(s)) | |
| 52 .then((b) => b.toString()); | |
| 53 } else { | |
| 54 future = multipart | |
| 55 .fold([], (b, s) => b..addAll(s)); | |
| 56 } | |
| 57 return future | |
| 58 .then((data) { | |
| 59 String contentType; | |
| 60 if (multipart.contentType != null) { | |
| 61 contentType = multipart.contentType.mimeType; | |
| 62 } | |
| 63 return new FormField( | |
| 64 multipart.contentDisposition.parameters['name'], | |
| 65 data, | |
| 66 contentType: contentType, | |
| 67 filename: | |
| 68 multipart.contentDisposition.parameters['filename']); | |
| 69 }); | |
| 70 }) | |
| 71 .fold([], (l, f) => l..add(f)) | |
| 72 .then(Future.wait) | |
| 73 .then((fields) { | |
| 74 Expect.listEquals(expectedFields, fields); | |
| 75 request.response.close(); | |
| 76 server.close(); | |
| 77 }); | |
| 78 }); | |
| 79 var client = new HttpClient(); | |
| 80 client.post('127.0.0.1', server.port, '/') | |
| 81 .then((request) { | |
| 82 request.headers.set('content-type', | |
| 83 'multipart/form-data; boundary=$boundary'); | |
| 84 request.add(message); | |
| 85 return request.close(); | |
| 86 }) | |
| 87 .then((response) { | |
| 88 }); | |
| 89 }); | |
| 90 } | |
| 91 | |
| 92 void testPostData() { | |
| 93 var message = ''' | |
| 94 \r\n--AaB03x\r | |
| 95 Content-Disposition: form-data; name="submit-name"\r | |
| 96 \r | |
| 97 Larry\r | |
| 98 --AaB03x\r | |
| 99 Content-Disposition: form-data; name="files"; filename="file1.txt"\r | |
| 100 Content-Type: text/plain\r | |
| 101 \r | |
| 102 Content of file\r | |
| 103 --AaB03x--\r\n'''; | |
| 104 | |
| 105 | |
| 106 postDataTest(message.codeUnits, | |
| 107 'multipart/form-data', | |
| 108 'AaB03x', | |
| 109 [new FormField('submit-name', 'Larry'), | |
| 110 new FormField('files', | |
| 111 'Content of file', | |
| 112 contentType: 'text/plain', | |
| 113 filename: 'file1.txt')]); | |
| 114 | |
| 115 // Similar test using Chrome posting. | |
| 116 message = [ | |
| 117 45, 45, 45, 45, 45, 45, 87, 101, 98, 75, 105, 116, 70, 111, 114, 109, 66, | |
| 118 111, 117, 110, 100, 97, 114, 121, 81, 83, 113, 108, 56, 107, 68, 65, 76, | |
| 119 77, 55, 116, 65, 107, 67, 49, 13, 10, 67, 111, 110, 116, 101, 110, 116, | |
| 120 45, 68, 105, 115, 112, 111, 115, 105, 116, 105, 111, 110, 58, 32, 102, | |
| 121 111, 114, 109, 45, 100, 97, 116, 97, 59, 32, 110, 97, 109, 101, 61, 34, | |
| 122 115, 117, 98, 109, 105, 116, 45, 110, 97, 109, 101, 34, 13, 10, 13, 10, | |
| 123 84, 101, 115, 116, 13, 10, 45, 45, 45, 45, 45, 45, 87, 101, 98, 75, 105, | |
| 124 116, 70, 111, 114, 109, 66, 111, 117, 110, 100, 97, 114, 121, 81, 83, 113, | |
| 125 108, 56, 107, 68, 65, 76, 77, 55, 116, 65, 107, 67, 49, 13, 10, 67, 111, | |
| 126 110, 116, 101, 110, 116, 45, 68, 105, 115, 112, 111, 115, 105, 116, 105, | |
| 127 111, 110, 58, 32, 102, 111, 114, 109, 45, 100, 97, 116, 97, 59, 32, 110, | |
| 128 97, 109, 101, 61, 34, 102, 105, 108, 101, 115, 34, 59, 32, 102, 105, 108, | |
| 129 101, 110, 97, 109, 101, 61, 34, 86, 69, 82, 83, 73, 79, 78, 34, 13, 10, | |
| 130 67, 111, 110, 116, 101, 110, 116, 45, 84, 121, 112, 101, 58, 32, 97, 112, | |
| 131 112, 108, 105, 99, 97, 116, 105, 111, 110, 47, 111, 99, 116, 101, 116, 45, | |
| 132 115, 116, 114, 101, 97, 109, 13, 10, 13, 10, 123, 32, 10, 32, 32, 34, 114, | |
| 133 101, 118, 105, 115, 105, 111, 110, 34, 58, 32, 34, 50, 49, 56, 54, 48, 34, | |
| 134 44, 10, 32, 32, 34, 118, 101, 114, 115, 105, 111, 110, 34, 32, 58, 32, 34, | |
| 135 48, 46, 49, 46, 50, 46, 48, 95, 114, 50, 49, 56, 54, 48, 34, 44, 10, 32, | |
| 136 32, 34, 100, 97, 116, 101, 34, 32, 32, 32, 32, 58, 32, 34, 50, 48, 49, 51, | |
| 137 48, 52, 50, 51, 48, 48, 48, 52, 34, 10, 125, 13, 10, 45, 45, 45, 45, 45, | |
| 138 45, 87, 101, 98, 75, 105, 116, 70, 111, 114, 109, 66, 111, 117, 110, 100, | |
| 139 97, 114, 121, 81, 83, 113, 108, 56, 107, 68, 65, 76, 77, 55, 116, 65, 107, | |
| 140 67, 49, 45, 45, 13, 10]; | |
| 141 | |
| 142 var data = [ | |
| 143 123, 32, 10, 32, 32, 34, 114, 101, 118, 105, 115, 105, 111, 110, 34, 58, | |
| 144 32, 34, 50, 49, 56, 54, 48, 34, 44, 10, 32, 32, 34, 118, 101, 114, 115, | |
| 145 105, 111, 110, 34, 32, 58, 32, 34, 48, 46, 49, 46, 50, 46, 48, 95, 114, | |
| 146 50, 49, 56, 54, 48, 34, 44, 10, 32, 32, 34, 100, 97, 116, 101, 34, 32, 32, | |
| 147 32, 32, 58, 32, 34, 50, 48, 49, 51, 48, 52, 50, 51, 48, 48, 48, 52, 34, | |
| 148 10, 125]; | |
| 149 | |
| 150 postDataTest(message, | |
| 151 'multipart/form-data', | |
| 152 '----WebKitFormBoundaryQSql8kDALM7tAkC1', | |
| 153 [new FormField('submit-name', 'Test'), | |
| 154 new FormField('files', | |
| 155 data, | |
| 156 contentType: 'application/octet-stream', | |
| 157 filename: 'VERSION')]); | |
| 158 | |
| 159 message = [ | |
| 160 45, 45, 45, 45, 45, 45, 87, 101, 98, 75, 105, 116, 70, 111, 114, 109, 66, | |
| 161 111, 117, 110, 100, 97, 114, 121, 118, 65, 86, 122, 117, 103, 75, 77, 116, | |
| 162 90, 98, 121, 87, 111, 66, 71, 13, 10, 67, 111, 110, 116, 101, 110, 116, | |
| 163 45, 68, 105, 115, 112, 111, 115, 105, 116, 105, 111, 110, 58, 32, 102, | |
| 164 111, 114, 109, 45, 100, 97, 116, 97, 59, 32, 110, 97, 109, 101, 61, 34, | |
| 165 110, 97, 109, 101, 34, 13, 10, 13, 10, 38, 35, 49, 50, 52, 48, 50, 59, 38, | |
| 166 35, 49, 50, 52, 50, 53, 59, 38, 35, 49, 50, 51, 54, 52, 59, 38, 35, 49, | |
| 167 50, 51, 57, 52, 59, 13, 10, 45, 45, 45, 45, 45, 45, 87, 101, 98, 75, 105, | |
| 168 116, 70, 111, 114, 109, 66, 111, 117, 110, 100, 97, 114, 121, 118, 65, 86, | |
| 169 122, 117, 103, 75, 77, 116, 90, 98, 121, 87, 111, 66, 71, 45, 45, 13, 10]; | |
| 170 | |
| 171 postDataTest(message, | |
| 172 'multipart/form-data', | |
| 173 '----WebKitFormBoundaryvAVzugKMtZbyWoBG', | |
| 174 [new FormField('name', 'ひらがな')]); | |
| 175 | |
| 176 message = [ | |
| 177 45, 45, 45, 45, 45, 45, 87, 101, 98, 75, 105, 116, 70, 111, 114, 109, 66, | |
| 178 111, 117, 110, 100, 97, 114, 121, 102, 101, 48, 69, 122, 86, 49, 97, 78, | |
| 179 121, 115, 68, 49, 98, 80, 104, 13, 10, 67, 111, 110, 116, 101, 110, 116, | |
| 180 45, 68, 105, 115, 112, 111, 115, 105, 116, 105, 111, 110, 58, 32, 102, | |
| 181 111, 114, 109, 45, 100, 97, 116, 97, 59, 32, 110, 97, 109, 101, 61, 34, | |
| 182 110, 97, 109, 101, 34, 13, 10, 13, 10, 248, 118, 13, 10, 45, 45, 45, 45, | |
| 183 45, 45, 87, 101, 98, 75, 105, 116, 70, 111, 114, 109, 66, 111, 117, 110, | |
| 184 100, 97, 114, 121, 102, 101, 48, 69, 122, 86, 49, 97, 78, 121, 115, 68, | |
| 185 49, 98, 80, 104, 45, 45, 13, 10]; | |
| 186 | |
| 187 postDataTest(message, | |
| 188 'multipart/form-data', | |
| 189 '----WebKitFormBoundaryfe0EzV1aNysD1bPh', | |
| 190 [new FormField('name', 'øv')]); | |
| 191 } | |
| 192 | |
| 193 | |
| 194 void main() { | |
| 195 testPostData(); | |
| 196 } | |
| OLD | NEW |