Skip to content

Commit

Permalink
pkgs/ok_http: OkHttpClientConfiguration and configurable timeouts. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Anikate-De committed Aug 16, 2024
1 parent 4322382 commit b97b8dc
Show file tree
Hide file tree
Showing 6 changed files with 658 additions and 24 deletions.
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

0 comments on commit b97b8dc

Please sign in to comment.