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

Side by Side Diff: pkg/http/test/multipart_test.dart

Issue 11363094: Add a multipart HTTP request class. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add test file Created 8 years, 1 month 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
« pkg/http/lib/src/utils.dart ('K') | « pkg/http/lib/src/utils.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 library multipart_test;
6
7 import 'dart:io';
8 import 'dart:utf';
9
10 // TODO(nweiz): get rid of this import before packaging this
11 import '../../../tests/utils/test_utils.dart';
12 // TODO(nweiz): make these package: imports
13 import '../../unittest/lib/unittest.dart';
14 import '../lib/http.dart' as http;
15 import '../lib/src/utils.dart';
16
17 import 'utils.dart';
18
19 /// A matcher that validates the body of a multipart request after finalization.
20 /// The string "{{boundary}}" in [pattern] will be replaced by the boundary
21 /// string for the request, and LF newlines will be replaced with CRLF.
22 /// Indentation will be normalized.
23 Matcher bodyMatches(String pattern) => new _BodyMatches(pattern);
24
25 class _BodyMatches extends BaseMatcher {
26 final String _pattern;
27
28 _BodyMatches(this._pattern);
29
30 bool matches(item, MatchState matchState) {
31 if (item is! http.MultipartRequest) return false;
32
33 var future = consumeInputStream(item.finalize()).transform((bodyBytes) {
34 var body = decodeUtf8(bodyBytes);
35 var contentType = new ContentType.fromString(item.headers['content-type']) ;
Bob Nystrom 2012/11/06 22:00:08 Long line.
nweiz 2012/11/06 23:15:56 Done.
36 var boundary = contentType.parameters['boundary'];
37 var expected = cleanUpLiteral(_pattern)
38 .replaceAll("\n", "\r\n")
39 .replaceAll("{{boundary}}", boundary);
40
41 expect(body, equals(expected));
42 expect(item.contentLength, equals(bodyBytes.length));
43 });
44
45 return completes.matches(future, matchState);
46 }
47
48 Description describe(Description description) {
49 return description.add('has a body that matches "$_pattern"');
50 }
51 }
52
53 void main() {
54 test('empty', () {
55 var request = new http.MultipartRequest('POST', dummyUrl);
56 expect(request, bodyMatches('''
57 --{{boundary}}--
58 '''));
59 });
60
61 test('with fields and files', () {
62 var request = new http.MultipartRequest('POST', dummyUrl);
63 request.fields['field1'] = 'value1';
64 request.fields['field2'] = 'value2';
65 request.files.add(new http.MultipartFile.fromString("file1", "contents1",
66 filename: "filename1.txt"));
67 request.files.add(new http.MultipartFile.fromString("file2", "contents2"));
68
69 expect(request, bodyMatches('''
70 --{{boundary}}
71 content-disposition: form-data; name="field1"
72
73 value1
74 --{{boundary}}
75 content-disposition: form-data; name="field2"
76
77 value2
78 --{{boundary}}
79 content-type: text/plain; charset=UTF-8
80 content-disposition: form-data; name="file1"; filename="filename1.txt"
81
82 contents1
83 --{{boundary}}
84 content-type: text/plain; charset=UTF-8
85 content-disposition: form-data; name="file2"
86
87 contents2
88 --{{boundary}}--
89 '''));
90 });
91
92 test('with a unicode field name', () {
93 var request = new http.MultipartRequest('POST', dummyUrl);
94 request.fields['fïēld'] = 'value';
95
96 expect(request, bodyMatches('''
97 --{{boundary}}
98 content-disposition: form-data; name="f%C3%AF%C4%93ld"
99
100 value
101 --{{boundary}}--
102 '''));
103 });
104
105 test('with a unicode field value', () {
106 var request = new http.MultipartRequest('POST', dummyUrl);
107 request.fields['field'] = 'vⱥlūe';
108
109 expect(request, bodyMatches('''
110 --{{boundary}}
111 content-disposition: form-data; name="field"
112 content-type: text/plain; charset=UTF-8
113
114 vⱥlūe
115 --{{boundary}}--
116 '''));
117 });
118
119 test('with a unicode filename', () {
120 var request = new http.MultipartRequest('POST', dummyUrl);
121 request.files.add(new http.MultipartFile.fromString('file', 'contents',
122 filename: 'fïlēname.txt'));
123
124 expect(request, bodyMatches('''
125 --{{boundary}}
126 content-type: text/plain; charset=UTF-8
127 content-disposition: form-data; name="file"; filename="f%C3%AFl%C4%93nam e.txt"
128
129 contents
130 --{{boundary}}--
131 '''));
132 });
133
134 test('with a string file with a content-type but no charset', () {
135 var request = new http.MultipartRequest('POST', dummyUrl);
136 var file = new http.MultipartFile.fromString('file', '{"hello": "world"}',
137 contentType: new ContentType('application', 'json'));
138 request.files.add(file);
139
140 expect(request, bodyMatches('''
141 --{{boundary}}
142 content-type: application/json; charset=UTF-8
143 content-disposition: form-data; name="file"
144
145 {"hello": "world"}
146 --{{boundary}}--
147 '''));
148 });
149
150 // TODO(nweiz): test creating a multipart file with a charset other than UTF-8
151 // once issue 6284 is fixed.
152
153 // TODO(nweiz): test creating a string with a unicode body once issue 6284 is
154 // fixed.
155
156 test('with a stream file', () {
157 var request = new http.MultipartRequest('POST', dummyUrl);
158 var stream = new ListInputStream();
159 request.files.add(new http.MultipartFile('file', stream, 5));
160
161 expect(request, bodyMatches('''
162 --{{boundary}}
163 content-type: application/octet-stream
164 content-disposition: form-data; name="file"
165
166 hello
167 --{{boundary}}--
168 '''));
169
170 stream.write([104, 101, 108, 108, 111]);
171 stream.markEndOfStream();
172 });
173
174 test('with an empty stream file', () {
175 var request = new http.MultipartRequest('POST', dummyUrl);
176 var stream = new ListInputStream();
177 stream.markEndOfStream();
178 request.files.add(new http.MultipartFile('file', stream, 0));
179
180 expect(request, bodyMatches('''
181 --{{boundary}}
182 content-type: application/octet-stream
183 content-disposition: form-data; name="file"
184
185
186 --{{boundary}}--
187 '''));
188 });
189
190 test('with a byte file', () {
191 var request = new http.MultipartRequest('POST', dummyUrl);
192 var file = new http.MultipartFile.fromBytes(
193 'file', [104, 101, 108, 108, 111]);
194 request.files.add(file);
195
196 expect(request, bodyMatches('''
197 --{{boundary}}
198 content-type: application/octet-stream
199 content-disposition: form-data; name="file"
200
201 hello
202 --{{boundary}}--
203 '''));
204 });
205 }
OLDNEW
« pkg/http/lib/src/utils.dart ('K') | « pkg/http/lib/src/utils.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698