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

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

Issue 11783009: Big merge from experimental to bleeding edge. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: 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:io'; 8 import 'dart:io';
8 9
9 import 'base_client.dart'; 10 import 'base_client.dart';
10 import 'base_request.dart'; 11 import 'base_request.dart';
11 import 'request.dart'; 12 import 'request.dart';
12 import 'response.dart'; 13 import 'response.dart';
13 import 'streamed_response.dart'; 14 import 'streamed_response.dart';
14 import 'utils.dart'; 15 import 'utils.dart';
15 16
16 // TODO(nweiz): once Dart has some sort of Rack- or WSGI-like standard for 17 // TODO(nweiz): once Dart has some sort of Rack- or WSGI-like standard for
17 // server APIs, MockClient should conform to it. 18 // server APIs, MockClient should conform to it.
18 19
19 /// A mock HTTP client designed for use when testing code that uses 20 /// A mock HTTP client designed for use when testing code that uses
20 /// [BaseClient]. This client allows you to define a handler callback for all 21 /// [BaseClient]. This client allows you to define a handler callback for all
21 /// requests that are made through it so that you can mock a server without 22 /// requests that are made through it so that you can mock a server without
22 /// having to send real HTTP requests. 23 /// having to send real HTTP requests.
23 class MockClient extends BaseClient { 24 class MockClient extends BaseClient {
24 /// The handler for receiving [StreamedRequest]s and sending 25 /// The handler for receiving [StreamedRequest]s and sending
25 /// [StreamedResponse]s. 26 /// [StreamedResponse]s.
26 final MockClientStreamHandler _handler; 27 final MockClientStreamHandler _handler;
27 28
28 MockClient._(this._handler); 29 MockClient._(this._handler);
29 30
30 /// Creates a [MockClient] with a handler that receives [Request]s and sends 31 /// Creates a [MockClient] with a handler that receives [Request]s and sends
31 /// [Response]s. 32 /// [Response]s.
32 MockClient(MockClientHandler fn) 33 MockClient(MockClientHandler fn)
33 : this._((baseRequest, bodyStream) { 34 : this._((baseRequest, bodyStream) {
34 return consumeInputStream(bodyStream).chain((bodyBytes) { 35 return consumeInputStream(bodyStream).then((bodyBytes) {
35 var request = new Request(baseRequest.method, baseRequest.url); 36 var request = new Request(baseRequest.method, baseRequest.url);
36 request.persistentConnection = baseRequest.persistentConnection; 37 request.persistentConnection = baseRequest.persistentConnection;
37 request.followRedirects = baseRequest.followRedirects; 38 request.followRedirects = baseRequest.followRedirects;
38 request.maxRedirects = baseRequest.maxRedirects; 39 request.maxRedirects = baseRequest.maxRedirects;
39 mapAddAll(request.headers, baseRequest.headers); 40 mapAddAll(request.headers, baseRequest.headers);
40 request.bodyBytes = bodyBytes; 41 request.bodyBytes = bodyBytes;
41 request.finalize(); 42 request.finalize();
42 43
43 return fn(request); 44 return fn(request);
44 }).transform((response) { 45 }).then((response) {
45 var stream = new ListInputStream(); 46 var stream = new ListInputStream();
46 stream.write(response.bodyBytes); 47 stream.write(response.bodyBytes);
47 stream.markEndOfStream(); 48 stream.markEndOfStream();
48 49
49 return new StreamedResponse( 50 return new StreamedResponse(
50 stream, 51 stream,
51 response.statusCode, 52 response.statusCode,
52 response.contentLength, 53 response.contentLength,
53 request: baseRequest, 54 request: baseRequest,
54 headers: response.headers, 55 headers: response.headers,
55 isRedirect: response.isRedirect, 56 isRedirect: response.isRedirect,
56 persistentConnection: response.persistentConnection, 57 persistentConnection: response.persistentConnection,
57 reasonPhrase: response.reasonPhrase); 58 reasonPhrase: response.reasonPhrase);
58 }); 59 });
59 }); 60 });
60 61
61 /// Creates a [MockClient] with a handler that receives [StreamedRequest]s and 62 /// Creates a [MockClient] with a handler that receives [StreamedRequest]s and
62 /// sends [StreamedResponse]s. 63 /// sends [StreamedResponse]s.
63 MockClient.streaming(MockClientStreamHandler fn) 64 MockClient.streaming(MockClientStreamHandler fn)
64 : this._((request, bodyStream) { 65 : this._((request, bodyStream) {
65 return fn(request, bodyStream).transform((response) { 66 return fn(request, bodyStream).then((response) {
66 return new StreamedResponse( 67 return new StreamedResponse(
67 response.stream, 68 response.stream,
68 response.statusCode, 69 response.statusCode,
69 response.contentLength, 70 response.contentLength,
70 request: request, 71 request: request,
71 headers: response.headers, 72 headers: response.headers,
72 isRedirect: response.isRedirect, 73 isRedirect: response.isRedirect,
73 persistentConnection: response.persistentConnection, 74 persistentConnection: response.persistentConnection,
74 reasonPhrase: response.reasonPhrase); 75 reasonPhrase: response.reasonPhrase);
75 }); 76 });
76 }); 77 });
77 78
78 /// Sends a request. 79 /// Sends a request.
79 Future<StreamedResponse> send(BaseRequest request) { 80 Future<StreamedResponse> send(BaseRequest request) {
80 var bodyStream = request.finalize(); 81 var bodyStream = request.finalize();
81 return async.chain((_) => _handler(request, bodyStream)); 82 return async.then((_) => _handler(request, bodyStream));
82 } 83 }
83 } 84 }
84 85
85 /// A handler function that receives [StreamedRequest]s and sends 86 /// A handler function that receives [StreamedRequest]s and sends
86 /// [StreamedResponse]s. Note that [request] will be finalized. 87 /// [StreamedResponse]s. Note that [request] will be finalized.
87 typedef Future<StreamedResponse> MockClientStreamHandler( 88 typedef Future<StreamedResponse> MockClientStreamHandler(
88 BaseRequest request, InputStream bodyStream); 89 BaseRequest request, InputStream bodyStream);
89 90
90 /// A handler function that receives [Request]s and sends [Response]s. Note that 91 /// A handler function that receives [Request]s and sends [Response]s. Note that
91 /// [request] will be finalized. 92 /// [request] will be finalized.
92 typedef Future<Response> MockClientHandler(Request request); 93 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