| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013, 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 client_test; | |
| 6 | |
| 7 import 'dart:io'; | |
| 8 | |
| 9 import 'package:http/http.dart' as http; | |
| 10 import 'package:unittest/unittest.dart'; | |
| 11 | |
| 12 import 'utils.dart'; | |
| 13 | |
| 14 void main() { | |
| 15 tearDown(stopServer); | |
| 16 | |
| 17 test('#send a StreamedRequest', () { | |
| 18 expect(startServer().then((_) { | |
| 19 var client = new http.Client(); | |
| 20 var request = new http.StreamedRequest("POST", serverUrl); | |
| 21 request.headers[HttpHeaders.CONTENT_TYPE] = | |
| 22 'application/json; charset=utf-8'; | |
| 23 request.headers[HttpHeaders.USER_AGENT] = 'Dart'; | |
| 24 | |
| 25 expect(client.send(request).then((response) { | |
| 26 expect(response.request, equals(request)); | |
| 27 expect(response.statusCode, equals(200)); | |
| 28 expect(response.headers['single'], equals('value')); | |
| 29 // dart:io internally normalizes outgoing headers so that they never | |
| 30 // have multiple headers with the same name, so there's no way to test | |
| 31 // whether we handle that case correctly. | |
| 32 | |
| 33 return response.stream.bytesToString(); | |
| 34 }).whenComplete(client.close), completion(parse(equals({ | |
| 35 'method': 'POST', | |
| 36 'path': '/', | |
| 37 'headers': { | |
| 38 'content-type': ['application/json; charset=utf-8'], | |
| 39 'accept-encoding': ['gzip'], | |
| 40 'user-agent': ['Dart'], | |
| 41 'transfer-encoding': ['chunked'] | |
| 42 }, | |
| 43 'body': '{"hello": "world"}' | |
| 44 })))); | |
| 45 | |
| 46 request.sink.add('{"hello": "world"}'.codeUnits); | |
| 47 request.sink.close(); | |
| 48 }), completes); | |
| 49 }); | |
| 50 | |
| 51 test('#send with an invalid URL', () { | |
| 52 expect(startServer().then((_) { | |
| 53 var client = new http.Client(); | |
| 54 var url = Uri.parse('http://http.invalid'); | |
| 55 var request = new http.StreamedRequest("POST", url); | |
| 56 request.headers[HttpHeaders.CONTENT_TYPE] = | |
| 57 'application/json; charset=utf-8'; | |
| 58 | |
| 59 expect(client.send(request), throwsSocketException); | |
| 60 | |
| 61 request.sink.add('{"hello": "world"}'.codeUnits); | |
| 62 request.sink.close(); | |
| 63 }), completes); | |
| 64 }); | |
| 65 } | |
| OLD | NEW |