|
| 1 | +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:io'; |
| 6 | + |
| 7 | +import 'package:http/http.dart'; |
| 8 | +import 'package:integration_test/integration_test.dart'; |
| 9 | +import 'package:ok_http/ok_http.dart'; |
| 10 | +import 'package:test/test.dart'; |
| 11 | + |
| 12 | +void testTimeouts() { |
| 13 | + group('timeouts', () { |
| 14 | + group('call timeout', () { |
| 15 | + late HttpServer server; |
| 16 | + |
| 17 | + setUp(() async { |
| 18 | + server = (await HttpServer.bind('localhost', 0)) |
| 19 | + ..listen((request) async { |
| 20 | + // Add a delay of `n` seconds for URI `http://localhost:port/n` |
| 21 | + final delay = int.parse(request.requestedUri.pathSegments.last); |
| 22 | + await Future<void>.delayed(Duration(seconds: delay)); |
| 23 | + |
| 24 | + await request.drain<void>(); |
| 25 | + await request.response.close(); |
| 26 | + }); |
| 27 | + }); |
| 28 | + tearDown(() { |
| 29 | + server.close(); |
| 30 | + }); |
| 31 | + |
| 32 | + test('exceeded', () { |
| 33 | + final client = OkHttpClient( |
| 34 | + configuration: const OkHttpClientConfiguration( |
| 35 | + callTimeout: Duration(milliseconds: 500), |
| 36 | + ), |
| 37 | + ); |
| 38 | + expect( |
| 39 | + () async { |
| 40 | + await client.get(Uri.parse('http://localhost:${server.port}/1')); |
| 41 | + }, |
| 42 | + throwsA( |
| 43 | + isA<ClientException>().having( |
| 44 | + (exception) => exception.message, |
| 45 | + 'message', |
| 46 | + startsWith('java.io.InterruptedIOException'), |
| 47 | + ), |
| 48 | + ), |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + test('not exceeded', () async { |
| 53 | + final client = OkHttpClient( |
| 54 | + configuration: const OkHttpClientConfiguration( |
| 55 | + callTimeout: Duration(milliseconds: 1500), |
| 56 | + ), |
| 57 | + ); |
| 58 | + final response = await client.send( |
| 59 | + Request( |
| 60 | + 'GET', |
| 61 | + Uri.http('localhost:${server.port}', '1'), |
| 62 | + ), |
| 63 | + ); |
| 64 | + |
| 65 | + expect(response.statusCode, 200); |
| 66 | + expect(response.contentLength, 0); |
| 67 | + }); |
| 68 | + |
| 69 | + test('not set', () async { |
| 70 | + final client = OkHttpClient(); |
| 71 | + |
| 72 | + expect( |
| 73 | + () async { |
| 74 | + await client.send( |
| 75 | + Request( |
| 76 | + 'GET', |
| 77 | + Uri.http('localhost:${server.port}', '11'), |
| 78 | + ), |
| 79 | + ); |
| 80 | + }, |
| 81 | + throwsA( |
| 82 | + isA<ClientException>().having( |
| 83 | + (exception) => exception.message, |
| 84 | + 'message', |
| 85 | + startsWith('java.net.SocketTimeoutException'), |
| 86 | + ), |
| 87 | + ), |
| 88 | + ); |
| 89 | + }); |
| 90 | + }); |
| 91 | + }); |
| 92 | +} |
| 93 | + |
| 94 | +void main() { |
| 95 | + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 96 | + |
| 97 | + testTimeouts(); |
| 98 | +} |
0 commit comments