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

Side by Side Diff: pkg/http/test/mock_client_test.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_test;
6
7 import 'dart:io';
8 import 'dart:json';
9 import 'dart:uri';
10
11 import '../../unittest/unittest.dart';
Bob Nystrom 2012/11/05 20:36:04 Add a TODO here that this should be a 'package:' i
nweiz 2012/11/05 22:35:16 Done.
12 import '../lib/http.dart' as http;
13 import '../lib/src/utils.dart';
14 import 'utils.dart';
15
16 void main() {
17 test('handles a request', () {
18 var client = new http.MockClient();
19 client.handler = (request) {
20 return new Future.immediate(new http.Response(
21 JSON.stringify(request.bodyFields), 200,
22 headers: {'content-type': 'application/json'}));
23 };
24
25 expect(client.post("http://example.com/foo", fields: {
26 'field1': 'value1',
27 'field2': 'value2'
28 }).transform((response) => response.body), completion(parse(equals({
29 'field1': 'value1',
30 'field2': 'value2'
31 }))));
32 });
33
34 test('handles a streamed request', () {
35 var client = new http.MockClient();
36 client.streamHandler = (request, bodyStream) {
37 return consumeInputStream(bodyStream).transform((body) {
38 var stream = new ListInputStream();
39 async.then((_) {
40 var bodyString = new String.fromCharCodes(body);
41 stream.write('Request body was "$bodyString"'.charCodes);
42 stream.markEndOfStream();
43 });
44
45 return new http.StreamedResponse(stream, 200, -1);
46 });
47 };
48
49 var uri = new Uri.fromString("http://example.com/foo");
50 var request = new http.Request("POST", uri);
51 request.body = "hello, world";
52 var future = client.send(request)
53 .chain(http.Response.fromStream)
54 .transform((response) => response.body);
55 expect(future, completion(equals('Request body was "hello, world"')));
Bob Nystrom 2012/11/05 20:36:04 You've organized this differently from the expect(
nweiz 2012/11/05 22:35:16 Just aesthetics. Since the future includes a coupl
56 });
57 }
OLDNEW
« pkg/http/lib/src/mock_client.dart ('K') | « pkg/http/lib/src/mock_client.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698