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