Skip to content

Commit

Permalink
Support a user-supplied dart:io client in pkg/http.
Browse files Browse the repository at this point in the history
R=jmesserly@google.com
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
  • Loading branch information
nex3 committed May 19, 2014
1 parent f21f056 commit 6d63acf
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 4 deletions.
4 changes: 4 additions & 0 deletions pkg/http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.11.1

* Expose the `IOClient` class which wraps a `dart:io` `HttpClient`.

## 0.11.0+1

* Fix a bug in handling errors in decoding XMLHttpRequest responses for
Expand Down
1 change: 1 addition & 0 deletions pkg/http/lib/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export 'src/base_response.dart';
export 'src/byte_stream.dart';
export 'src/client.dart';
export 'src/exception.dart';
export 'src/io_client.dart';
export 'src/multipart_file.dart';
export 'src/multipart_request.dart';
export 'src/request.dart';
Expand Down
3 changes: 3 additions & 0 deletions pkg/http/lib/src/io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ newFile(String path) => _file.newInstance(const Symbol(''), [path]).reflectee;
/// Returns whether [error] is a `dart:io` HttpException.
bool isHttpException(error) => reflect(error).type.isSubtypeOf(_httpException);

/// Returns whether [client] is a `dart:io` HttpClient.
bool isHttpClient(client) => reflect(client).type.isSubtypeOf(_httpClient);

/// Tries to load `dart:io` and returns `null` if it fails.
LibraryMirror _getLibrary() {
try {
Expand Down
18 changes: 15 additions & 3 deletions pkg/http/lib/src/io_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,27 @@ import 'exception.dart';
import 'io.dart' as io;
import 'streamed_response.dart';

/// A `dart:io`-based HTTP client. This is the default client.
/// A `dart:io`-based HTTP client.
///
/// This is the default client when running on the command line.
class IOClient extends BaseClient {
/// The underlying `dart:io` HTTP client.
var _inner;

/// Creates a new HTTP client.
IOClient() {
///
/// [innerClient] must be a `dart:io` HTTP client. If it's not passed, a
/// default one will be instantiated.
IOClient([innerClient]) {
io.assertSupported("IOClient");
_inner = io.newHttpClient();
if (innerClient != null) {
// TODO(nweiz): remove this assert when we can type [innerClient]
// properly.
assert(io.isHttpClient(innerClient));
_inner = innerClient;
} else {
_inner = io.newHttpClient();
}
}

/// Sends an HTTP request and asynchronously returns the response.
Expand Down
2 changes: 1 addition & 1 deletion pkg/http/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: http
version: 0.11.0+1
version: 0.11.1
author: "Dart Team <misc@dartlang.org>"
homepage: https://pub.dartlang.org/packages/http
description: A composable, Future-based API for making HTTP requests.
Expand Down
35 changes: 35 additions & 0 deletions pkg/http/test/io/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,41 @@ void main() {
}), completes);
});

test('#send a StreamedRequest with a custom client', () {
expect(startServer().then((_) {
var ioClient = new HttpClient();
var client = new http.IOClient(ioClient);
var request = new http.StreamedRequest("POST", serverUrl);
request.headers[HttpHeaders.CONTENT_TYPE] =
'application/json; charset=utf-8';
request.headers[HttpHeaders.USER_AGENT] = 'Dart';

expect(client.send(request).then((response) {
expect(response.request, equals(request));
expect(response.statusCode, equals(200));
expect(response.headers['single'], equals('value'));
// dart:io internally normalizes outgoing headers so that they never
// have multiple headers with the same name, so there's no way to test
// whether we handle that case correctly.

return response.stream.bytesToString();
}).whenComplete(client.close), completion(parse(equals({
'method': 'POST',
'path': '/',
'headers': {
'content-type': ['application/json; charset=utf-8'],
'accept-encoding': ['gzip'],
'user-agent': ['Dart'],
'transfer-encoding': ['chunked']
},
'body': '{"hello": "world"}'
}))));

request.sink.add('{"hello": "world"}'.codeUnits);
request.sink.close();
}), completes);
});

test('#send with an invalid URL', () {
expect(startServer().then((_) {
var client = new http.Client();
Expand Down

0 comments on commit 6d63acf

Please sign in to comment.