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

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

Issue 11362097: Add a mock HTTP client class. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: 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
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. Setting this will override [handler].
26 set streamHandler(MockClientStreamHandler fn) => _handler = fn;
Bob Nystrom 2012/11/05 20:36:04 Setters that don't have getters are a design smell
nweiz 2012/11/05 22:35:16 Made them constructor parameters.
27 MockClientStreamHandler _handler;
28
29 /// The handler for receiving [Request]s and sending [Response]s. Setting this
30 /// will override [streamHandler].
31 set handler(MockClientHandler fn) {
32 _handler = (baseRequest, bodyStream) {
33 return consumeInputStream(bodyStream).chain((bodyBytes) {
34 var request = new Request(baseRequest.method, baseRequest.url);
35 request.persistentConnection = baseRequest.persistentConnection;
36 request.followRedirects = baseRequest.followRedirects;
37 request.maxRedirects = baseRequest.maxRedirects;
38 mapAddAll(request.headers, baseRequest.headers);
39 request.bodyBytes = bodyBytes;
40 request.finalize();
41
42 return fn(request);
43 }).transform((response) {
44 var stream = new ListInputStream();
45 stream.write(response.bodyBytes);
46 stream.markEndOfStream();
47
48 return new StreamedResponse(
49 stream,
50 response.statusCode,
51 response.contentLength,
52 headers: response.headers,
53 isRedirect: response.isRedirect,
54 persistentConnection: response.persistentConnection,
55 reasonPhrase: response.reasonPhrase);
56 });
57 };
58 }
59
60 /// Sends a request to be handled by [handler].
61 Future<StreamedResponse> send(BaseRequest request) {
62 var bodyStream = request.finalize();
63 var redirectCount = 0;
Bob Nystrom 2012/11/05 20:36:04 Remove this?
nweiz 2012/11/05 22:35:16 Done.
64 return async.chain((_) => _handler(request, bodyStream));
65 }
66 }
67
68 /// A handler function that receives [StreamedRequest]s and sends
69 /// [StreamedResponse]s. Note that [request] will be finalized.
70 typedef Future<StreamedResponse> MockClientStreamHandler(
71 BaseRequest request, InputStream bodyStream);
72
73 /// A handler function that receives [Request]s and sends [Response]s. Note that
74 /// [request] will be finalized.
75 typedef Future<Response> MockClientHandler(Request request);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698