Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: tests/standalone/src/io/HttpServerEarlyCloseTest.dart

Issue 9965008: Updated the early close test, to use a Socket that we can close at any time. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/standalone/standalone.status » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #import("dart:io"); 1 #import("dart:io");
2 #import("dart:isolate"); 2 #import("dart:isolate");
3 3
4 class Server { 4 Future sendData(List<int> data, int port) {
5 Server() { 5 Completer c = new Completer();
6 HttpServer server = new HttpServer(); 6 Socket socket = new Socket("127.0.0.1", port);
7 server.listen("127.0.0.1", 0); 7 socket.onConnect = () {
8 port = server.port; 8 socket.onData = () {
9 server.onRequest = (HttpRequest request, HttpResponse response) { 9 Expect.fail("No data response was expected");
10 new Timer(100, (timer) => server.close());
11 }; 10 };
12 server.onError = (Object exception) { 11 socket.outputStream.onNoPendingWrites = () {
13 Expect.fail("Close should not give an error."); 12 socket.close(false);
13 c.complete(null);
14 }; 14 };
15 } 15 socket.outputStream.write(data);
16 int port; 16 };
17 return c.future;
17 } 18 }
18 19
19 class Client { 20 class EarlyCloseTest {
20 Client(int port) { 21 EarlyCloseTest(Object this.data,
21 ReceivePort r = new ReceivePort(); 22 String this.exception,
22 HttpClient client = new HttpClient(); 23 [bool this.expectRequest = false]);
23 HttpClientConnection c = client.get("127.0.0.1", port, "/"); 24
24 c.onRequest = (HttpClientRequest request) { 25 Future execute(HttpServer server) {
25 request.outputStream.close(); 26 Completer c = new Completer();
27
28 server.onRequest = (HttpRequest request, HttpResponse response) {
Søren Gjesse 2012/03/30 08:56:53 Maybe check that each onRequest function are only
Anders Johnsen 2012/03/30 09:29:27 Done.
29 Expect.isTrue(expectRequest);
26 }; 30 };
27 c.onResponse = (HttpClientResponse response) { 31 ReceivePort port = new ReceivePort();
28 Expect.fail("Response should not be given, as not data was returned."); 32 server.onError = (Exception error) {
Søren Gjesse 2012/03/30 08:56:53 Maybe set onRequest to a failure function here, ju
Anders Johnsen 2012/03/30 09:29:27 Ahh yes, very nice catch!
33 Expect.equals(exception, error.message);
34 port.close();
35 c.complete(null);
29 }; 36 };
30 c.onError = (Object exception) { 37
31 r.close(); 38 List<int> d;
32 }; 39 if (data is List<int>) d = data;
40 if (data is String) d = data.charCodes();
41 if (d == null) Expect.fail("Invalid data");
42 sendData(d, server.port).then((_) {
Søren Gjesse 2012/03/30 08:56:53 We don't want timed tests, as they are bound to b
Anders Johnsen 2012/03/30 09:29:27 Done.
43 // The error is to be expected within 100 ms.
44 new Timer(100, (_) {
45 Expect.isTrue(c.future.isComplete, "onError have not been signaled");
46 });
47 });
48
49 return c.future;
33 } 50 }
51
52 final Object data;
53 final String exception;
54 final bool expectRequest;
34 } 55 }
35 56
36 main() { 57 void testEarlyClose() {
37 Server server = new Server(); 58 List<EarlyCloseTest> tests = new List<EarlyCloseTest>();
38 new Client(server.port); 59 void add(Object data, String exception, [bool expectRequest = false]) {
60 tests.add(new EarlyCloseTest(data, exception, expectRequest));
61 }
62 // The empty packet is valid.
63
64 // Close while sending header
65 add("G", "Connection closed before header was received");
66 add("GET /", "Failed to parse HTTP");
67 add("GET / HTTP/1.1", "Failed to parse HTTP");
68 add("GET / HTTP/1.1\r\n", "Failed to parse HTTP");
69
70 // Close while sending content
71 add("GET / HTTP/1.1\r\nContent-Length: 100\r\n\r\n",
72 "Failed to parse HTTP",
73 expectRequest: true);
74 add("GET / HTTP/1.1\r\nContent-Length: 100\r\n\r\n1",
75 "Failed to parse HTTP",
76 expectRequest: true);
77
78
79 HttpServer server = new HttpServer();
80 server.listen("127.0.0.1", 0);
81 void runTest(Iterator it) {
82 if (it.hasNext()) {
83 it.next().execute(server).then((_) => runTest(it));
84 } else {
85 server.close();
86 }
87 }
88 runTest(tests.iterator());
39 } 89 }
90
91 void main() {
92 testEarlyClose();
93 }
OLDNEW
« no previous file with comments | « no previous file | tests/standalone/standalone.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698