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

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

Issue 261763002: Rip out the last dart:io dependency from pkg/http. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 6 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
« no previous file with comments | « pkg/http/test/multipart_test.dart ('k') | pkg/http/test/streamed_request_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 request_test; 5 library request_test;
6 6
7 import 'dart:convert'; 7 import 'dart:convert';
8 import 'dart:io';
9 8
10 import 'package:http/http.dart' as http; 9 import 'package:http/http.dart' as http;
11 import 'package:unittest/unittest.dart'; 10 import 'package:unittest/unittest.dart';
12 11
13 import 'utils.dart'; 12 import 'utils.dart';
14 13
15 void main() { 14 void main() {
16 test('.send', () {
17 expect(startServer().then((_) {
18
19 var request = new http.Request('POST', serverUrl);
20 request.body = "hello";
21 request.headers['User-Agent'] = 'Dart';
22
23 expect(request.send().then((response) {
24 expect(response.statusCode, equals(200));
25 return response.stream.bytesToString();
26 }).whenComplete(stopServer), completion(parse(equals({
27 'method': 'POST',
28 'path': '/',
29 'headers': {
30 'content-type': ['text/plain; charset=utf-8'],
31 'accept-encoding': ['gzip'],
32 'user-agent': ['Dart'],
33 'content-length': ['5']
34 },
35 'body': 'hello'
36 }))));
37 }), completes);
38 });
39
40 group('#contentLength', () { 15 group('#contentLength', () {
41 test('is computed from bodyBytes', () { 16 test('is computed from bodyBytes', () {
42 var request = new http.Request('POST', dummyUrl); 17 var request = new http.Request('POST', dummyUrl);
43 request.bodyBytes = [1, 2, 3, 4, 5]; 18 request.bodyBytes = [1, 2, 3, 4, 5];
44 expect(request.contentLength, equals(5)); 19 expect(request.contentLength, equals(5));
45 request.bodyBytes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 20 request.bodyBytes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
46 expect(request.contentLength, equals(10)); 21 expect(request.contentLength, equals(10));
47 }); 22 });
48 23
49 test('is computed from body', () { 24 test('is computed from body', () {
(...skipping 17 matching lines...) Expand all
67 }); 42 });
68 43
69 test('can be set', () { 44 test('can be set', () {
70 var request = new http.Request('POST', dummyUrl); 45 var request = new http.Request('POST', dummyUrl);
71 request.encoding = LATIN1; 46 request.encoding = LATIN1;
72 expect(request.encoding.name, equals(LATIN1.name)); 47 expect(request.encoding.name, equals(LATIN1.name));
73 }); 48 });
74 49
75 test('is based on the content-type charset if it exists', () { 50 test('is based on the content-type charset if it exists', () {
76 var request = new http.Request('POST', dummyUrl); 51 var request = new http.Request('POST', dummyUrl);
77 request.headers[HttpHeaders.CONTENT_TYPE] = 52 request.headers['Content-Type'] = 'text/plain; charset=iso-8859-1';
78 'text/plain; charset=iso-8859-1';
79 expect(request.encoding.name, equals(LATIN1.name)); 53 expect(request.encoding.name, equals(LATIN1.name));
80 }); 54 });
81 55
82 test('remains the default if the content-type charset is set and unset', 56 test('remains the default if the content-type charset is set and unset',
83 () { 57 () {
84 var request = new http.Request('POST', dummyUrl); 58 var request = new http.Request('POST', dummyUrl);
85 request.encoding = LATIN1; 59 request.encoding = LATIN1;
86 request.headers[HttpHeaders.CONTENT_TYPE] = 60 request.headers['Content-Type'] = 'text/plain; charset=utf-8';
87 'text/plain; charset=utf-8';
88 expect(request.encoding.name, equals(UTF8.name)); 61 expect(request.encoding.name, equals(UTF8.name));
89 62
90 request.headers.remove(HttpHeaders.CONTENT_TYPE); 63 request.headers.remove('Content-Type');
91 expect(request.encoding.name, equals(LATIN1.name)); 64 expect(request.encoding.name, equals(LATIN1.name));
92 }); 65 });
93 66
94 test('throws an error if the content-type charset is unknown', () { 67 test('throws an error if the content-type charset is unknown', () {
95 var request = new http.Request('POST', dummyUrl); 68 var request = new http.Request('POST', dummyUrl);
96 request.headers[HttpHeaders.CONTENT_TYPE] = 69 request.headers['Content-Type'] =
97 'text/plain; charset=not-a-real-charset'; 70 'text/plain; charset=not-a-real-charset';
98 expect(() => request.encoding, throwsFormatException); 71 expect(() => request.encoding, throwsFormatException);
99 }); 72 });
100 }); 73 });
101 74
102 group('#bodyBytes', () { 75 group('#bodyBytes', () {
103 test('defaults to empty', () { 76 test('defaults to empty', () {
104 var request = new http.Request('POST', dummyUrl); 77 var request = new http.Request('POST', dummyUrl);
105 expect(request.bodyBytes, isEmpty); 78 expect(request.bodyBytes, isEmpty);
106 }); 79 });
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 }); 125 });
153 126
154 group('#bodyFields', () { 127 group('#bodyFields', () {
155 test("can't be read without setting the content-type", () { 128 test("can't be read without setting the content-type", () {
156 var request = new http.Request('POST', dummyUrl); 129 var request = new http.Request('POST', dummyUrl);
157 expect(() => request.bodyFields, throwsStateError); 130 expect(() => request.bodyFields, throwsStateError);
158 }); 131 });
159 132
160 test("can't be read with the wrong content-type", () { 133 test("can't be read with the wrong content-type", () {
161 var request = new http.Request('POST', dummyUrl); 134 var request = new http.Request('POST', dummyUrl);
162 request.headers[HttpHeaders.CONTENT_TYPE] = 'text/plain'; 135 request.headers['Content-Type'] = 'text/plain';
163 expect(() => request.bodyFields, throwsStateError); 136 expect(() => request.bodyFields, throwsStateError);
164 }); 137 });
165 138
166 test("can't be set with the wrong content-type", () { 139 test("can't be set with the wrong content-type", () {
167 var request = new http.Request('POST', dummyUrl); 140 var request = new http.Request('POST', dummyUrl);
168 request.headers[HttpHeaders.CONTENT_TYPE] = 'text/plain'; 141 request.headers['Content-Type'] = 'text/plain';
169 expect(() => request.bodyFields = {}, throwsStateError); 142 expect(() => request.bodyFields = {}, throwsStateError);
170 }); 143 });
171 144
172 test('defaults to empty', () { 145 test('defaults to empty', () {
173 var request = new http.Request('POST', dummyUrl); 146 var request = new http.Request('POST', dummyUrl);
174 request.headers[HttpHeaders.CONTENT_TYPE] = 147 request.headers['Content-Type'] =
175 'application/x-www-form-urlencoded'; 148 'application/x-www-form-urlencoded';
176 expect(request.bodyFields, isEmpty); 149 expect(request.bodyFields, isEmpty);
177 }); 150 });
178 151
179 test('can be set with no content-type', () { 152 test('can be set with no content-type', () {
180 var request = new http.Request('POST', dummyUrl); 153 var request = new http.Request('POST', dummyUrl);
181 request.bodyFields = {'hello': 'world'}; 154 request.bodyFields = {'hello': 'world'};
182 expect(request.bodyFields, equals({'hello': 'world'})); 155 expect(request.bodyFields, equals({'hello': 'world'}));
183 }); 156 });
184 157
185 test('changes when body changes', () { 158 test('changes when body changes', () {
186 var request = new http.Request('POST', dummyUrl); 159 var request = new http.Request('POST', dummyUrl);
187 request.headers[HttpHeaders.CONTENT_TYPE] = 160 request.headers['Content-Type'] =
188 'application/x-www-form-urlencoded'; 161 'application/x-www-form-urlencoded';
189 request.body = 'key%201=value&key+2=other%2bvalue'; 162 request.body = 'key%201=value&key+2=other%2bvalue';
190 expect(request.bodyFields, 163 expect(request.bodyFields,
191 equals({'key 1': 'value', 'key 2': 'other+value'})); 164 equals({'key 1': 'value', 'key 2': 'other+value'}));
192 }); 165 });
193 166
194 test('is encoded according to the given encoding', () { 167 test('is encoded according to the given encoding', () {
195 var request = new http.Request('POST', dummyUrl); 168 var request = new http.Request('POST', dummyUrl);
196 request.headers[HttpHeaders.CONTENT_TYPE] = 169 request.headers['Content-Type'] =
197 'application/x-www-form-urlencoded'; 170 'application/x-www-form-urlencoded';
198 request.encoding = LATIN1; 171 request.encoding = LATIN1;
199 request.bodyFields = {"föø": "bãr"}; 172 request.bodyFields = {"föø": "bãr"};
200 expect(request.body, equals('f%F6%F8=b%E3r')); 173 expect(request.body, equals('f%F6%F8=b%E3r'));
201 }); 174 });
202 175
203 test('is decoded according to the given encoding', () { 176 test('is decoded according to the given encoding', () {
204 var request = new http.Request('POST', dummyUrl); 177 var request = new http.Request('POST', dummyUrl);
205 request.headers[HttpHeaders.CONTENT_TYPE] = 178 request.headers['Content-Type'] =
206 'application/x-www-form-urlencoded'; 179 'application/x-www-form-urlencoded';
207 request.encoding = LATIN1; 180 request.encoding = LATIN1;
208 request.body = 'f%F6%F8=b%E3r'; 181 request.body = 'f%F6%F8=b%E3r';
209 expect(request.bodyFields, equals({"föø": "bãr"})); 182 expect(request.bodyFields, equals({"föø": "bãr"}));
210 }); 183 });
211 }); 184 });
212 185
213 test('#followRedirects', () {
214 print("This test is known to be flaky, please ignore "
215 "(debug prints below added by sgjesse@)");
216 print("#followRedirects test starting server...");
217 expect(startServer().then((_) {
218 print("#followRedirects test server running");
219
220 var request = new http.Request('POST', serverUrl.resolve('/redirect'))
221 ..followRedirects = false;
222 var future = request.send().then((response) {
223 print("#followRedirects test response received");
224 expect(response.statusCode, equals(302));
225 });
226 expect(future.catchError((_) {}).then((_) {
227 print("#followRedirects test stopping server...");
228 stopServer();
229 print("#followRedirects test server stopped");
230 }), completes);
231
232 expect(future, completes);
233 print("#followRedirects test started");
234 }), completes);
235 });
236
237 test('#maxRedirects', () {
238 print("This test is known to be flaky, please ignore "
239 "(debug prints below added by sgjesse@)");
240 print("#maxRedirects test starting server...");
241 expect(startServer().then((_) {
242 print("#maxRedirects test server running");
243
244 var request = new http.Request('POST', serverUrl.resolve('/loop?1'))
245 ..maxRedirects = 2;
246 var future = request.send().catchError((error) {
247 print("#maxRedirects test exception received");
248 expect(error, isRedirectLimitExceededException);
249 expect(error.redirects.length, equals(2));
250 });
251 expect(future.catchError((_) {}).then((_) {
252 print("#maxRedirects test stopping server...");
253 stopServer();
254 print("#maxRedirects test server stopped");
255 }), completes);
256
257 expect(future, completes);
258 print("#maxRedirects test started");
259 }), completes);
260 });
261
262 group('content-type header', () { 186 group('content-type header', () {
263 test('defaults to empty', () { 187 test('defaults to empty', () {
264 var request = new http.Request('POST', dummyUrl); 188 var request = new http.Request('POST', dummyUrl);
265 expect(request.headers[HttpHeaders.CONTENT_TYPE], isNull); 189 expect(request.headers['Content-Type'], isNull);
266 }); 190 });
267 191
268 test('defaults to empty if only encoding is set', () { 192 test('defaults to empty if only encoding is set', () {
269 var request = new http.Request('POST', dummyUrl); 193 var request = new http.Request('POST', dummyUrl);
270 request.encoding = LATIN1; 194 request.encoding = LATIN1;
271 expect(request.headers[HttpHeaders.CONTENT_TYPE], isNull); 195 expect(request.headers['Content-Type'], isNull);
272 }); 196 });
273 197
274 test('name is case insensitive', () { 198 test('name is case insensitive', () {
275 var request = new http.Request('POST', dummyUrl); 199 var request = new http.Request('POST', dummyUrl);
276 request.headers['CoNtEnT-tYpE'] = 'application/json'; 200 request.headers['CoNtEnT-tYpE'] = 'application/json';
277 expect(request.headers, 201 expect(request.headers,
278 containsPair('content-type', 'application/json')); 202 containsPair('content-type', 'application/json'));
279 }); 203 });
280 204
281 test('is set to application/x-www-form-urlencoded with charset utf-8 if ' 205 test('is set to application/x-www-form-urlencoded with charset utf-8 if '
282 'bodyFields is set', () { 206 'bodyFields is set', () {
283 var request = new http.Request('POST', dummyUrl); 207 var request = new http.Request('POST', dummyUrl);
284 request.bodyFields = {'hello': 'world'}; 208 request.bodyFields = {'hello': 'world'};
285 expect(request.headers[HttpHeaders.CONTENT_TYPE], 209 expect(request.headers['Content-Type'],
286 equals('application/x-www-form-urlencoded; charset=utf-8')); 210 equals('application/x-www-form-urlencoded; charset=utf-8'));
287 }); 211 });
288 212
289 test('is set to application/x-www-form-urlencoded with the given charset ' 213 test('is set to application/x-www-form-urlencoded with the given charset '
290 'if bodyFields and encoding are set', () { 214 'if bodyFields and encoding are set', () {
291 var request = new http.Request('POST', dummyUrl); 215 var request = new http.Request('POST', dummyUrl);
292 request.encoding = LATIN1; 216 request.encoding = LATIN1;
293 request.bodyFields = {'hello': 'world'}; 217 request.bodyFields = {'hello': 'world'};
294 expect(request.headers[HttpHeaders.CONTENT_TYPE], 218 expect(request.headers['Content-Type'],
295 equals('application/x-www-form-urlencoded; charset=iso-8859-1')); 219 equals('application/x-www-form-urlencoded; charset=iso-8859-1'));
296 }); 220 });
297 221
298 test('is set to text/plain and the given encoding if body and encoding are ' 222 test('is set to text/plain and the given encoding if body and encoding are '
299 'both set', () { 223 'both set', () {
300 var request = new http.Request('POST', dummyUrl); 224 var request = new http.Request('POST', dummyUrl);
301 request.encoding = LATIN1; 225 request.encoding = LATIN1;
302 request.body = 'hello, world'; 226 request.body = 'hello, world';
303 expect(request.headers[HttpHeaders.CONTENT_TYPE], 227 expect(request.headers['Content-Type'],
304 equals('text/plain; charset=iso-8859-1')); 228 equals('text/plain; charset=iso-8859-1'));
305 }); 229 });
306 230
307 test('is modified to include utf-8 if body is set', () { 231 test('is modified to include utf-8 if body is set', () {
308 var request = new http.Request('POST', dummyUrl); 232 var request = new http.Request('POST', dummyUrl);
309 request.headers[HttpHeaders.CONTENT_TYPE] = 'application/json'; 233 request.headers['Content-Type'] = 'application/json';
310 request.body = '{"hello": "world"}'; 234 request.body = '{"hello": "world"}';
311 expect(request.headers[HttpHeaders.CONTENT_TYPE], 235 expect(request.headers['Content-Type'],
312 equals('application/json; charset=utf-8')); 236 equals('application/json; charset=utf-8'));
313 }); 237 });
314 238
315 test('is modified to include the given encoding if encoding is set', () { 239 test('is modified to include the given encoding if encoding is set', () {
316 var request = new http.Request('POST', dummyUrl); 240 var request = new http.Request('POST', dummyUrl);
317 request.headers[HttpHeaders.CONTENT_TYPE] = 'application/json'; 241 request.headers['Content-Type'] = 'application/json';
318 request.encoding = LATIN1; 242 request.encoding = LATIN1;
319 expect(request.headers[HttpHeaders.CONTENT_TYPE], 243 expect(request.headers['Content-Type'],
320 equals('application/json; charset=iso-8859-1')); 244 equals('application/json; charset=iso-8859-1'));
321 }); 245 });
322 246
323 test('has its charset overridden by an explicit encoding', () { 247 test('has its charset overridden by an explicit encoding', () {
324 var request = new http.Request('POST', dummyUrl); 248 var request = new http.Request('POST', dummyUrl);
325 request.headers[HttpHeaders.CONTENT_TYPE] = 249 request.headers['Content-Type'] =
326 'application/json; charset=utf-8'; 250 'application/json; charset=utf-8';
327 request.encoding = LATIN1; 251 request.encoding = LATIN1;
328 expect(request.headers[HttpHeaders.CONTENT_TYPE], 252 expect(request.headers['Content-Type'],
329 equals('application/json; charset=iso-8859-1')); 253 equals('application/json; charset=iso-8859-1'));
330 }); 254 });
331 255
332 test("doen't have its charset overridden by setting bodyFields", () { 256 test("doen't have its charset overridden by setting bodyFields", () {
333 var request = new http.Request('POST', dummyUrl); 257 var request = new http.Request('POST', dummyUrl);
334 request.headers[HttpHeaders.CONTENT_TYPE] = 258 request.headers['Content-Type'] =
335 'application/x-www-form-urlencoded; charset=iso-8859-1'; 259 'application/x-www-form-urlencoded; charset=iso-8859-1';
336 request.bodyFields = {'hello': 'world'}; 260 request.bodyFields = {'hello': 'world'};
337 expect(request.headers[HttpHeaders.CONTENT_TYPE], 261 expect(request.headers['Content-Type'],
338 equals('application/x-www-form-urlencoded; charset=iso-8859-1')); 262 equals('application/x-www-form-urlencoded; charset=iso-8859-1'));
339 }); 263 });
340 264
341 test("doen't have its charset overridden by setting body", () { 265 test("doen't have its charset overridden by setting body", () {
342 var request = new http.Request('POST', dummyUrl); 266 var request = new http.Request('POST', dummyUrl);
343 request.headers[HttpHeaders.CONTENT_TYPE] = 267 request.headers['Content-Type'] =
344 'application/json; charset=iso-8859-1'; 268 'application/json; charset=iso-8859-1';
345 request.body = '{"hello": "world"}'; 269 request.body = '{"hello": "world"}';
346 expect(request.headers[HttpHeaders.CONTENT_TYPE], 270 expect(request.headers['Content-Type'],
347 equals('application/json; charset=iso-8859-1')); 271 equals('application/json; charset=iso-8859-1'));
348 }); 272 });
349 }); 273 });
350 274
351 group('#finalize', () { 275 group('#finalize', () {
352 test('returns a stream that emits the request body', () { 276 test('returns a stream that emits the request body', () {
353 var request = new http.Request('POST', dummyUrl); 277 var request = new http.Request('POST', dummyUrl);
354 request.body = "Hello, world!"; 278 request.body = "Hello, world!";
355 expect(request.finalize().bytesToString(), 279 expect(request.finalize().bytesToString(),
356 completion(equals("Hello, world!"))); 280 completion(equals("Hello, world!")));
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
423 }); 347 });
424 348
425 group('#toString()', () { 349 group('#toString()', () {
426 test('includes the method and URL', () { 350 test('includes the method and URL', () {
427 var request = new http.Request('POST', dummyUrl); 351 var request = new http.Request('POST', dummyUrl);
428 expect(request.toString(), 'POST $dummyUrl'); 352 expect(request.toString(), 'POST $dummyUrl');
429 }); 353 });
430 }); 354 });
431 } 355 }
432 356
OLDNEW
« no previous file with comments | « pkg/http/test/multipart_test.dart ('k') | pkg/http/test/streamed_request_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698