LCOV - code coverage report
Current view: top level - lib/src/utils - http_timeout.dart (source / functions) Hit Total Coverage
Test: merged.info Lines: 15 15 100.0 %
Date: 2024-09-28 12:47:43 Functions: 0 0 -

          Line data    Source code
       1             : /*
       2             :  *   Famedly Matrix SDK
       3             :  *   Copyright (C) 2021 Famedly GmbH
       4             :  *
       5             :  *   This program is free software: you can redistribute it and/or modify
       6             :  *   it under the terms of the GNU Affero General Public License as
       7             :  *   published by the Free Software Foundation, either version 3 of the
       8             :  *   License, or (at your option) any later version.
       9             :  *
      10             :  *   This program is distributed in the hope that it will be useful,
      11             :  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      13             :  *   GNU Affero General Public License for more details.
      14             :  *
      15             :  *   You should have received a copy of the GNU Affero General Public License
      16             :  *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
      17             :  */
      18             : 
      19             : import 'dart:async';
      20             : 
      21             : import 'package:http/http.dart' as http;
      22             : 
      23          35 : http.StreamedResponse replaceStream(
      24             :         http.StreamedResponse base, Stream<List<int>> stream) =>
      25          35 :     http.StreamedResponse(
      26          35 :       http.ByteStream(stream),
      27          35 :       base.statusCode,
      28          35 :       contentLength: base.contentLength,
      29          35 :       request: base.request,
      30          35 :       headers: base.headers,
      31          35 :       isRedirect: base.isRedirect,
      32          35 :       persistentConnection: base.persistentConnection,
      33          35 :       reasonPhrase: base.reasonPhrase,
      34             :     );
      35             : 
      36             : /// Http Client that enforces a timeout on requests.
      37             : /// Timeout calculation is done in a subclass.
      38             : abstract class TimeoutHttpClient extends http.BaseClient {
      39          39 :   TimeoutHttpClient(this.inner);
      40             : 
      41             :   http.Client inner;
      42             : 
      43             :   Duration get timeout;
      44             : 
      45          35 :   @override
      46             :   Future<http.StreamedResponse> send(http.BaseRequest request) async {
      47          70 :     final response = await inner.send(request);
      48         140 :     return replaceStream(response, response.stream.timeout(timeout));
      49             :   }
      50             : }
      51             : 
      52             : class FixedTimeoutHttpClient extends TimeoutHttpClient {
      53          39 :   FixedTimeoutHttpClient(super.inner, this.timeout);
      54             :   @override
      55             :   Duration timeout;
      56             : }

Generated by: LCOV version 1.14