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

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

Issue 11364134: Merge libv1. (Closed) Base URL: https://dart.googlecode.com/svn/experimental/lib_v2/dart
Patch Set: Reupload due to error Created 8 years, 1 month 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/htmlescape/lib/htmlescape.dart ('k') | pkg/http/lib/testing.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library mock_client;
6
7 import 'dart:io';
8
9 import 'base_client.dart';
10 import 'base_request.dart';
11 import 'request.dart';
12 import 'response.dart';
13 import 'streamed_response.dart';
14 import 'utils.dart';
15
16 // TODO(nweiz): once Dart has some sort of Rack- or WSGI-like standard for
17 // server APIs, MockClient should conform to it.
18
19 /// 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 /// requests that are made through it so that you can mock a server without
22 /// having to send real HTTP requests.
23 class MockClient extends BaseClient {
24 /// The handler for receiving [StreamedRequest]s and sending
25 /// [StreamedResponse]s.
26 final MockClientStreamHandler _handler;
27
28 /// Creates a [MockClient] with a handler that receives [Request]s and sends
29 /// [Response]s.
30 MockClient(MockClientHandler fn)
31 : this.streaming((baseRequest, bodyStream) {
32 return consumeInputStream(bodyStream).chain((bodyBytes) {
33 var request = new Request(baseRequest.method, baseRequest.url);
34 request.persistentConnection = baseRequest.persistentConnection;
35 request.followRedirects = baseRequest.followRedirects;
36 request.maxRedirects = baseRequest.maxRedirects;
37 mapAddAll(request.headers, baseRequest.headers);
38 request.bodyBytes = bodyBytes;
39 request.finalize();
40
41 return fn(request);
42 }).transform((response) {
43 var stream = new ListInputStream();
44 stream.write(response.bodyBytes);
45 stream.markEndOfStream();
46
47 return new StreamedResponse(
48 stream,
49 response.statusCode,
50 response.contentLength,
51 headers: response.headers,
52 isRedirect: response.isRedirect,
53 persistentConnection: response.persistentConnection,
54 reasonPhrase: response.reasonPhrase);
55 });
56 });
57
58 /// Creates a [MockClient] with a handler that receives [StreamedRequest]s and
59 /// sends [StreamedResponse]s.
60 MockClient.streaming(MockClientStreamHandler this._handler);
61
62 /// Sends a request.
63 Future<StreamedResponse> send(BaseRequest request) {
64 var bodyStream = request.finalize();
65 return async.chain((_) => _handler(request, bodyStream));
66 }
67 }
68
69 /// A handler function that receives [StreamedRequest]s and sends
70 /// [StreamedResponse]s. Note that [request] will be finalized.
71 typedef Future<StreamedResponse> MockClientStreamHandler(
72 BaseRequest request, InputStream bodyStream);
73
74 /// A handler function that receives [Request]s and sends [Response]s. Note that
75 /// [request] will be finalized.
76 typedef Future<Response> MockClientHandler(Request request);
OLDNEW
« no previous file with comments | « pkg/htmlescape/lib/htmlescape.dart ('k') | pkg/http/lib/testing.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698