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 | 5 |
6 import "dart:async"; | 6 import "dart:async"; |
7 import "dart:io"; | 7 import "dart:io"; |
8 import "dart:uri"; | 8 import "dart:uri"; |
9 | 9 |
10 void testHttp10Close(bool closeRequest) { | 10 void testHttp10Close(bool closeRequest) { |
11 HttpServer server = new HttpServer(); | 11 HttpServer.bind().then((server) { |
12 server.listen("127.0.0.1", 0, backlog: 5); | 12 server.listen((request) { |
| 13 request.response.close(); |
| 14 }); |
13 | 15 |
14 Socket socket = new Socket("127.0.0.1", server.port); | 16 Socket.connect("127.0.0.1", server.port) |
15 socket.onConnect = () { | 17 .then((socket) { |
16 List<int> buffer = new List<int>.fixedLength(1024); | 18 socket.addString("GET / HTTP/1.0\r\n\r\n"); |
17 socket.outputStream.writeString("GET / HTTP/1.0\r\n\r\n"); | 19 socket.listen( |
18 if (closeRequest) socket.outputStream.close(); | 20 (data) {}, |
19 socket.onData = () => socket.readList(buffer, 0, buffer.length); | 21 onDone: () { |
20 socket.onClosed = () { | 22 if (!closeRequest) socket.destroy(); |
21 if (!closeRequest) socket.close(true); | 23 server.close(); |
22 server.close(); | 24 }); |
23 }; | 25 if (closeRequest) socket.close(); |
24 }; | 26 }); |
| 27 }); |
25 } | 28 } |
26 | 29 |
27 void testHttp11Close(bool closeRequest) { | 30 void testHttp11Close(bool closeRequest) { |
28 HttpServer server = new HttpServer(); | 31 HttpServer.bind().then((server) { |
29 server.listen("127.0.0.1", 0, backlog: 5); | 32 server.listen((request) { |
| 33 request.response.close(); |
| 34 }); |
30 | 35 |
31 Socket socket = new Socket("127.0.0.1", server.port); | 36 Socket.connect("127.0.0.1", server.port) |
32 socket.onConnect = () { | 37 .then((socket) { |
33 List<int> buffer = new List<int>.fixedLength(1024); | 38 List<int> buffer = new List<int>.fixedLength(1024); |
34 socket.outputStream.writeString( | 39 socket.addString("GET / HTTP/1.1\r\nConnection: close\r\n\r\n"); |
35 "GET / HTTP/1.1\r\nConnection: close\r\n\r\n"); | 40 socket.listen( |
36 if (closeRequest) socket.outputStream.close(); | 41 (data) {}, |
37 socket.onData = () => socket.readList(buffer, 0, buffer.length); | 42 onDone: () { |
38 socket.onClosed = () { | 43 if (!closeRequest) socket.destroy(); |
39 if (!closeRequest) socket.close(true); | 44 server.close(); |
40 server.close(); | 45 }); |
41 }; | 46 if (closeRequest) socket.close(); |
42 }; | 47 }); |
| 48 }); |
43 } | 49 } |
44 | 50 |
45 void testStreamResponse() { | 51 void testStreamResponse() { |
46 Timer timer; | 52 HttpServer.bind().then((server) { |
47 var server = new HttpServer(); | 53 server.listen((request) { |
48 server.onError = (e) { | 54 // TODO(ajohnsen): Use timer (see old version). |
49 server.close(); | 55 for (int i = 0; i < 10; i++) { |
50 timer.cancel(); | 56 request.response.addString( |
51 }; | 57 'data:${new DateTime.now().millisecondsSinceEpoch}\n\n'); |
52 server.listen("127.0.0.1", 0, backlog: 5); | |
53 server.defaultRequestHandler = (var request, var response) { | |
54 timer = new Timer.repeating(new Duration(milliseconds: 10), (_) { | |
55 DateTime now = new DateTime.now(); | |
56 try { | |
57 response.outputStream.writeString( | |
58 'data:${now.millisecondsSinceEpoch}\n\n'); | |
59 } catch (e) { | |
60 timer.cancel(); | |
61 server.close(); | |
62 } | 58 } |
63 }); | 59 }); |
64 }; | |
65 | 60 |
66 var client = new HttpClient(); | 61 var client = new HttpClient(); |
67 var connection = | 62 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}")) |
68 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}")); | 63 .then((request) => request.close()) |
69 connection.onResponse = (resp) { | 64 .then((response) { |
70 int bytes = 0; | 65 int bytes = 0; |
71 resp.inputStream.onData = () { | 66 response.listen( |
72 bytes += resp.inputStream.read().length; | 67 (data) { |
73 if (bytes > 100) { | 68 bytes += data.length; |
74 client.shutdown(force: true); | 69 if (bytes > 100) { |
75 } | 70 client.close(force: true); |
76 }; | 71 } |
77 }; | 72 }, |
78 connection.onError = (e) => Expect.isTrue(e is HttpException); | 73 onError: (error) { |
| 74 server.close(); |
| 75 }); |
| 76 }); |
| 77 }); |
79 } | 78 } |
80 | 79 |
81 main() { | 80 main() { |
82 testHttp10Close(false); | 81 testHttp10Close(false); |
83 testHttp10Close(true); | 82 testHttp10Close(true); |
84 testHttp11Close(false); | 83 testHttp11Close(false); |
85 testHttp11Close(true); | 84 testHttp11Close(true); |
86 testStreamResponse(); | 85 testStreamResponse(); |
87 } | 86 } |
OLD | NEW |