Skip to content

Commit 2a22f2b

Browse files
authored
Refactor tests to async/await (#322)
1 parent 8662d61 commit 2a22f2b

10 files changed

+605
-763
lines changed

test/html/client_test.dart

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@ import 'package:test/test.dart';
1111
import 'utils.dart';
1212

1313
void main() {
14-
test('#send a StreamedRequest', () {
14+
test('#send a StreamedRequest', () async {
1515
var client = BrowserClient();
1616
var request = http.StreamedRequest('POST', echoUrl);
1717

18-
expect(
19-
client.send(request).then((response) {
20-
return response.stream.bytesToString();
21-
}).whenComplete(client.close),
22-
completion(equals('{"hello": "world"}')));
18+
var responseFuture = client.send(request);
19+
request.sink
20+
..add('{"hello": "world"}'.codeUnits)
21+
..close();
2322

24-
request.sink.add('{"hello": "world"}'.codeUnits);
25-
request.sink.close();
23+
var response = await responseFuture;
24+
var bytesString = await response.stream.bytesToString();
25+
client.close();
26+
27+
expect(bytesString, equals('{"hello": "world"}'));
2628
}, skip: 'Need to fix server tests for browser');
2729

2830
test('#send with an invalid URL', () {

test/html/streamed_request_test.dart

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,26 @@ import 'utils.dart';
1212

1313
void main() {
1414
group('contentLength', () {
15-
test("works when it's set", () {
15+
test("works when it's set", () async {
1616
var request = http.StreamedRequest('POST', echoUrl)
1717
..contentLength = 10
1818
..sink.add([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
1919
..sink.close();
2020

21-
return BrowserClient().send(request).then((response) {
22-
expect(response.stream.toBytes(),
23-
completion(equals([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])));
24-
});
21+
final response = await BrowserClient().send(request);
22+
23+
expect(await response.stream.toBytes(),
24+
equals([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));
2525
});
2626

27-
test("works when it's not set", () {
27+
test("works when it's not set", () async {
2828
var request = http.StreamedRequest('POST', echoUrl);
2929
request.sink.add([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
3030
request.sink.close();
3131

32-
return BrowserClient().send(request).then((response) {
33-
expect(response.stream.toBytes(),
34-
completion(equals([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])));
35-
});
32+
final response = await BrowserClient().send(request);
33+
expect(await response.stream.toBytes(),
34+
equals([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));
3635
});
3736
}, skip: 'Need to fix server tests for browser');
3837
}

test/io/client_test.dart

Lines changed: 83 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -13,99 +13,97 @@ import 'package:test/test.dart';
1313
import 'utils.dart';
1414

1515
void main() {
16+
setUp(startServer);
17+
1618
tearDown(stopServer);
1719

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();
1943
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+
})));
5456
});
5557

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();
5782
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+
})));
9395
});
9496

9597
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();
110108
});
111109
}

0 commit comments

Comments
 (0)