OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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:io'; | 8 import 'dart:io'; |
8 | 9 |
9 import 'package:http/http.dart' as http; | 10 import 'package:http/http.dart' as http; |
10 import 'package:unittest/unittest.dart'; | 11 import 'package:unittest/unittest.dart'; |
11 | 12 |
12 import 'utils.dart'; | 13 import 'utils.dart'; |
13 | 14 |
14 void main() { | 15 void main() { |
15 test('.send', () { | 16 test('.send', () { |
16 expect(startServer().then((_) { | 17 expect(startServer().then((_) { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 | 56 |
56 test('is not directly mutable', () { | 57 test('is not directly mutable', () { |
57 var request = new http.Request('POST', dummyUrl); | 58 var request = new http.Request('POST', dummyUrl); |
58 expect(() => request.contentLength = 50, throwsUnsupportedError); | 59 expect(() => request.contentLength = 50, throwsUnsupportedError); |
59 }); | 60 }); |
60 }); | 61 }); |
61 | 62 |
62 group('#encoding', () { | 63 group('#encoding', () { |
63 test('defaults to utf-8', () { | 64 test('defaults to utf-8', () { |
64 var request = new http.Request('POST', dummyUrl); | 65 var request = new http.Request('POST', dummyUrl); |
65 expect(request.encoding.name, equals(Encoding.UTF_8.name)); | 66 expect(request.encoding.name, equals(UTF8.name)); |
66 }); | 67 }); |
67 | 68 |
68 test('can be set', () { | 69 test('can be set', () { |
69 var request = new http.Request('POST', dummyUrl); | 70 var request = new http.Request('POST', dummyUrl); |
70 request.encoding = Encoding.ISO_8859_1; | 71 request.encoding = LATIN1; |
71 expect(request.encoding.name, equals(Encoding.ISO_8859_1.name)); | 72 expect(request.encoding.name, equals(LATIN1.name)); |
72 }); | 73 }); |
73 | 74 |
74 test('is based on the content-type charset if it exists', () { | 75 test('is based on the content-type charset if it exists', () { |
75 var request = new http.Request('POST', dummyUrl); | 76 var request = new http.Request('POST', dummyUrl); |
76 request.headers[HttpHeaders.CONTENT_TYPE] = | 77 request.headers[HttpHeaders.CONTENT_TYPE] = |
77 'text/plain; charset=iso-8859-1'; | 78 'text/plain; charset=iso-8859-1'; |
78 expect(request.encoding.name, equals(Encoding.ISO_8859_1.name)); | 79 expect(request.encoding.name, equals(LATIN1.name)); |
79 }); | 80 }); |
80 | 81 |
81 test('remains the default if the content-type charset is set and unset', | 82 test('remains the default if the content-type charset is set and unset', |
82 () { | 83 () { |
83 var request = new http.Request('POST', dummyUrl); | 84 var request = new http.Request('POST', dummyUrl); |
84 request.encoding = Encoding.ISO_8859_1; | 85 request.encoding = LATIN1; |
85 request.headers[HttpHeaders.CONTENT_TYPE] = | 86 request.headers[HttpHeaders.CONTENT_TYPE] = |
86 'text/plain; charset=utf-8'; | 87 'text/plain; charset=utf-8'; |
87 expect(request.encoding.name, equals(Encoding.UTF_8.name)); | 88 expect(request.encoding.name, equals(UTF8.name)); |
88 | 89 |
89 request.headers.remove(HttpHeaders.CONTENT_TYPE); | 90 request.headers.remove(HttpHeaders.CONTENT_TYPE); |
90 expect(request.encoding.name, equals(Encoding.ISO_8859_1.name)); | 91 expect(request.encoding.name, equals(LATIN1.name)); |
91 }); | 92 }); |
92 | 93 |
93 test('throws an error if the content-type charset is unknown', () { | 94 test('throws an error if the content-type charset is unknown', () { |
94 var request = new http.Request('POST', dummyUrl); | 95 var request = new http.Request('POST', dummyUrl); |
95 request.headers[HttpHeaders.CONTENT_TYPE] = | 96 request.headers[HttpHeaders.CONTENT_TYPE] = |
96 'text/plain; charset=not-a-real-charset'; | 97 'text/plain; charset=not-a-real-charset'; |
97 expect(() => request.encoding, throwsFormatException); | 98 expect(() => request.encoding, throwsFormatException); |
98 }); | 99 }); |
99 }); | 100 }); |
100 | 101 |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 }); | 234 }); |
234 | 235 |
235 group('content-type header', () { | 236 group('content-type header', () { |
236 test('defaults to empty', () { | 237 test('defaults to empty', () { |
237 var request = new http.Request('POST', dummyUrl); | 238 var request = new http.Request('POST', dummyUrl); |
238 expect(request.headers[HttpHeaders.CONTENT_TYPE], isNull); | 239 expect(request.headers[HttpHeaders.CONTENT_TYPE], isNull); |
239 }); | 240 }); |
240 | 241 |
241 test('defaults to empty if only encoding is set', () { | 242 test('defaults to empty if only encoding is set', () { |
242 var request = new http.Request('POST', dummyUrl); | 243 var request = new http.Request('POST', dummyUrl); |
243 request.encoding = Encoding.ISO_8859_1; | 244 request.encoding = LATIN1; |
244 expect(request.headers[HttpHeaders.CONTENT_TYPE], isNull); | 245 expect(request.headers[HttpHeaders.CONTENT_TYPE], isNull); |
245 }); | 246 }); |
246 | 247 |
247 test('is set to application/x-www-form-urlencoded with charset utf-8 if ' | 248 test('is set to application/x-www-form-urlencoded with charset utf-8 if ' |
248 'bodyFields is set', () { | 249 'bodyFields is set', () { |
249 var request = new http.Request('POST', dummyUrl); | 250 var request = new http.Request('POST', dummyUrl); |
250 request.bodyFields = {'hello': 'world'}; | 251 request.bodyFields = {'hello': 'world'}; |
251 expect(request.headers[HttpHeaders.CONTENT_TYPE], | 252 expect(request.headers[HttpHeaders.CONTENT_TYPE], |
252 equals('application/x-www-form-urlencoded; charset=utf-8')); | 253 equals('application/x-www-form-urlencoded; charset=utf-8')); |
253 }); | 254 }); |
254 | 255 |
255 test('is set to application/x-www-form-urlencoded with the given charset ' | 256 test('is set to application/x-www-form-urlencoded with the given charset ' |
256 'if bodyFields and encoding are set', () { | 257 'if bodyFields and encoding are set', () { |
257 var request = new http.Request('POST', dummyUrl); | 258 var request = new http.Request('POST', dummyUrl); |
258 request.encoding = Encoding.ISO_8859_1; | 259 request.encoding = LATIN1; |
259 request.bodyFields = {'hello': 'world'}; | 260 request.bodyFields = {'hello': 'world'}; |
260 expect(request.headers[HttpHeaders.CONTENT_TYPE], | 261 expect(request.headers[HttpHeaders.CONTENT_TYPE], |
261 equals('application/x-www-form-urlencoded; charset=iso-8859-1')); | 262 equals('application/x-www-form-urlencoded; charset=iso-8859-1')); |
262 }); | 263 }); |
263 | 264 |
264 test('is set to text/plain and the given encoding if body and encoding are ' | 265 test('is set to text/plain and the given encoding if body and encoding are ' |
265 'both set', () { | 266 'both set', () { |
266 var request = new http.Request('POST', dummyUrl); | 267 var request = new http.Request('POST', dummyUrl); |
267 request.encoding = Encoding.ISO_8859_1; | 268 request.encoding = LATIN1; |
268 request.body = 'hello, world'; | 269 request.body = 'hello, world'; |
269 expect(request.headers[HttpHeaders.CONTENT_TYPE], | 270 expect(request.headers[HttpHeaders.CONTENT_TYPE], |
270 equals('text/plain; charset=iso-8859-1')); | 271 equals('text/plain; charset=iso-8859-1')); |
271 }); | 272 }); |
272 | 273 |
273 test('is modified to include utf-8 if body is set', () { | 274 test('is modified to include utf-8 if body is set', () { |
274 var request = new http.Request('POST', dummyUrl); | 275 var request = new http.Request('POST', dummyUrl); |
275 request.headers[HttpHeaders.CONTENT_TYPE] = 'application/json'; | 276 request.headers[HttpHeaders.CONTENT_TYPE] = 'application/json'; |
276 request.body = '{"hello": "world"}'; | 277 request.body = '{"hello": "world"}'; |
277 expect(request.headers[HttpHeaders.CONTENT_TYPE], | 278 expect(request.headers[HttpHeaders.CONTENT_TYPE], |
278 equals('application/json; charset=utf-8')); | 279 equals('application/json; charset=utf-8')); |
279 }); | 280 }); |
280 | 281 |
281 test('is modified to include the given encoding if encoding is set', () { | 282 test('is modified to include the given encoding if encoding is set', () { |
282 var request = new http.Request('POST', dummyUrl); | 283 var request = new http.Request('POST', dummyUrl); |
283 request.headers[HttpHeaders.CONTENT_TYPE] = 'application/json'; | 284 request.headers[HttpHeaders.CONTENT_TYPE] = 'application/json'; |
284 request.encoding = Encoding.ISO_8859_1; | 285 request.encoding = LATIN1; |
285 expect(request.headers[HttpHeaders.CONTENT_TYPE], | 286 expect(request.headers[HttpHeaders.CONTENT_TYPE], |
286 equals('application/json; charset=iso-8859-1')); | 287 equals('application/json; charset=iso-8859-1')); |
287 }); | 288 }); |
288 | 289 |
289 test('has its charset overridden by an explicit encoding', () { | 290 test('has its charset overridden by an explicit encoding', () { |
290 var request = new http.Request('POST', dummyUrl); | 291 var request = new http.Request('POST', dummyUrl); |
291 request.headers[HttpHeaders.CONTENT_TYPE] = | 292 request.headers[HttpHeaders.CONTENT_TYPE] = |
292 'application/json; charset=utf-8'; | 293 'application/json; charset=utf-8'; |
293 request.encoding = Encoding.ISO_8859_1; | 294 request.encoding = LATIN1; |
294 expect(request.headers[HttpHeaders.CONTENT_TYPE], | 295 expect(request.headers[HttpHeaders.CONTENT_TYPE], |
295 equals('application/json; charset=iso-8859-1')); | 296 equals('application/json; charset=iso-8859-1')); |
296 }); | 297 }); |
297 | 298 |
298 test("doen't have its charset overridden by setting bodyFields", () { | 299 test("doen't have its charset overridden by setting bodyFields", () { |
299 var request = new http.Request('POST', dummyUrl); | 300 var request = new http.Request('POST', dummyUrl); |
300 request.headers[HttpHeaders.CONTENT_TYPE] = | 301 request.headers[HttpHeaders.CONTENT_TYPE] = |
301 'application/x-www-form-urlencoded; charset=iso-8859-1'; | 302 'application/x-www-form-urlencoded; charset=iso-8859-1'; |
302 request.bodyFields = {'hello': 'world'}; | 303 request.bodyFields = {'hello': 'world'}; |
303 expect(request.headers[HttpHeaders.CONTENT_TYPE], | 304 expect(request.headers[HttpHeaders.CONTENT_TYPE], |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 request.finalize(); | 344 request.finalize(); |
344 | 345 |
345 expect(request.maxRedirects, equals(5)); | 346 expect(request.maxRedirects, equals(5)); |
346 expect(() => request.maxRedirects = 10, throwsStateError); | 347 expect(() => request.maxRedirects = 10, throwsStateError); |
347 }); | 348 }); |
348 | 349 |
349 test('freezes #encoding', () { | 350 test('freezes #encoding', () { |
350 var request = new http.Request('POST', dummyUrl); | 351 var request = new http.Request('POST', dummyUrl); |
351 request.finalize(); | 352 request.finalize(); |
352 | 353 |
353 expect(request.encoding.name, equals(Encoding.UTF_8.name)); | 354 expect(request.encoding.name, equals(UTF8.name)); |
354 expect(() => request.encoding = Encoding.ASCII, throwsStateError); | 355 expect(() => request.encoding = ASCII, throwsStateError); |
355 }); | 356 }); |
356 | 357 |
357 test('freezes #bodyBytes', () { | 358 test('freezes #bodyBytes', () { |
358 var request = new http.Request('POST', dummyUrl); | 359 var request = new http.Request('POST', dummyUrl); |
359 request.bodyBytes = [1, 2, 3]; | 360 request.bodyBytes = [1, 2, 3]; |
360 request.finalize(); | 361 request.finalize(); |
361 | 362 |
362 expect(request.bodyBytes, equals([1, 2, 3])); | 363 expect(request.bodyBytes, equals([1, 2, 3])); |
363 expect(() => request.bodyBytes = [4, 5, 6], throwsStateError); | 364 expect(() => request.bodyBytes = [4, 5, 6], throwsStateError); |
364 }); | 365 }); |
(...skipping 24 matching lines...) Expand all Loading... |
389 }); | 390 }); |
390 | 391 |
391 group('#toString()', () { | 392 group('#toString()', () { |
392 test('includes the method and URL', () { | 393 test('includes the method and URL', () { |
393 var request = new http.Request('POST', dummyUrl); | 394 var request = new http.Request('POST', dummyUrl); |
394 expect(request.toString(), 'POST $dummyUrl'); | 395 expect(request.toString(), 'POST $dummyUrl'); |
395 }); | 396 }); |
396 }); | 397 }); |
397 } | 398 } |
398 | 399 |
OLD | NEW |