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:io'; | 8 import 'dart:io'; |
8 import 'dart:json'; | 9 import 'dart:json' as json; |
9 import 'dart:uri'; | 10 import 'dart:uri'; |
10 | 11 |
11 import 'package:unittest/unittest.dart'; | 12 import 'package:unittest/unittest.dart'; |
12 import 'package:http/http.dart' as http; | 13 import 'package:http/http.dart' as http; |
13 import 'package:http/testing.dart'; | 14 import 'package:http/testing.dart'; |
14 import 'package:http/src/utils.dart'; | 15 import 'package:http/src/utils.dart'; |
15 import 'utils.dart'; | 16 import 'utils.dart'; |
16 | 17 |
17 void main() { | 18 void main() { |
18 test('handles a request', () { | 19 test('handles a request', () { |
19 var client = new MockClient((request) { | 20 var client = new MockClient((request) { |
20 return new Future.immediate(new http.Response( | 21 return new Future.immediate(new http.Response( |
21 JSON.stringify(request.bodyFields), 200, | 22 json.stringify(request.bodyFields), 200, |
22 request: request, headers: {'content-type': 'application/json'})); | 23 request: request, headers: {'content-type': 'application/json'})); |
23 }); | 24 }); |
24 | 25 |
25 expect(client.post("http://example.com/foo", fields: { | 26 expect(client.post("http://example.com/foo", fields: { |
26 'field1': 'value1', | 27 'field1': 'value1', |
27 'field2': 'value2' | 28 'field2': 'value2' |
28 }).transform((response) => response.body), completion(parse(equals({ | 29 }).then((response) => response.body), completion(parse(equals({ |
29 'field1': 'value1', | 30 'field1': 'value1', |
30 'field2': 'value2' | 31 'field2': 'value2' |
31 })))); | 32 })))); |
32 }); | 33 }); |
33 | 34 |
34 test('handles a streamed request', () { | 35 test('handles a streamed request', () { |
35 var client = new MockClient.streaming((request, bodyStream) { | 36 var client = new MockClient.streaming((request, bodyStream) { |
36 return consumeInputStream(bodyStream).transform((body) { | 37 return consumeInputStream(bodyStream).then((body) { |
37 var stream = new ListInputStream(); | 38 var stream = new ListInputStream(); |
38 async.then((_) { | 39 async.then((_) { |
39 var bodyString = new String.fromCharCodes(body); | 40 var bodyString = new String.fromCharCodes(body); |
40 stream.write('Request body was "$bodyString"'.charCodes); | 41 stream.write('Request body was "$bodyString"'.charCodes); |
41 stream.markEndOfStream(); | 42 stream.markEndOfStream(); |
42 }); | 43 }); |
43 | 44 |
44 return new http.StreamedResponse(stream, 200, -1); | 45 return new http.StreamedResponse(stream, 200, -1); |
45 }); | 46 }); |
46 }); | 47 }); |
47 | 48 |
48 var uri = new Uri.fromString("http://example.com/foo"); | 49 var uri = new Uri.fromString("http://example.com/foo"); |
49 var request = new http.Request("POST", uri); | 50 var request = new http.Request("POST", uri); |
50 request.body = "hello, world"; | 51 request.body = "hello, world"; |
51 var future = client.send(request) | 52 var future = client.send(request) |
52 .chain(http.Response.fromStream) | 53 .then(http.Response.fromStream) |
53 .transform((response) => response.body); | 54 .then((response) => response.body); |
54 expect(future, completion(equals('Request body was "hello, world"'))); | 55 expect(future, completion(equals('Request body was "hello, world"'))); |
55 }); | 56 }); |
56 | 57 |
57 test('handles a request with no body', () { | 58 test('handles a request with no body', () { |
58 var client = new MockClient((request) { | 59 var client = new MockClient((request) { |
59 return new Future.immediate(new http.Response('you did it', 200)); | 60 return new Future.immediate(new http.Response('you did it', 200)); |
60 }); | 61 }); |
61 | 62 |
62 expect(client.read("http://example.com/foo"), | 63 expect(client.read("http://example.com/foo"), |
63 completion(equals('you did it'))); | 64 completion(equals('you did it'))); |
64 }); | 65 }); |
65 } | 66 } |
OLD | NEW |