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