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 import 'dart:async'; | |
6 import 'dart:io'; | |
7 import 'dart:uri'; | |
8 | |
9 void main() { | |
10 var client = new HttpClient(); | |
11 var server = new HttpServer(); | |
12 | |
13 var count = 0; | |
14 server.defaultRequestHandler = (HttpRequest request, HttpResponse response) { | |
15 List<int> data = []; | |
16 request.inputStream.onData = () { | |
17 data.addAll(request.inputStream.read()); | |
18 }; | |
19 request.inputStream.onClosed = () { | |
20 count++; | |
21 Expect.equals(count, data.length); | |
22 switch (count - 1) { | |
23 case 0: | |
24 response.outputStream.writeFrom([65, 66, 67], 1, 1); | |
25 break; | |
26 case 1: | |
27 response.outputStream.writeFrom([65, 66, 67], 1); | |
28 break; | |
29 case 2: | |
30 response.outputStream.writeFrom([65, 66, 67]); | |
31 break; | |
32 default: | |
33 Expect.fail("Unexpected state"); | |
34 } | |
35 response.outputStream.close(); | |
36 }; | |
37 }; | |
38 server.listen('127.0.0.1', 0); | |
39 | |
40 Future makeRequest(int n) { | |
41 var completer = new Completer(); | |
42 var url = Uri.parse("http://localhost:${server.port}"); | |
43 var connection = client.openUrl("POST", url); | |
44 connection.onRequest = (HttpClientRequest request) { | |
45 request.contentLength = n + 1; | |
46 switch (n) { | |
47 case 0: | |
48 request.outputStream.writeFrom([65, 66, 67], 1, 1); | |
49 break; | |
50 case 1: | |
51 request.outputStream.writeFrom([65, 66, 67], 1); | |
52 break; | |
53 case 2: | |
54 request.outputStream.writeFrom([65, 66, 67]); | |
55 break; | |
56 default: | |
57 Expect.fail("Unexpected state"); | |
58 } | |
59 request.outputStream.close(); | |
60 }; | |
61 connection.onResponse = (HttpClientResponse response) { | |
62 List<int> data = []; | |
63 response.inputStream.onData = () { | |
64 data.addAll(response.inputStream.read()); | |
65 }; | |
66 response.inputStream.onClosed = () { | |
67 Expect.equals(count, data.length); | |
68 completer.complete(null); | |
69 }; | |
70 }; | |
71 return completer.future; | |
72 } | |
73 | |
74 makeRequest(0).then((_) { | |
75 makeRequest(1).then((_) { | |
76 makeRequest(2).then((_) { | |
77 client.shutdown(); | |
78 server.close(); | |
79 }); | |
80 }); | |
81 }); | |
82 } | |
OLD | NEW |