22// for details. All rights reserved. Use of this source code is governed by a
33// BSD-style license that can be found in the LICENSE file.
44
5+ import 'dart:async' ;
56import 'dart:convert' ;
67
78import 'package:async/async.dart' ;
@@ -21,16 +22,16 @@ import 'response_body_streamed_server_vm.dart'
2122void testResponseBodyStreamed (Client client,
2223 {bool canStreamResponseBody = true }) async {
2324 group ('streamed response body' , () {
24- late final String host;
25- late final StreamChannel <Object ?> httpServerChannel;
26- late final StreamQueue <Object ?> httpServerQueue;
25+ late String host;
26+ late StreamChannel <Object ?> httpServerChannel;
27+ late StreamQueue <Object ?> httpServerQueue;
2728
28- setUpAll (() async {
29+ setUp (() async {
2930 httpServerChannel = await startServer ();
3031 httpServerQueue = StreamQueue (httpServerChannel.stream);
3132 host = 'localhost:${await httpServerQueue .nextAsInt }' ;
3233 });
33- tearDownAll (() => httpServerChannel.sink.add (null ));
34+ tearDown (() => httpServerChannel.sink.add (null ));
3435
3536 test ('large response streamed without content length' , () async {
3637 // The server continuously streams data to the client until
@@ -56,6 +57,25 @@ void testResponseBodyStreamed(Client client,
5657 expect (response.reasonPhrase, 'OK' );
5758 expect (response.request! .method, 'GET' );
5859 expect (response.statusCode, 200 );
59- }, skip: canStreamResponseBody ? false : 'does not stream response bodies' );
60- });
60+ });
61+
62+ test ('cancel streamed response' , () async {
63+ final request = Request ('GET' , Uri .http (host, '' ));
64+ final response = await client.send (request);
65+ final cancelled = Completer <void >();
66+ expect (response.reasonPhrase, 'OK' );
67+ expect (response.statusCode, 200 );
68+ late StreamSubscription <String > subscription;
69+ subscription = const LineSplitter ()
70+ .bind (const Utf8Decoder ().bind (response.stream))
71+ .listen ((s) async {
72+ final lastReceived = int .parse (s.trim ());
73+ if (lastReceived == 1000 ) {
74+ unawaited (subscription.cancel ());
75+ cancelled.complete ();
76+ }
77+ });
78+ await cancelled.future;
79+ });
80+ }, skip: canStreamResponseBody ? false : 'does not stream response bodies' );
6181}
0 commit comments