OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, 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 http.test.io.client_test; | |
6 | |
7 import 'dart:io'; | |
8 | |
9 import 'package:http/http.dart' as http; | |
10 import 'package:unittest/unittest.dart'; | |
11 | |
12 import 'utils.dart'; | |
13 | |
14 void main() { | |
15 tearDown(stopServer); | |
16 | |
17 test('#send a StreamedRequest', () { | |
18 expect(startServer().then((_) { | |
19 var client = new http.Client(); | |
20 var request = new http.StreamedRequest("POST", serverUrl); | |
21 request.headers[HttpHeaders.CONTENT_TYPE] = | |
22 'application/json; charset=utf-8'; | |
23 request.headers[HttpHeaders.USER_AGENT] = 'Dart'; | |
24 | |
25 expect(client.send(request).then((response) { | |
26 expect(response.request, equals(request)); | |
27 expect(response.statusCode, equals(200)); | |
28 expect(response.headers['single'], equals('value')); | |
29 // dart:io internally normalizes outgoing headers so that they never | |
30 // have multiple headers with the same name, so there's no way to test | |
31 // whether we handle that case correctly. | |
32 | |
33 return response.stream.bytesToString(); | |
34 }).whenComplete(client.close), completion(parse(equals({ | |
35 'method': 'POST', | |
36 'path': '/', | |
37 'headers': { | |
38 'content-type': ['application/json; charset=utf-8'], | |
39 'accept-encoding': ['gzip'], | |
40 'user-agent': ['Dart'], | |
41 'transfer-encoding': ['chunked'] | |
42 }, | |
43 'body': '{"hello": "world"}' | |
44 })))); | |
45 | |
46 request.sink.add('{"hello": "world"}'.codeUnits); | |
47 request.sink.close(); | |
48 }), completes); | |
49 }); | |
50 | |
51 test('#send a StreamedRequest with a custom client', () { | |
52 expect(startServer().then((_) { | |
53 var ioClient = new HttpClient(); | |
54 var client = new http.IOClient(ioClient); | |
55 var request = new http.StreamedRequest("POST", serverUrl); | |
56 request.headers[HttpHeaders.CONTENT_TYPE] = | |
57 'application/json; charset=utf-8'; | |
58 request.headers[HttpHeaders.USER_AGENT] = 'Dart'; | |
59 | |
60 expect(client.send(request).then((response) { | |
61 expect(response.request, equals(request)); | |
62 expect(response.statusCode, equals(200)); | |
63 expect(response.headers['single'], equals('value')); | |
64 // dart:io internally normalizes outgoing headers so that they never | |
65 // have multiple headers with the same name, so there's no way to test | |
66 // whether we handle that case correctly. | |
67 | |
68 return response.stream.bytesToString(); | |
69 }).whenComplete(client.close), completion(parse(equals({ | |
70 'method': 'POST', | |
71 'path': '/', | |
72 'headers': { | |
73 'content-type': ['application/json; charset=utf-8'], | |
74 'accept-encoding': ['gzip'], | |
75 'user-agent': ['Dart'], | |
76 'transfer-encoding': ['chunked'] | |
77 }, | |
78 'body': '{"hello": "world"}' | |
79 })))); | |
80 | |
81 request.sink.add('{"hello": "world"}'.codeUnits); | |
82 request.sink.close(); | |
83 }), completes); | |
84 }); | |
85 | |
86 test('#send with an invalid URL', () { | |
87 expect(startServer().then((_) { | |
88 var client = new http.Client(); | |
89 var url = Uri.parse('http://http.invalid'); | |
90 var request = new http.StreamedRequest("POST", url); | |
91 request.headers[HttpHeaders.CONTENT_TYPE] = | |
92 'application/json; charset=utf-8'; | |
93 | |
94 expect(client.send(request), throwsSocketException); | |
95 | |
96 request.sink.add('{"hello": "world"}'.codeUnits); | |
97 request.sink.close(); | |
98 }), completes); | |
99 }); | |
100 } | |
OLD | NEW |