Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkgs/ok_http: OkHttpClientConfiguration and configurable timeouts. #1289

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkgs/ok_http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.1.1-wip

- `OkHttpClient` now receives an `OkHttpClientConfiguration` to configure the client on a per-call basis.
- `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`.

## 0.1.0

- 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)
Expand Down
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();
}
1 change: 1 addition & 0 deletions pkgs/ok_http/jnigen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ classes:
- "com.example.ok_http.WebSocketListenerProxy"
- "okio.ByteString"
- "com.example.ok_http.WebSocketInterceptor"
- "java.util.concurrent.TimeUnit"

# Exclude the deprecated methods listed below
# They cause syntax errors during the `dart format` step of JNIGen.
Expand Down
Loading
Loading