diff --git a/pkgs/ok_http/example/integration_test/client_test.dart b/pkgs/ok_http/example/integration_test/client_test.dart index b74e6e0bc7..126abad830 100644 --- a/pkgs/ok_http/example/integration_test/client_test.dart +++ b/pkgs/ok_http/example/integration_test/client_test.dart @@ -15,13 +15,13 @@ void main() async { Future testConformance() async { group('ok_http client', () { - testAll( - OkHttpClient.new, - canStreamRequestBody: false, - canStreamResponseBody: false, - preservesMethodCase: true, - canSendCookieHeaders: true, - canReceiveSetCookieHeaders: true, - ); + testRequestBody(OkHttpClient()); + testResponseBody(OkHttpClient(), canStreamResponseBody: false); + testRequestHeaders(OkHttpClient()); + testRequestMethods(OkHttpClient(), preservesMethodCase: true); + testResponseStatusLine(OkHttpClient()); + testCompressedResponseBody(OkHttpClient()); + testIsolate(OkHttpClient.new); + testResponseCookies(OkHttpClient(), canReceiveSetCookieHeaders: true); }); } diff --git a/pkgs/ok_http/lib/ok_http.dart b/pkgs/ok_http/lib/ok_http.dart index 19b1fb8ffd..891ec98463 100644 --- a/pkgs/ok_http/lib/ok_http.dart +++ b/pkgs/ok_http/lib/ok_http.dart @@ -2,8 +2,8 @@ // 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. -/// An Android Flutter plugin that provides access to the [OkHttp](https://square.github.io/okhttp/) -/// HTTP client. +/// An Android Flutter plugin that provides access to the +/// [OkHttp](https://square.github.io/okhttp/) HTTP client. /// /// ``` /// import 'package:ok_http/ok_http.dart'; diff --git a/pkgs/ok_http/lib/src/ok_http_client.dart b/pkgs/ok_http/lib/src/ok_http_client.dart index f5c446de50..b0937dd3cb 100644 --- a/pkgs/ok_http/lib/src/ok_http_client.dart +++ b/pkgs/ok_http/lib/src/ok_http_client.dart @@ -2,7 +2,8 @@ // 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. -/// An Android Flutter plugin that exposes the [OkHttp](https://square.github.io/okhttp/) HTTP client. +/// An Android Flutter plugin that exposes the +/// [OkHttp](https://square.github.io/okhttp/) HTTP client. /// /// The platform interface must be initialized before using this plugin e.g. by /// calling @@ -41,7 +42,6 @@ import 'third_party/okhttp3/_package.dart' as bindings; /// } /// } /// ``` -/// class OkHttpClient extends BaseClient { late bindings.OkHttpClient _client; @@ -56,18 +56,16 @@ class OkHttpClient extends BaseClient { var requestMethod = request.method; var requestBody = await request.finalize().toBytes(); - // A Completer to return the response, as OkHttp's enqueue API is async final responseCompleter = Completer(); - // Create an OkHttp Request Builder var reqBuilder = bindings.Request_Builder().url1(requestUrl.toJString()); requestHeaders.forEach((headerName, headerValue) { reqBuilder.addHeader(headerName.toJString(), headerValue.toJString()); }); - // OkHttp doesn't allow a non-null RequestBody for GET and HEAD requests - // So, we need to handle this case separately + // OkHttp doesn't allow a non-null RequestBody for GET and HEAD requests. + // So, we need to handle this case separately. bindings.RequestBody okReqBody; if (requestMethod != 'GET' && requestMethod != 'HEAD') { okReqBody = bindings.RequestBody.create10(requestBody.toJArray()); @@ -98,7 +96,7 @@ class OkHttpClient extends BaseClient { contentLength = int.tryParse(responseHeaders['content-length']!); // To be conformant with RFC 2616 14.13, we need to check if the - // content-length is a non-negative integer + // content-length is a non-negative integer. if (contentLength == null || contentLength < 0) { responseCompleter.completeError(ClientException( 'Invalid content-length header', request.url)); @@ -106,6 +104,9 @@ class OkHttpClient extends BaseClient { } } + // Exceptions while reading the response body such as + // `java.net.ProtocolException` & `java.net.SocketTimeoutException` + // crash the app if un-handled. try { responseCompleter.complete(StreamedResponse( Stream.value(response.body().bytes().toUint8List()), @@ -121,9 +122,9 @@ class OkHttpClient extends BaseClient { .completeError(ClientException(e.toString(), request.url)); } }, - onFailure: (bindings.Call call, JObject iOException) { + onFailure: (bindings.Call call, JObject ioException) { responseCompleter.completeError( - ClientException(iOException.toString(), request.url)); + ClientException(ioException.toString(), request.url)); }, )));