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