OLD | NEW |
| (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:async'; | |
8 import 'dart:convert'; | |
9 | |
10 import 'package:http/http.dart' as http; | |
11 import 'package:http/src/utils.dart'; | |
12 import 'package:http/testing.dart'; | |
13 import 'package:unittest/unittest.dart'; | |
14 | |
15 import 'utils.dart'; | |
16 | |
17 void main() { | |
18 test('handles a request', () { | |
19 var client = new MockClient((request) { | |
20 return new Future.value(new http.Response( | |
21 JSON.encode(request.bodyFields), 200, | |
22 request: request, headers: {'content-type': 'application/json'})); | |
23 }); | |
24 | |
25 expect(client.post("http://example.com/foo", body: { | |
26 'field1': 'value1', | |
27 'field2': 'value2' | |
28 }).then((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 MockClient.streaming((request, bodyStream) { | |
36 return bodyStream.bytesToString().then((bodyString) { | |
37 var controller = new StreamController<List<int>>(sync: true); | |
38 async.then((_) { | |
39 controller.add('Request body was "$bodyString"'.codeUnits); | |
40 controller.close(); | |
41 }); | |
42 | |
43 return new http.StreamedResponse(controller.stream, 200); | |
44 }); | |
45 }); | |
46 | |
47 var uri = Uri.parse("http://example.com/foo"); | |
48 var request = new http.Request("POST", uri); | |
49 request.body = "hello, world"; | |
50 var future = client.send(request) | |
51 .then(http.Response.fromStream) | |
52 .then((response) => response.body); | |
53 expect(future, completion(equals('Request body was "hello, world"'))); | |
54 }); | |
55 | |
56 test('handles a request with no body', () { | |
57 var client = new MockClient((request) { | |
58 return new Future.value(new http.Response('you did it', 200)); | |
59 }); | |
60 | |
61 expect(client.read("http://example.com/foo"), | |
62 completion(equals('you did it'))); | |
63 }); | |
64 } | |
OLD | NEW |