| 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:io'; |
| 8 import 'dart:json'; |
| 9 import 'dart:uri'; |
| 10 |
| 11 // TODO(nweiz): make these "package:" imports. |
| 12 import '../../unittest/lib/unittest.dart'; |
| 13 import '../lib/http.dart' as http; |
| 14 import '../lib/testing.dart'; |
| 15 import '../lib/src/utils.dart'; |
| 16 import 'utils.dart'; |
| 17 |
| 18 void main() { |
| 19 test('handles a request', () { |
| 20 var client = new MockClient((request) { |
| 21 return new Future.immediate(new http.Response( |
| 22 JSON.stringify(request.bodyFields), 200, |
| 23 headers: {'content-type': 'application/json'})); |
| 24 }); |
| 25 |
| 26 expect(client.post("http://example.com/foo", fields: { |
| 27 'field1': 'value1', |
| 28 'field2': 'value2' |
| 29 }).transform((response) => response.body), completion(parse(equals({ |
| 30 'field1': 'value1', |
| 31 'field2': 'value2' |
| 32 })))); |
| 33 }); |
| 34 |
| 35 test('handles a streamed request', () { |
| 36 var client = new MockClient.streaming((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"'))); |
| 56 }); |
| 57 } |
| OLD | NEW |