Skip to content

Commit

Permalink
pkgs/ok_http: #1215 review fixes
Browse files Browse the repository at this point in the history
fixed comments
run individual tests that pass

Co-authored-by: Brian Quinlan <[email protected]>
  • Loading branch information
Anikate-De and brianquinlan committed May 31, 2024
1 parent bae6f5c commit d005a72
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
16 changes: 8 additions & 8 deletions pkgs/ok_http/example/integration_test/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ void main() async {

Future<void> 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);
});
}
4 changes: 2 additions & 2 deletions pkgs/ok_http/lib/ok_http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
19 changes: 10 additions & 9 deletions pkgs/ok_http/lib/src/ok_http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -41,7 +42,6 @@ import 'third_party/okhttp3/_package.dart' as bindings;
/// }
/// }
/// ```
///
class OkHttpClient extends BaseClient {
late bindings.OkHttpClient _client;

Expand All @@ -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<StreamedResponse>();

// 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());
Expand Down Expand Up @@ -98,14 +96,17 @@ 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));
return;
}
}

// 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()),
Expand All @@ -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));
},
)));

Expand Down

0 comments on commit d005a72

Please sign in to comment.