Skip to content

Commit b97b8dc

Browse files
authored
pkgs/ok_http: OkHttpClientConfiguration and configurable timeouts. (#1289)
1 parent 4322382 commit b97b8dc

File tree

6 files changed

+658
-24
lines changed

6 files changed

+658
-24
lines changed

pkgs/ok_http/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.1.1-wip
2+
3+
- `OkHttpClient` now receives an `OkHttpClientConfiguration` to configure the client on a per-call basis.
4+
- `OkHttpClient` supports setting four types of timeouts: [`connectTimeout`](https://square.github.io/okhttp/5.x/okhttp/okhttp3/-ok-http-client/-builder/connect-timeout.html), [`readTimeout`](https://square.github.io/okhttp/5.x/okhttp/okhttp3/-ok-http-client/-builder/read-timeout.html), [`writeTimeout`](https://square.github.io/okhttp/5.x/okhttp/okhttp3/-ok-http-client/-builder/write-timeout.html), and [`callTimeout`](https://square.github.io/okhttp/5.x/okhttp/okhttp3/-ok-http-client/-builder/call-timeout.html), using the `OkHttpClientConfiguration`.
5+
16
## 0.1.0
27

38
- Implementation of [`BaseClient`](https://pub.dev/documentation/http/latest/http/BaseClient-class.html) and `send()` method using [`enqueue()` API](https://square.github.io/okhttp/5.x/okhttp/okhttp3/-call/enqueue.html)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
}

pkgs/ok_http/jnigen.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ classes:
3636
- "com.example.ok_http.WebSocketListenerProxy"
3737
- "okio.ByteString"
3838
- "com.example.ok_http.WebSocketInterceptor"
39+
- "java.util.concurrent.TimeUnit"
3940

4041
# Exclude the deprecated methods listed below
4142
# They cause syntax errors during the `dart format` step of JNIGen.

0 commit comments

Comments
 (0)