OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 import "dart:io"; | 5 import "dart:io"; |
6 | 6 |
7 void testHEAD(int totalConnections) { | 7 void testHEAD(int totalConnections) { |
8 HttpServer server = new HttpServer(); | 8 HttpServer.bind().then((server) { |
9 server.onError = (e) => Expect.fail("Unexpected error $e"); | 9 server.listen((request) { |
10 server.listen("127.0.0.1", 0, backlog: totalConnections); | 10 var response = request.response; |
11 server.addRequestHandler( | 11 if (request.uri.path == "/test100") { |
12 (request) => request.path == "/test100", | 12 response.contentLength = 100; |
13 (HttpRequest request, HttpResponse response) { | 13 response.close(); |
14 response.contentLength = 100; | 14 } else if (request.uri.path == "/test200") { |
15 response.outputStream.close(); | 15 response.contentLength = 200; |
16 }); | 16 List<int> data = new List<int>.fixedLength(200, fill: 0); |
17 server.addRequestHandler( | 17 response.add(data); |
18 (request) => request.path == "/test200", | 18 response.close(); |
19 (HttpRequest request, HttpResponse response) { | 19 } else { |
20 response.contentLength = 200; | 20 assert(false); |
21 List<int> data = new List<int>.fixedLength(200); | 21 } |
22 response.outputStream.write(data); | |
23 response.outputStream.close(); | |
24 }); | 22 }); |
25 | 23 |
26 HttpClient client = new HttpClient(); | 24 HttpClient client = new HttpClient(); |
27 | 25 |
28 int count = 0; | 26 int count = 0; |
29 for (int i = 0; i < totalConnections; i++) { | 27 for (int i = 0; i < totalConnections; i++) { |
30 HttpClientConnection conn; | 28 int len = (i % 2 == 0) ? 100 : 200; |
31 int len = (i % 2 == 0) ? 100 : 200; | 29 client.open("HEAD", "127.0.0.1", server.port, "/test$len") |
32 if (i % 2 == 0) { | 30 .then((request) => request.close()) |
33 conn = client.open("HEAD", "127.0.0.1", server.port, "/test$len"); | 31 .then((HttpClientResponse response) { |
34 } else { | 32 Expect.equals(len, response.contentLength); |
35 conn = client.open("HEAD", "127.0.0.1", server.port, "/test$len"); | 33 response.listen( |
| 34 (_) => Expect.fail("Data from HEAD request"), |
| 35 onDone: () { |
| 36 count++; |
| 37 if (count == totalConnections) { |
| 38 client.close(); |
| 39 server.close(); |
| 40 } |
| 41 }); |
| 42 }); |
36 } | 43 } |
37 conn.onError = (e) => Expect.fail("Unexpected error $e"); | 44 }); |
38 conn.onResponse = (HttpClientResponse response) { | |
39 Expect.equals(len, response.contentLength); | |
40 response.inputStream.onData = () => Expect.fail("Data from HEAD request"); | |
41 response.inputStream.onClosed = () { | |
42 count++; | |
43 if (count == totalConnections) { | |
44 client.shutdown(); | |
45 server.close(); | |
46 } | |
47 }; | |
48 }; | |
49 } | |
50 } | 45 } |
51 | 46 |
52 void main() { | 47 void main() { |
53 testHEAD(4); | 48 testHEAD(4); |
54 } | 49 } |
OLD | NEW |