Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1070)

Side by Side Diff: tests/standalone/io/http_multipart_test.dart

Issue 14796015: Add new HttpMultipartFormData, used for parsing a MimeMultipart and extracting either text or binar… (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Update doc comments. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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
120 // Similar test using Chrome posting.
121 message = [
122 45, 45, 45, 45, 45, 45, 87, 101, 98, 75, 105, 116, 70, 111, 114, 109, 66,
123 111, 117, 110, 100, 97, 114, 121, 81, 83, 113, 108, 56, 107, 68, 65, 76,
124 77, 55, 116, 65, 107, 67, 49, 13, 10, 67, 111, 110, 116, 101, 110, 116,
125 45, 68, 105, 115, 112, 111, 115, 105, 116, 105, 111, 110, 58, 32, 102,
126 111, 114, 109, 45, 100, 97, 116, 97, 59, 32, 110, 97, 109, 101, 61, 34,
127 115, 117, 98, 109, 105, 116, 45, 110, 97, 109, 101, 34, 13, 10, 13, 10,
128 84, 101, 115, 116, 13, 10, 45, 45, 45, 45, 45, 45, 87, 101, 98, 75, 105,
129 116, 70, 111, 114, 109, 66, 111, 117, 110, 100, 97, 114, 121, 81, 83, 113,
130 108, 56, 107, 68, 65, 76, 77, 55, 116, 65, 107, 67, 49, 13, 10, 67, 111,
131 110, 116, 101, 110, 116, 45, 68, 105, 115, 112, 111, 115, 105, 116, 105,
132 111, 110, 58, 32, 102, 111, 114, 109, 45, 100, 97, 116, 97, 59, 32, 110,
133 97, 109, 101, 61, 34, 102, 105, 108, 101, 115, 34, 59, 32, 102, 105, 108,
134 101, 110, 97, 109, 101, 61, 34, 86, 69, 82, 83, 73, 79, 78, 34, 13, 10,
135 67, 111, 110, 116, 101, 110, 116, 45, 84, 121, 112, 101, 58, 32, 97, 112,
136 112, 108, 105, 99, 97, 116, 105, 111, 110, 47, 111, 99, 116, 101, 116, 45,
137 115, 116, 114, 101, 97, 109, 13, 10, 13, 10, 123, 32, 10, 32, 32, 34, 114,
138 101, 118, 105, 115, 105, 111, 110, 34, 58, 32, 34, 50, 49, 56, 54, 48, 34,
139 44, 10, 32, 32, 34, 118, 101, 114, 115, 105, 111, 110, 34, 32, 58, 32, 34,
140 48, 46, 49, 46, 50, 46, 48, 95, 114, 50, 49, 56, 54, 48, 34, 44, 10, 32,
141 32, 34, 100, 97, 116, 101, 34, 32, 32, 32, 32, 58, 32, 34, 50, 48, 49, 51,
142 48, 52, 50, 51, 48, 48, 48, 52, 34, 10, 125, 13, 10, 45, 45, 45, 45, 45,
143 45, 87, 101, 98, 75, 105, 116, 70, 111, 114, 109, 66, 111, 117, 110, 100,
144 97, 114, 121, 81, 83, 113, 108, 56, 107, 68, 65, 76, 77, 55, 116, 65, 107,
145 67, 49, 45, 45, 13, 10];
146
147 var data = [
148 123, 32, 10, 32, 32, 34, 114, 101, 118, 105, 115, 105, 111, 110, 34, 58,
149 32, 34, 50, 49, 56, 54, 48, 34, 44, 10, 32, 32, 34, 118, 101, 114, 115,
150 105, 111, 110, 34, 32, 58, 32, 34, 48, 46, 49, 46, 50, 46, 48, 95, 114,
151 50, 49, 56, 54, 48, 34, 44, 10, 32, 32, 34, 100, 97, 116, 101, 34, 32, 32,
152 32, 32, 58, 32, 34, 50, 48, 49, 51, 48, 52, 50, 51, 48, 48, 48, 52, 34,
153 10, 125];
154
155 postDataTest(message,
156 'multipart/form-data',
157 '----WebKitFormBoundaryQSql8kDALM7tAkC1',
158 [new FormField('submit-name', 'Test'),
159 new FormField('files',
160 data,
161 contentType: 'application/octet-stream',
162 filename: 'VERSION')]);
163
164 message = [
165 45, 45, 45, 45, 45, 45, 87, 101, 98, 75, 105, 116, 70, 111, 114, 109, 66,
166 111, 117, 110, 100, 97, 114, 121, 118, 65, 86, 122, 117, 103, 75, 77, 116,
167 90, 98, 121, 87, 111, 66, 71, 13, 10, 67, 111, 110, 116, 101, 110, 116,
168 45, 68, 105, 115, 112, 111, 115, 105, 116, 105, 111, 110, 58, 32, 102,
169 111, 114, 109, 45, 100, 97, 116, 97, 59, 32, 110, 97, 109, 101, 61, 34,
170 110, 97, 109, 101, 34, 13, 10, 13, 10, 38, 35, 49, 50, 52, 48, 50, 59, 38,
171 35, 49, 50, 52, 50, 53, 59, 38, 35, 49, 50, 51, 54, 52, 59, 38, 35, 49,
172 50, 51, 57, 52, 59, 13, 10, 45, 45, 45, 45, 45, 45, 87, 101, 98, 75, 105,
173 116, 70, 111, 114, 109, 66, 111, 117, 110, 100, 97, 114, 121, 118, 65, 86,
174 122, 117, 103, 75, 77, 116, 90, 98, 121, 87, 111, 66, 71, 45, 45, 13, 10];
175
176 postDataTest(message,
177 'multipart/form-data',
178 '----WebKitFormBoundaryvAVzugKMtZbyWoBG',
179 [new FormField('name', 'ひらがな')]);
180
181 message = [
182 45, 45, 45, 45, 45, 45, 87, 101, 98, 75, 105, 116, 70, 111, 114, 109, 66,
183 111, 117, 110, 100, 97, 114, 121, 102, 101, 48, 69, 122, 86, 49, 97, 78,
184 121, 115, 68, 49, 98, 80, 104, 13, 10, 67, 111, 110, 116, 101, 110, 116,
185 45, 68, 105, 115, 112, 111, 115, 105, 116, 105, 111, 110, 58, 32, 102,
186 111, 114, 109, 45, 100, 97, 116, 97, 59, 32, 110, 97, 109, 101, 61, 34,
187 110, 97, 109, 101, 34, 13, 10, 13, 10, 248, 118, 13, 10, 45, 45, 45, 45,
188 45, 45, 87, 101, 98, 75, 105, 116, 70, 111, 114, 109, 66, 111, 117, 110,
189 100, 97, 114, 121, 102, 101, 48, 69, 122, 86, 49, 97, 78, 121, 115, 68,
190 49, 98, 80, 104, 45, 45, 13, 10];
191
192 postDataTest(message,
193 'multipart/form-data',
194 '----WebKitFormBoundaryfe0EzV1aNysD1bPh',
195 [new FormField('name', 'øv')]);
196 }
197
198
199 void main() {
200 testPostData();
201 }
OLDNEW
« no previous file with comments | « tests/standalone/io/http_auth_digest_test.dart ('k') | tests/standalone/io/mime_multipart_parser_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698