OLD | NEW |
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_test; | 5 library mock_client_test; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 | 9 |
10 import 'package:http/http.dart' as http; | 10 import 'package:http/http.dart' as http; |
(...skipping 22 matching lines...) Expand all Loading... |
33 | 33 |
34 test('handles a streamed request', () { | 34 test('handles a streamed request', () { |
35 var client = new MockClient.streaming((request, bodyStream) { | 35 var client = new MockClient.streaming((request, bodyStream) { |
36 return bodyStream.bytesToString().then((bodyString) { | 36 return bodyStream.bytesToString().then((bodyString) { |
37 var controller = new StreamController<List<int>>(sync: true); | 37 var controller = new StreamController<List<int>>(sync: true); |
38 async.then((_) { | 38 async.then((_) { |
39 controller.add('Request body was "$bodyString"'.codeUnits); | 39 controller.add('Request body was "$bodyString"'.codeUnits); |
40 controller.close(); | 40 controller.close(); |
41 }); | 41 }); |
42 | 42 |
43 return new http.StreamedResponse(controller.stream, 200, -1); | 43 return new http.StreamedResponse(controller.stream, 200); |
44 }); | 44 }); |
45 }); | 45 }); |
46 | 46 |
47 var uri = Uri.parse("http://example.com/foo"); | 47 var uri = Uri.parse("http://example.com/foo"); |
48 var request = new http.Request("POST", uri); | 48 var request = new http.Request("POST", uri); |
49 request.body = "hello, world"; | 49 request.body = "hello, world"; |
50 var future = client.send(request) | 50 var future = client.send(request) |
51 .then(http.Response.fromStream) | 51 .then(http.Response.fromStream) |
52 .then((response) => response.body); | 52 .then((response) => response.body); |
53 expect(future, completion(equals('Request body was "hello, world"'))); | 53 expect(future, completion(equals('Request body was "hello, world"'))); |
54 }); | 54 }); |
55 | 55 |
56 test('handles a request with no body', () { | 56 test('handles a request with no body', () { |
57 var client = new MockClient((request) { | 57 var client = new MockClient((request) { |
58 return new Future.value(new http.Response('you did it', 200)); | 58 return new Future.value(new http.Response('you did it', 200)); |
59 }); | 59 }); |
60 | 60 |
61 expect(client.read("http://example.com/foo"), | 61 expect(client.read("http://example.com/foo"), |
62 completion(equals('you did it'))); | 62 completion(equals('you did it'))); |
63 }); | 63 }); |
64 } | 64 } |
OLD | NEW |