OLD | NEW |
1 // Copyright (c) 2013, 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 "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
7 import "dart:async"; | 7 import "dart:async"; |
8 import "dart:io"; | 8 import "dart:io"; |
9 | 9 |
10 void testHttp10Close(bool closeRequest) { | 10 void testHttp10Close(bool closeRequest) { |
11 HttpServer.bind("127.0.0.1", 0).then((server) { | 11 HttpServer.bind("127.0.0.1", 0).then((server) { |
12 server.listen((request) { | 12 server.listen((request) { |
13 request.response.close(); | 13 request.response.close(); |
14 }); | 14 }); |
15 | 15 |
16 Socket.connect("127.0.0.1", server.port) | 16 Socket.connect("127.0.0.1", server.port).then((socket) { |
17 .then((socket) { | 17 socket.write("GET / HTTP/1.0\r\n\r\n"); |
18 socket.write("GET / HTTP/1.0\r\n\r\n"); | 18 socket.listen((data) {}, onDone: () { |
19 socket.listen( | 19 if (!closeRequest) socket.destroy(); |
20 (data) {}, | 20 server.close(); |
21 onDone: () { | |
22 if (!closeRequest) socket.destroy(); | |
23 server.close(); | |
24 }); | |
25 if (closeRequest) socket.close(); | |
26 }); | 21 }); |
| 22 if (closeRequest) socket.close(); |
| 23 }); |
27 }); | 24 }); |
28 } | 25 } |
29 | 26 |
30 void testHttp11Close(bool closeRequest) { | 27 void testHttp11Close(bool closeRequest) { |
31 HttpServer.bind("127.0.0.1", 0).then((server) { | 28 HttpServer.bind("127.0.0.1", 0).then((server) { |
32 server.listen((request) { | 29 server.listen((request) { |
33 request.response.close(); | 30 request.response.close(); |
34 }); | 31 }); |
35 | 32 |
36 Socket.connect("127.0.0.1", server.port) | 33 Socket.connect("127.0.0.1", server.port).then((socket) { |
37 .then((socket) { | 34 List<int> buffer = new List<int>(1024); |
38 List<int> buffer = new List<int>(1024); | 35 socket.write("GET / HTTP/1.1\r\nConnection: close\r\n\r\n"); |
39 socket.write("GET / HTTP/1.1\r\nConnection: close\r\n\r\n"); | 36 socket.listen((data) {}, onDone: () { |
40 socket.listen( | 37 if (!closeRequest) socket.destroy(); |
41 (data) {}, | 38 server.close(); |
42 onDone: () { | |
43 if (!closeRequest) socket.destroy(); | |
44 server.close(); | |
45 }); | |
46 if (closeRequest) socket.close(); | |
47 }); | 39 }); |
| 40 if (closeRequest) socket.close(); |
| 41 }); |
48 }); | 42 }); |
49 } | 43 } |
50 | 44 |
51 void testStreamResponse() { | 45 void testStreamResponse() { |
52 HttpServer.bind("127.0.0.1", 0).then((server) { | 46 HttpServer.bind("127.0.0.1", 0).then((server) { |
53 server.listen((request) { | 47 server.listen((request) { |
54 var timer = new Timer.periodic(const Duration(milliseconds: 0), (_) { | 48 var timer = new Timer.periodic(const Duration(milliseconds: 0), (_) { |
55 request.response.write( | 49 request.response |
56 'data:${new DateTime.now().millisecondsSinceEpoch}\n\n'); | 50 .write('data:${new DateTime.now().millisecondsSinceEpoch}\n\n'); |
57 }); | 51 }); |
58 request.response.done | 52 request.response.done.whenComplete(() { |
59 .whenComplete(() { | 53 timer.cancel(); |
60 timer.cancel(); | 54 }).catchError((_) {}); |
61 }) | |
62 .catchError((_) {}); | |
63 }); | 55 }); |
64 | 56 |
65 var client = new HttpClient(); | 57 var client = new HttpClient(); |
66 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}")) | 58 client |
67 .then((request) => request.close()) | 59 .getUrl(Uri.parse("http://127.0.0.1:${server.port}")) |
68 .then((response) { | 60 .then((request) => request.close()) |
69 int bytes = 0; | 61 .then((response) { |
70 response.listen( | 62 int bytes = 0; |
71 (data) { | 63 response.listen((data) { |
72 bytes += data.length; | 64 bytes += data.length; |
73 if (bytes > 100) { | 65 if (bytes > 100) { |
74 client.close(force: true); | 66 client.close(force: true); |
75 } | 67 } |
76 }, | 68 }, onError: (error) { |
77 onError: (error) { | 69 server.close(); |
78 server.close(); | |
79 }); | |
80 }); | 70 }); |
| 71 }); |
81 }); | 72 }); |
82 } | 73 } |
83 | 74 |
84 main() { | 75 main() { |
85 testHttp10Close(false); | 76 testHttp10Close(false); |
86 testHttp10Close(true); | 77 testHttp10Close(true); |
87 testHttp11Close(false); | 78 testHttp11Close(false); |
88 testHttp11Close(true); | 79 testHttp11Close(true); |
89 testStreamResponse(); | 80 testStreamResponse(); |
90 } | 81 } |
OLD | NEW |