Skip to content

Commit 047d6ed

Browse files
authored
Fixes a bug where requests made in different Clients would fail (#827)
1 parent 9dbbafb commit 047d6ed

File tree

14 files changed

+139
-25
lines changed

14 files changed

+139
-25
lines changed

pkgs/cronet_http/example/integration_test/client_conformance_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ void main() {
1717
testRequestHeaders(client);
1818
testResponseHeaders(client);
1919
testRedirect(client);
20+
testCompressedResponseBody(client);
21+
testMultipleClients(CronetClient.new);
2022

2123
// TODO: Use `testAll` when `testServerErrors` passes i.e.
2224
// testAll(CronetClient(), canStreamRequestBody: false);

pkgs/cupertino_http/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.0.10
2+
3+
* Fix [Use of multiple CupertinoClients can result in cancelled requests](https://github.com/dart-lang/http/issues/826)
4+
15
## 0.0.9
26

37
* Add a more complete implementation for `URLSessionTask`:

pkgs/cupertino_http/example/integration_test/client_conformance_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ void main() {
1212
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
1313

1414
group('defaultSessionConfiguration', () {
15-
testAll(CupertinoClient.defaultSessionConfiguration(),
15+
testAll(CupertinoClient.defaultSessionConfiguration,
1616
canStreamRequestBody: false);
1717
});
1818
group('fromSessionConfiguration', () {
1919
final config = URLSessionConfiguration.ephemeralSessionConfiguration();
20-
testAll(CupertinoClient.fromSessionConfiguration(config),
20+
testAll(() => CupertinoClient.fromSessionConfiguration(config),
2121
canStreamRequestBody: false);
2222
});
2323
}

pkgs/cupertino_http/lib/cupertino_client.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class _TaskTracker {
5050
/// }
5151
/// ```
5252
class CupertinoClient extends BaseClient {
53-
static final Map<int, _TaskTracker> _tasks = {};
53+
static final Map<URLSessionTask, _TaskTracker> _tasks = {};
5454

5555
URLSession _urlSession;
5656

@@ -143,8 +143,7 @@ class CupertinoClient extends BaseClient {
143143
}
144144
}
145145

146-
static _TaskTracker _tracker(URLSessionTask task) =>
147-
_tasks[task.taskIdentifier]!;
146+
static _TaskTracker _tracker(URLSessionTask task) => _tasks[task]!;
148147

149148
static void _onComplete(
150149
URLSession session, URLSessionTask task, Error? error) {
@@ -163,7 +162,7 @@ class CupertinoClient extends BaseClient {
163162
StateError('task completed without an error or response'));
164163
}
165164
taskTracker.close();
166-
_tasks.remove(task.taskIdentifier);
165+
_tasks.remove(task);
167166
}
168167

169168
static void _onData(URLSession session, URLSessionTask task, Data data) {
@@ -234,7 +233,7 @@ class CupertinoClient extends BaseClient {
234233

235234
final task = _urlSession.dataTaskWithRequest(urlRequest);
236235
final taskTracker = _TaskTracker(request);
237-
_tasks[task.taskIdentifier] = taskTracker;
236+
_tasks[task] = taskTracker;
238237
task.resume();
239238

240239
final maxRedirects = request.followRedirects ? request.maxRedirects : 0;

pkgs/cupertino_http/lib/cupertino_http.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ abstract class _ObjectHolder<T extends ncb.NSObject> {
3939
final T _nsObject;
4040

4141
_ObjectHolder(this._nsObject);
42+
43+
@override
44+
bool operator ==(Object other) {
45+
if (other is _ObjectHolder) {
46+
return _nsObject == other._nsObject;
47+
}
48+
return false;
49+
}
50+
51+
@override
52+
int get hashCode => _nsObject.hashCode;
4253
}
4354

4455
/// Settings for controlling whether cookies will be accepted.

pkgs/cupertino_http/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: cupertino_http
22
description: >
33
A macOS/iOS Flutter plugin that provides access to the Foundation URL
44
Loading System.
5-
version: 0.0.9
5+
version: 0.0.10
66
repository: https://github.com/dart-lang/http/tree/master/pkgs/cupertino_http
77

88
environment:

pkgs/http/test/html/client_conformance_test.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import 'package:http_client_conformance_tests/http_client_conformance_tests.dart
99
import 'package:test/test.dart';
1010

1111
void main() {
12-
final client = BrowserClient();
13-
14-
testAll(client,
12+
testAll(() => BrowserClient(),
1513
redirectAlwaysAllowed: true,
1614
canStreamRequestBody: false,
1715
canStreamResponseBody: false);

pkgs/http/test/io/client_conformance_test.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,5 @@ import 'package:http_client_conformance_tests/http_client_conformance_tests.dart
99
import 'package:test/test.dart';
1010

1111
void main() {
12-
final client = IOClient();
13-
14-
testAll(client);
12+
testAll(() => IOClient());
1513
}

pkgs/http_client_conformance_tests/example/client_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ class MyHttpClient extends BaseClient {
1212

1313
void main() {
1414
group('client conformance tests', () {
15-
testAll(MyHttpClient());
15+
testAll(() => MyHttpClient());
1616
});
1717
}

pkgs/http_client_conformance_tests/lib/http_client_conformance_tests.dart

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'package:http/http.dart';
66

77
import 'src/compressed_response_body_tests.dart';
8+
import 'src/multiple_clients_tests.dart';
89
import 'src/redirect_tests.dart';
910
import 'src/request_body_streamed_tests.dart';
1011
import 'src/request_body_tests.dart';
@@ -16,6 +17,7 @@ import 'src/server_errors_test.dart';
1617

1718
export 'src/compressed_response_body_tests.dart'
1819
show testCompressedResponseBody;
20+
export 'src/multiple_clients_tests.dart' show testMultipleClients;
1921
export 'src/redirect_tests.dart' show testRedirect;
2022
export 'src/request_body_streamed_tests.dart' show testRequestBodyStreamed;
2123
export 'src/request_body_tests.dart' show testRequestBody;
@@ -41,18 +43,21 @@ export 'src/server_errors_test.dart' show testServerErrors;
4143
/// The tests are run against a series of HTTP servers that are started by the
4244
/// tests. If the tests are run in the browser, then the test servers are
4345
/// started in another process. Otherwise, the test servers are run in-process.
44-
void testAll(Client client,
46+
void testAll(Client Function() clientFactory,
4547
{bool canStreamRequestBody = true,
4648
bool canStreamResponseBody = true,
4749
bool redirectAlwaysAllowed = false}) {
48-
testRequestBody(client);
49-
testRequestBodyStreamed(client, canStreamRequestBody: canStreamRequestBody);
50-
testResponseBody(client, canStreamResponseBody: canStreamResponseBody);
51-
testResponseBodyStreamed(client,
50+
testRequestBody(clientFactory());
51+
testRequestBodyStreamed(clientFactory(),
52+
canStreamRequestBody: canStreamRequestBody);
53+
testResponseBody(clientFactory(),
5254
canStreamResponseBody: canStreamResponseBody);
53-
testRequestHeaders(client);
54-
testResponseHeaders(client);
55-
testRedirect(client, redirectAlwaysAllowed: redirectAlwaysAllowed);
56-
testServerErrors(client);
57-
testCompressedResponseBody(client);
55+
testResponseBodyStreamed(clientFactory(),
56+
canStreamResponseBody: canStreamResponseBody);
57+
testRequestHeaders(clientFactory());
58+
testResponseHeaders(clientFactory());
59+
testRedirect(clientFactory(), redirectAlwaysAllowed: redirectAlwaysAllowed);
60+
testServerErrors(clientFactory());
61+
testCompressedResponseBody(clientFactory());
62+
testMultipleClients(clientFactory);
5863
}

0 commit comments

Comments
 (0)