Skip to content

Commit c29a1e8

Browse files
authored
test(conformance): tests close client (#1906)
* test(http): conformance tests close client * Fix format * Close when needed
1 parent 6bfe07f commit c29a1e8

16 files changed

Lines changed: 111 additions & 44 deletions

pkgs/http_client_conformance_tests/lib/http_client_conformance_tests.dart

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -97,32 +97,30 @@ void testAll(
9797
bool supportsMultipartRequest = true,
9898
bool supportsAbort = false,
9999
}) {
100-
testRequestBody(clientFactory());
101-
testRequestBodyStreamed(clientFactory(),
100+
testRequestBody(clientFactory);
101+
testRequestBodyStreamed(clientFactory,
102102
canStreamRequestBody: canStreamRequestBody);
103-
testResponseBody(clientFactory(),
103+
testResponseBody(clientFactory, canStreamResponseBody: canStreamResponseBody);
104+
testResponseBodyStreamed(clientFactory,
104105
canStreamResponseBody: canStreamResponseBody);
105-
testResponseBodyStreamed(clientFactory(),
106-
canStreamResponseBody: canStreamResponseBody);
107-
testRequestHeaders(clientFactory());
108-
testRequestMethods(clientFactory(), preservesMethodCase: preservesMethodCase);
109-
testResponseHeaders(clientFactory(),
106+
testRequestHeaders(clientFactory);
107+
testRequestMethods(clientFactory, preservesMethodCase: preservesMethodCase);
108+
testResponseHeaders(clientFactory,
110109
supportsFoldedHeaders: supportsFoldedHeaders,
111110
correctlyHandlesNullHeaderValues: correctlyHandlesNullHeaderValues);
112-
testResponseStatusLine(clientFactory());
113-
testRedirect(clientFactory(), redirectAlwaysAllowed: redirectAlwaysAllowed);
114-
testServerErrors(clientFactory());
115-
testCompressedResponseBody(clientFactory());
111+
testResponseStatusLine(clientFactory);
112+
testRedirect(clientFactory, redirectAlwaysAllowed: redirectAlwaysAllowed);
113+
testServerErrors(clientFactory);
114+
testCompressedResponseBody(clientFactory);
116115
testMultipleClients(clientFactory);
117-
testMultipartRequests(clientFactory(),
116+
testMultipartRequests(clientFactory,
118117
supportsMultipartRequest: supportsMultipartRequest);
119118
testClose(clientFactory);
120119
testIsolate(clientFactory, canWorkInIsolates: canWorkInIsolates);
121-
testRequestCookies(clientFactory(),
122-
canSendCookieHeaders: canSendCookieHeaders);
123-
testResponseCookies(clientFactory(),
120+
testRequestCookies(clientFactory, canSendCookieHeaders: canSendCookieHeaders);
121+
testResponseCookies(clientFactory,
124122
canReceiveSetCookieHeaders: canReceiveSetCookieHeaders);
125-
testAbort(clientFactory(),
123+
testAbort(clientFactory,
126124
supportsAbort: supportsAbort,
127125
canStreamRequestBody: canStreamRequestBody,
128126
canStreamResponseBody: canStreamResponseBody);

pkgs/http_client_conformance_tests/lib/src/abort_tests.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,26 @@ import 'abort_server_vm.dart'
2626
/// [Client] supports sending HTTP requests with unbounded body sizes will be
2727
/// skipped.
2828
void testAbort(
29-
Client client, {
29+
Client Function() clientFactory, {
3030
bool supportsAbort = false,
3131
bool canStreamRequestBody = true,
3232
bool canStreamResponseBody = true,
3333
}) {
3434
group('abort', () {
35+
late Client client;
3536
late String host;
3637
late StreamChannel<Object?> httpServerChannel;
3738
late StreamQueue<Object?> httpServerQueue;
3839
late Uri serverUrl;
3940

4041
setUp(() async {
42+
client = clientFactory();
4143
httpServerChannel = await startServer();
4244
httpServerQueue = StreamQueue(httpServerChannel.stream);
4345
host = 'localhost:${await httpServerQueue.nextAsInt}';
4446
serverUrl = Uri.http(host, '');
4547
});
48+
tearDown(() => client.close());
4649
tearDownAll(() => httpServerChannel.sink.add(null));
4750

4851
test('before request', () async {

pkgs/http_client_conformance_tests/lib/src/compressed_response_body_tests.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ import 'compressed_response_body_server_vm.dart'
2222
///
2323
/// The value of `StreamedResponse.contentLength` is not defined for responses
2424
/// with a 'Content-Encoding' header.
25-
void testCompressedResponseBody(Client client) async {
25+
void testCompressedResponseBody(Client Function() clientFactory) {
2626
group('response body', () {
27+
late Client client;
2728
late final String host;
2829
late final StreamChannel<Object?> httpServerChannel;
2930
late final StreamQueue<Object?> httpServerQueue;
@@ -34,6 +35,8 @@ void testCompressedResponseBody(Client client) async {
3435
httpServerQueue = StreamQueue(httpServerChannel.stream);
3536
host = 'localhost:${await httpServerQueue.nextAsInt}';
3637
});
38+
setUp(() => client = clientFactory());
39+
tearDown(() => client.close());
3740
tearDownAll(() => httpServerChannel.sink.add(null));
3841

3942
test('gzip: small response with content length', () async {

pkgs/http_client_conformance_tests/lib/src/multipart_tests.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,30 @@ import 'multipart_server_vm.dart'
1414
///
1515
/// If [supportsMultipartRequest] is `false` then tests that assume that
1616
/// multipart requests can be sent will be skipped.
17-
void testMultipartRequests(Client client,
17+
void testMultipartRequests(Client Function() clientFactory,
1818
{required bool supportsMultipartRequest}) async {
1919
group('multipart requests', () {
20+
late Client client;
2021
late final String host;
2122
late final StreamChannel<Object?> httpServerChannel;
2223
late final StreamQueue<Object?> httpServerQueue;
2324

25+
setUp(() => client = clientFactory());
2426
setUpAll(() async {
2527
httpServerChannel = await startServer();
2628
httpServerQueue = StreamQueue(httpServerChannel.stream);
2729
host = 'localhost:${await httpServerQueue.nextAsInt}';
2830
});
31+
tearDown(() => client.close());
2932
tearDownAll(() => httpServerChannel.sink.add(null));
3033

3134
test('attached file', () async {
3235
final request = MultipartRequest('POST', Uri.http(host, ''));
3336

3437
request.files.add(MultipartFile.fromString('file1', 'Hello World'));
3538

36-
await client.send(request);
39+
final response = await client.send(request);
40+
await response.stream.drain<void>();
3741
final serverRequest = await httpServerQueue.next as List;
3842
final headers = (serverRequest[0] as Map).cast<String, List<Object?>>();
3943
final body = serverRequest[1] as String;

pkgs/http_client_conformance_tests/lib/src/redirect_tests.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ import 'redirect_server_vm.dart'
1414
///
1515
/// If [redirectAlwaysAllowed] is `true` then tests that require the [Client]
1616
/// to limit redirects will be skipped.
17-
void testRedirect(Client client, {bool redirectAlwaysAllowed = false}) async {
17+
void testRedirect(Client Function() clientFactory,
18+
{bool redirectAlwaysAllowed = false}) {
1819
group('redirects', () {
20+
late Client client;
1921
late final String host;
2022
late final StreamChannel<Object?> httpServerChannel;
2123
late final StreamQueue<Object?> httpServerQueue;
@@ -25,12 +27,15 @@ void testRedirect(Client client, {bool redirectAlwaysAllowed = false}) async {
2527
httpServerQueue = StreamQueue(httpServerChannel.stream);
2628
host = 'localhost:${await httpServerQueue.nextAsInt}';
2729
});
30+
setUp(() => client = clientFactory());
31+
tearDown(() => client.close());
2832
tearDownAll(() => httpServerChannel.sink.add(null));
2933

3034
test('no redirect', () async {
3135
final request = Request('GET', Uri.http(host, '/'))
3236
..followRedirects = false;
3337
final response = await client.send(request);
38+
await response.stream.drain<void>();
3439
expect(response.statusCode, 200);
3540
expect(response.isRedirect, false);
3641
if (response case BaseResponseWithUrl(url: final url)) {
@@ -42,6 +47,7 @@ void testRedirect(Client client, {bool redirectAlwaysAllowed = false}) async {
4247
final request = Request('GET', Uri.http(host, '/1'))
4348
..followRedirects = false;
4449
final response = await client.send(request);
50+
await response.stream.drain<void>();
4551
expect(response.statusCode, 302);
4652
expect(response.isRedirect, true);
4753
if (response case BaseResponseWithUrl(url: final url)) {
@@ -54,6 +60,7 @@ void testRedirect(Client client, {bool redirectAlwaysAllowed = false}) async {
5460
..followRedirects = false
5561
..maxRedirects = 0;
5662
final response = await client.send(request);
63+
await response.stream.drain<void>();
5764
expect(response.statusCode, 302);
5865
expect(response.isRedirect, true);
5966
if (response case BaseResponseWithUrl(url: final url)) {
@@ -65,6 +72,7 @@ void testRedirect(Client client, {bool redirectAlwaysAllowed = false}) async {
6572
final request = Request('GET', Uri.http(host, '/1'))
6673
..followRedirects = true;
6774
final response = await client.send(request);
75+
await response.stream.drain<void>();
6876
expect(response.statusCode, 200);
6977
expect(response.isRedirect, false);
7078
if (response case BaseResponseWithUrl(url: final url)) {
@@ -87,6 +95,7 @@ void testRedirect(Client client, {bool redirectAlwaysAllowed = false}) async {
8795
..followRedirects = true
8896
..maxRedirects = 5;
8997
final response = await client.send(request);
98+
await response.stream.drain<void>();
9099
expect(response.statusCode, 200);
91100
expect(response.isRedirect, false);
92101
if (response case BaseResponseWithUrl(url: final url)) {

pkgs/http_client_conformance_tests/lib/src/request_body_streamed_tests.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,24 @@ import 'request_body_streamed_server_vm.dart'
1919
/// If [canStreamRequestBody] is `false` then tests that assume that the
2020
/// [Client] supports sending HTTP requests with unbounded body sizes will be
2121
/// skipped.
22-
void testRequestBodyStreamed(Client client,
22+
void testRequestBodyStreamed(Client Function() clientFactory,
2323
{bool canStreamRequestBody = true}) {
2424
group('streamed requests', () {
25+
late Client client;
2526
late String host;
2627
late StreamChannel<Object?> httpServerChannel;
2728
late StreamQueue<Object?> httpServerQueue;
2829

2930
setUp(() async {
31+
client = clientFactory();
3032
httpServerChannel = await startServer();
3133
httpServerQueue = StreamQueue(httpServerChannel.stream);
3234
host = 'localhost:${await httpServerQueue.nextAsInt}';
3335
});
34-
tearDown(() => httpServerChannel.sink.add(null));
36+
tearDown(() {
37+
client.close();
38+
httpServerChannel.sink.add(null);
39+
});
3540

3641
test('client.send() with StreamedRequest', () async {
3742
// The client continuously streams data to the server until
@@ -56,7 +61,8 @@ void testRequestBodyStreamed(Client client,
5661
final request = StreamedRequest('POST', Uri.http(host, ''));
5762
const Utf8Encoder().bind(count()).listen(request.sink.add,
5863
onError: request.sink.addError, onDone: request.sink.close);
59-
await client.send(request);
64+
final response = await client.send(request);
65+
await response.stream.drain<void>();
6066

6167
expect(lastReceived, greaterThanOrEqualTo(1000));
6268
});

pkgs/http_client_conformance_tests/lib/src/request_body_tests.dart

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,23 @@ class _Plus2Encoding extends Encoding {
3838

3939
/// Tests that the [Client] correctly implements HTTP requests with bodies e.g.
4040
/// 'POST'.
41-
void testRequestBody(Client client) {
41+
void testRequestBody(Client Function() clientFactory) {
4242
group('request body', () {
43+
late Client client;
4344
late String host;
4445
late StreamChannel<Object?> httpServerChannel;
4546
late StreamQueue<Object?> httpServerQueue;
4647

4748
setUp(() async {
49+
client = clientFactory();
4850
httpServerChannel = await startServer();
4951
httpServerQueue = StreamQueue(httpServerChannel.stream);
5052
host = 'localhost:${await httpServerQueue.nextAsInt}';
5153
});
52-
tearDown(() => httpServerChannel.sink.add(null));
54+
tearDown(() {
55+
client.close();
56+
httpServerChannel.sink.add(null);
57+
});
5358

5459
test('client.post() with string body', () async {
5560
await client.post(Uri.http(host, ''), body: 'Hello World!');
@@ -164,7 +169,8 @@ void testRequestBody(Client client) {
164169
request.sink.add([5]);
165170
// ignore: unawaited_futures
166171
request.sink.close();
167-
await client.send(request);
172+
final response = await client.send(request);
173+
await response.stream.drain<void>();
168174

169175
final serverReceivedContentType = await httpServerQueue.next;
170176
final serverReceivedBody = await httpServerQueue.next as String;
@@ -197,7 +203,8 @@ void testRequestBody(Client client) {
197203

198204
stream().listen(request.sink.add,
199205
onError: request.sink.addError, onDone: request.sink.close);
200-
await client.send(request);
206+
final response = await client.send(request);
207+
await response.stream.drain<void>();
201208

202209
final serverReceivedContentType = await httpServerQueue.next;
203210
final serverReceivedBody = await httpServerQueue.next as String;
@@ -230,6 +237,7 @@ void testRequestBody(Client client) {
230237
request.sink.close();
231238

232239
final response = await client.send(request);
240+
await response.stream.drain<void>();
233241
expect(response.statusCode, 200);
234242

235243
final serverReceivedContentType = await httpServerQueue.next;
@@ -249,6 +257,7 @@ void testRequestBody(Client client) {
249257
request.sink.close();
250258

251259
final response = await client.send(request);
260+
await response.stream.drain<void>();
252261
expect(response.statusCode, 200);
253262

254263
final serverReceivedContentType = await httpServerQueue.next;
@@ -268,6 +277,7 @@ void testRequestBody(Client client) {
268277
..body = 'Hello World $i';
269278

270279
final response = await client.send(request);
280+
await response.stream.drain<void>();
271281
expect(response.statusCode, 200);
272282

273283
final serverReceivedContentType = await httpServerQueue.next;
@@ -290,6 +300,7 @@ void testRequestBody(Client client) {
290300
..body = body;
291301

292302
final response = await client.send(request);
303+
await response.stream.drain<void>();
293304
expect(response.statusCode, 200);
294305

295306
final serverReceivedContentType = await httpServerQueue.next;
@@ -307,6 +318,7 @@ void testRequestBody(Client client) {
307318
request.sink.close();
308319

309320
final response = await client.send(request);
321+
await response.stream.drain<void>();
310322
expect(response.statusCode, 200);
311323

312324
final serverReceivedContentType = await httpServerQueue.next;

pkgs/http_client_conformance_tests/lib/src/request_cookies_test.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ final headerSplitter = RegExp(':[ \t]+');
1717
///
1818
/// If [canSendCookieHeaders] is `false` then tests that require that "cookie"
1919
/// headers be sent by the client will not be run.
20-
void testRequestCookies(Client client,
21-
{bool canSendCookieHeaders = false}) async {
20+
void testRequestCookies(Client Function() clientFactory,
21+
{bool canSendCookieHeaders = false}) {
2222
group('request cookies', () {
23+
late Client client;
2324
late final String host;
2425
late final StreamChannel<Object?> httpServerChannel;
2526
late final StreamQueue<Object?> httpServerQueue;
@@ -29,6 +30,8 @@ void testRequestCookies(Client client,
2930
httpServerQueue = StreamQueue(httpServerChannel.stream);
3031
host = 'localhost:${await httpServerQueue.nextAsInt}';
3132
});
33+
setUp(() => client = clientFactory());
34+
tearDown(() => client.close());
3235
tearDownAll(() => httpServerChannel.sink.add(null));
3336

3437
test('one cookie', () async {

pkgs/http_client_conformance_tests/lib/src/request_headers_tests.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,20 @@ import 'request_headers_server_vm.dart'
1111
if (dart.library.js_interop) 'request_headers_server_web.dart';
1212

1313
/// Tests that the [Client] correctly sends headers in the request.
14-
void testRequestHeaders(Client client) async {
14+
void testRequestHeaders(Client Function() clientFactory) async {
1515
group('client headers', () {
16+
late Client client;
1617
late final String host;
1718
late final StreamChannel<Object?> httpServerChannel;
1819
late final StreamQueue<Object?> httpServerQueue;
1920

21+
setUp(() => client = clientFactory());
2022
setUpAll(() async {
2123
httpServerChannel = await startServer();
2224
httpServerQueue = StreamQueue(httpServerChannel.stream);
2325
host = 'localhost:${await httpServerQueue.nextAsInt}';
2426
});
27+
tearDown(() => client.close());
2528
tearDownAll(() => httpServerChannel.sink.add(null));
2629

2730
test('single header', () async {

pkgs/http_client_conformance_tests/lib/src/request_methods_tests.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,39 @@ import 'request_methods_server_vm.dart'
1515
///
1616
/// If [preservesMethodCase] is `false` then tests that assume that the
1717
/// [Client] preserves custom request method casing will be skipped.
18-
void testRequestMethods(Client client,
18+
void testRequestMethods(Client Function() clientFactory,
1919
{bool preservesMethodCase = true}) async {
2020
group('request methods', () {
21+
late Client client;
2122
late final String host;
2223
late final StreamChannel<Object?> httpServerChannel;
2324
late final StreamQueue<Object?> httpServerQueue;
2425

26+
setUp(() => client = clientFactory());
2527
setUpAll(() async {
2628
httpServerChannel = await startServer();
2729
httpServerQueue = StreamQueue(httpServerChannel.stream);
2830
host = 'localhost:${await httpServerQueue.nextAsInt}';
2931
});
32+
tearDown(() => client.close());
3033
tearDownAll(() => httpServerChannel.sink.add(null));
3134

3235
test('custom method - not case preserving', () async {
33-
await client.send(Request(
36+
final response = await client.send(Request(
3437
'CuStOm',
3538
Uri.http(host, ''),
3639
));
40+
await response.stream.drain<void>();
3741
final method = await httpServerQueue.next as String;
3842
expect('CUSTOM', method.toUpperCase());
3943
});
4044

4145
test('custom method case preserving', () async {
42-
await client.send(Request(
46+
final response = await client.send(Request(
4347
'CuStOm',
4448
Uri.http(host, ''),
4549
));
50+
await response.stream.drain<void>();
4651
final method = await httpServerQueue.next as String;
4752
expect('CuStOm', method);
4853
},

0 commit comments

Comments
 (0)