OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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 import "dart:async"; |
| 6 import 'dart:convert'; |
| 7 import "dart:io"; |
| 8 |
| 9 import "package:expect/expect.dart"; |
| 10 |
| 11 void test(responseBytes, bodyLength) async { |
| 12 fullRequest(bytes) { |
| 13 var len = bytes.length; |
| 14 return len > 4 && |
| 15 bytes[len - 4] == 13 && |
| 16 bytes[len - 3] == 10 && |
| 17 bytes[len - 2] == 13 && |
| 18 bytes[len - 1] == 10; |
| 19 } |
| 20 |
| 21 handleSocket(socket) async { |
| 22 var bytes = []; |
| 23 await for (var data in socket) { |
| 24 bytes.addAll(data); |
| 25 if (fullRequest(bytes)) { |
| 26 socket.add(responseBytes); |
| 27 socket.close(); |
| 28 } |
| 29 } |
| 30 } |
| 31 |
| 32 var server = await ServerSocket.bind('127.0.0.1', 0); |
| 33 server.listen(handleSocket); |
| 34 |
| 35 var client = new HttpClient(); |
| 36 var request = |
| 37 await client.getUrl(Uri.parse('http://127.0.0.1:${server.port}/')); |
| 38 var response = await request.close(); |
| 39 Expect.equals(response.statusCode, 200); |
| 40 Expect.equals(bodyLength, |
| 41 (await response.fold([], (p, e) => p..addAll(e))).length); |
| 42 server.close(); |
| 43 } |
| 44 |
| 45 main() { |
| 46 var r1 = ''' |
| 47 HTTP/1.1 100 Continue\r |
| 48 \r |
| 49 HTTP/1.1 200 OK\r |
| 50 \r |
| 51 '''; |
| 52 |
| 53 var r2 = ''' |
| 54 HTTP/1.1 100 Continue\r |
| 55 My-Header-1: hello\r |
| 56 My-Header-2: world\r |
| 57 \r |
| 58 HTTP/1.1 200 OK\r |
| 59 \r |
| 60 '''; |
| 61 |
| 62 var r3 = ''' |
| 63 HTTP/1.1 100 Continue\r |
| 64 \r |
| 65 HTTP/1.1 200 OK\r |
| 66 Content-Length: 2\r |
| 67 \r |
| 68 AB'''; |
| 69 |
| 70 test(ASCII.encode(r1), 0); |
| 71 test(ASCII.encode(r2), 0); |
| 72 test(ASCII.encode(r3), 2); |
| 73 } |
OLD | NEW |