-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkgs/ok_http: OkHttpClientConfiguration and configurable timeouts. (#…
- Loading branch information
1 parent
4322382
commit b97b8dc
Showing
6 changed files
with
658 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
pkgs/ok_http/example/integration_test/client_configuration_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:io'; | ||
|
||
import 'package:http/http.dart'; | ||
import 'package:integration_test/integration_test.dart'; | ||
import 'package:ok_http/ok_http.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void testTimeouts() { | ||
group('timeouts', () { | ||
group('call timeout', () { | ||
late HttpServer server; | ||
|
||
setUp(() async { | ||
server = (await HttpServer.bind('localhost', 0)) | ||
..listen((request) async { | ||
// Add a delay of `n` seconds for URI `http://localhost:port/n` | ||
final delay = int.parse(request.requestedUri.pathSegments.last); | ||
await Future<void>.delayed(Duration(seconds: delay)); | ||
|
||
await request.drain<void>(); | ||
await request.response.close(); | ||
}); | ||
}); | ||
tearDown(() { | ||
server.close(); | ||
}); | ||
|
||
test('exceeded', () { | ||
final client = OkHttpClient( | ||
configuration: const OkHttpClientConfiguration( | ||
callTimeout: Duration(milliseconds: 500), | ||
), | ||
); | ||
expect( | ||
() async { | ||
await client.get(Uri.parse('http://localhost:${server.port}/1')); | ||
}, | ||
throwsA( | ||
isA<ClientException>().having( | ||
(exception) => exception.message, | ||
'message', | ||
startsWith('java.io.InterruptedIOException'), | ||
), | ||
), | ||
); | ||
}); | ||
|
||
test('not exceeded', () async { | ||
final client = OkHttpClient( | ||
configuration: const OkHttpClientConfiguration( | ||
callTimeout: Duration(milliseconds: 1500), | ||
), | ||
); | ||
final response = await client.send( | ||
Request( | ||
'GET', | ||
Uri.http('localhost:${server.port}', '1'), | ||
), | ||
); | ||
|
||
expect(response.statusCode, 200); | ||
expect(response.contentLength, 0); | ||
}); | ||
|
||
test('not set', () async { | ||
final client = OkHttpClient(); | ||
|
||
expect( | ||
() async { | ||
await client.send( | ||
Request( | ||
'GET', | ||
Uri.http('localhost:${server.port}', '11'), | ||
), | ||
); | ||
}, | ||
throwsA( | ||
isA<ClientException>().having( | ||
(exception) => exception.message, | ||
'message', | ||
startsWith('java.net.SocketTimeoutException'), | ||
), | ||
), | ||
); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
void main() { | ||
IntegrationTestWidgetsFlutterBinding.ensureInitialized(); | ||
|
||
testTimeouts(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.