Skip to content

Commit 6d63acf

Browse files
committed
Support a user-supplied dart:io client in pkg/http.
[email protected] BUG=18871 Review URL: https://codereview.chromium.org//299483002 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@36319 260f80e4-7a28-3924-810f-c04153c831b5
1 parent f21f056 commit 6d63acf

File tree

6 files changed

+59
-4
lines changed

6 files changed

+59
-4
lines changed

pkg/http/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.11.1
2+
3+
* Expose the `IOClient` class which wraps a `dart:io` `HttpClient`.
4+
15
## 0.11.0+1
26

37
* Fix a bug in handling errors in decoding XMLHttpRequest responses for

pkg/http/lib/http.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export 'src/base_response.dart';
1818
export 'src/byte_stream.dart';
1919
export 'src/client.dart';
2020
export 'src/exception.dart';
21+
export 'src/io_client.dart';
2122
export 'src/multipart_file.dart';
2223
export 'src/multipart_request.dart';
2324
export 'src/request.dart';

pkg/http/lib/src/io.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ newFile(String path) => _file.newInstance(const Symbol(''), [path]).reflectee;
4343
/// Returns whether [error] is a `dart:io` HttpException.
4444
bool isHttpException(error) => reflect(error).type.isSubtypeOf(_httpException);
4545

46+
/// Returns whether [client] is a `dart:io` HttpClient.
47+
bool isHttpClient(client) => reflect(client).type.isSubtypeOf(_httpClient);
48+
4649
/// Tries to load `dart:io` and returns `null` if it fails.
4750
LibraryMirror _getLibrary() {
4851
try {

pkg/http/lib/src/io_client.dart

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,27 @@ import 'exception.dart';
1414
import 'io.dart' as io;
1515
import 'streamed_response.dart';
1616

17-
/// A `dart:io`-based HTTP client. This is the default client.
17+
/// A `dart:io`-based HTTP client.
18+
///
19+
/// This is the default client when running on the command line.
1820
class IOClient extends BaseClient {
1921
/// The underlying `dart:io` HTTP client.
2022
var _inner;
2123

2224
/// Creates a new HTTP client.
23-
IOClient() {
25+
///
26+
/// [innerClient] must be a `dart:io` HTTP client. If it's not passed, a
27+
/// default one will be instantiated.
28+
IOClient([innerClient]) {
2429
io.assertSupported("IOClient");
25-
_inner = io.newHttpClient();
30+
if (innerClient != null) {
31+
// TODO(nweiz): remove this assert when we can type [innerClient]
32+
// properly.
33+
assert(io.isHttpClient(innerClient));
34+
_inner = innerClient;
35+
} else {
36+
_inner = io.newHttpClient();
37+
}
2638
}
2739

2840
/// Sends an HTTP request and asynchronously returns the response.

pkg/http/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: http
2-
version: 0.11.0+1
2+
version: 0.11.1
33
author: "Dart Team <[email protected]>"
44
homepage: https://pub.dartlang.org/packages/http
55
description: A composable, Future-based API for making HTTP requests.

pkg/http/test/io/client_test.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,41 @@ void main() {
4848
}), completes);
4949
});
5050

51+
test('#send a StreamedRequest with a custom client', () {
52+
expect(startServer().then((_) {
53+
var ioClient = new HttpClient();
54+
var client = new http.IOClient(ioClient);
55+
var request = new http.StreamedRequest("POST", serverUrl);
56+
request.headers[HttpHeaders.CONTENT_TYPE] =
57+
'application/json; charset=utf-8';
58+
request.headers[HttpHeaders.USER_AGENT] = 'Dart';
59+
60+
expect(client.send(request).then((response) {
61+
expect(response.request, equals(request));
62+
expect(response.statusCode, equals(200));
63+
expect(response.headers['single'], equals('value'));
64+
// dart:io internally normalizes outgoing headers so that they never
65+
// have multiple headers with the same name, so there's no way to test
66+
// whether we handle that case correctly.
67+
68+
return response.stream.bytesToString();
69+
}).whenComplete(client.close), completion(parse(equals({
70+
'method': 'POST',
71+
'path': '/',
72+
'headers': {
73+
'content-type': ['application/json; charset=utf-8'],
74+
'accept-encoding': ['gzip'],
75+
'user-agent': ['Dart'],
76+
'transfer-encoding': ['chunked']
77+
},
78+
'body': '{"hello": "world"}'
79+
}))));
80+
81+
request.sink.add('{"hello": "world"}'.codeUnits);
82+
request.sink.close();
83+
}), completes);
84+
});
85+
5186
test('#send with an invalid URL', () {
5287
expect(startServer().then((_) {
5388
var client = new http.Client();

0 commit comments

Comments
 (0)