Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(174)

Side by Side Diff: pkg/http/lib/src/mock_client.dart

Issue 11825010: Update pkg/http to use the new async APIs. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/http/lib/src/io_client.dart ('k') | pkg/http/lib/src/multipart_file.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library mock_client; 5 library mock_client;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:io'; 8 import 'dart:io';
9 9
10 import 'base_client.dart'; 10 import 'base_client.dart';
11 import 'base_request.dart'; 11 import 'base_request.dart';
12 import 'byte_stream.dart';
12 import 'request.dart'; 13 import 'request.dart';
13 import 'response.dart'; 14 import 'response.dart';
14 import 'streamed_response.dart'; 15 import 'streamed_response.dart';
15 import 'utils.dart'; 16 import 'utils.dart';
16 17
17 // TODO(nweiz): once Dart has some sort of Rack- or WSGI-like standard for 18 // TODO(nweiz): once Dart has some sort of Rack- or WSGI-like standard for
18 // server APIs, MockClient should conform to it. 19 // server APIs, MockClient should conform to it.
19 20
20 /// A mock HTTP client designed for use when testing code that uses 21 /// A mock HTTP client designed for use when testing code that uses
21 /// [BaseClient]. This client allows you to define a handler callback for all 22 /// [BaseClient]. This client allows you to define a handler callback for all
22 /// requests that are made through it so that you can mock a server without 23 /// requests that are made through it so that you can mock a server without
23 /// having to send real HTTP requests. 24 /// having to send real HTTP requests.
24 class MockClient extends BaseClient { 25 class MockClient extends BaseClient {
25 /// The handler for receiving [StreamedRequest]s and sending 26 /// The handler for receiving [StreamedRequest]s and sending
26 /// [StreamedResponse]s. 27 /// [StreamedResponse]s.
27 final MockClientStreamHandler _handler; 28 final MockClientStreamHandler _handler;
28 29
29 MockClient._(this._handler); 30 MockClient._(this._handler);
30 31
31 /// Creates a [MockClient] with a handler that receives [Request]s and sends 32 /// Creates a [MockClient] with a handler that receives [Request]s and sends
32 /// [Response]s. 33 /// [Response]s.
33 MockClient(MockClientHandler fn) 34 MockClient(MockClientHandler fn)
34 : this._((baseRequest, bodyStream) { 35 : this._((baseRequest, bodyStream) {
35 return consumeInputStream(bodyStream).then((bodyBytes) { 36 return bodyStream.toBytes().then((bodyBytes) {
36 var request = new Request(baseRequest.method, baseRequest.url); 37 var request = new Request(baseRequest.method, baseRequest.url);
37 request.persistentConnection = baseRequest.persistentConnection; 38 request.persistentConnection = baseRequest.persistentConnection;
38 request.followRedirects = baseRequest.followRedirects; 39 request.followRedirects = baseRequest.followRedirects;
39 request.maxRedirects = baseRequest.maxRedirects; 40 request.maxRedirects = baseRequest.maxRedirects;
40 mapAddAll(request.headers, baseRequest.headers); 41 mapAddAll(request.headers, baseRequest.headers);
41 request.bodyBytes = bodyBytes; 42 request.bodyBytes = bodyBytes;
42 request.finalize(); 43 request.finalize();
43 44
44 return fn(request); 45 return fn(request);
45 }).then((response) { 46 }).then((response) {
46 var stream = new ListInputStream();
47 stream.write(response.bodyBytes);
48 stream.markEndOfStream();
49
50 return new StreamedResponse( 47 return new StreamedResponse(
51 stream, 48 new ByteStream.fromBytes(response.bodyBytes),
52 response.statusCode, 49 response.statusCode,
53 response.contentLength, 50 response.contentLength,
54 request: baseRequest, 51 request: baseRequest,
55 headers: response.headers, 52 headers: response.headers,
56 isRedirect: response.isRedirect, 53 isRedirect: response.isRedirect,
57 persistentConnection: response.persistentConnection, 54 persistentConnection: response.persistentConnection,
58 reasonPhrase: response.reasonPhrase); 55 reasonPhrase: response.reasonPhrase);
59 }); 56 });
60 }); 57 });
61 58
(...skipping 17 matching lines...) Expand all
79 /// Sends a request. 76 /// Sends a request.
80 Future<StreamedResponse> send(BaseRequest request) { 77 Future<StreamedResponse> send(BaseRequest request) {
81 var bodyStream = request.finalize(); 78 var bodyStream = request.finalize();
82 return async.then((_) => _handler(request, bodyStream)); 79 return async.then((_) => _handler(request, bodyStream));
83 } 80 }
84 } 81 }
85 82
86 /// A handler function that receives [StreamedRequest]s and sends 83 /// A handler function that receives [StreamedRequest]s and sends
87 /// [StreamedResponse]s. Note that [request] will be finalized. 84 /// [StreamedResponse]s. Note that [request] will be finalized.
88 typedef Future<StreamedResponse> MockClientStreamHandler( 85 typedef Future<StreamedResponse> MockClientStreamHandler(
89 BaseRequest request, InputStream bodyStream); 86 BaseRequest request, ByteStream bodyStream);
90 87
91 /// A handler function that receives [Request]s and sends [Response]s. Note that 88 /// A handler function that receives [Request]s and sends [Response]s. Note that
92 /// [request] will be finalized. 89 /// [request] will be finalized.
93 typedef Future<Response> MockClientHandler(Request request); 90 typedef Future<Response> MockClientHandler(Request request);
OLDNEW
« no previous file with comments | « pkg/http/lib/src/io_client.dart ('k') | pkg/http/lib/src/multipart_file.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698