| OLD | NEW |
| (Empty) |
| 1 #import("dart:io"); | |
| 2 #import("dart:isolate"); | |
| 3 | |
| 4 void sendData(List<int> data, int port) { | |
| 5 Socket socket = new Socket("127.0.0.1", port); | |
| 6 socket.onConnect = () { | |
| 7 socket.onData = () { | |
| 8 Expect.fail("No data response was expected"); | |
| 9 }; | |
| 10 socket.outputStream.onNoPendingWrites = () { | |
| 11 socket.close(true); | |
| 12 }; | |
| 13 socket.outputStream.write(data); | |
| 14 }; | |
| 15 } | |
| 16 | |
| 17 class EarlyCloseTest { | |
| 18 EarlyCloseTest(Object this.data, | |
| 19 String this.exception, | |
| 20 [bool this.expectRequest = false]); | |
| 21 | |
| 22 Future execute(HttpServer server) { | |
| 23 Completer c = new Completer(); | |
| 24 | |
| 25 bool calledOnRequest = false; | |
| 26 bool calledOnError = false; | |
| 27 server.defaultRequestHandler = | |
| 28 (HttpRequest request, HttpResponse response) { | |
| 29 Expect.isTrue(expectRequest); | |
| 30 Expect.isFalse(calledOnError); | |
| 31 Expect.isFalse(calledOnRequest, "onRequest called multiple times"); | |
| 32 calledOnRequest = true; | |
| 33 }; | |
| 34 ReceivePort port = new ReceivePort(); | |
| 35 server.onError = (Exception error) { | |
| 36 Expect.isFalse(calledOnError); | |
| 37 Expect.equals(exception, error.message); | |
| 38 Expect.equals(expectRequest, calledOnRequest); | |
| 39 calledOnError = true; | |
| 40 port.close(); | |
| 41 c.complete(null); | |
| 42 }; | |
| 43 | |
| 44 List<int> d; | |
| 45 if (data is List<int>) d = data; | |
| 46 if (data is String) d = data.charCodes(); | |
| 47 if (d == null) Expect.fail("Invalid data"); | |
| 48 sendData(d, server.port); | |
| 49 | |
| 50 return c.future; | |
| 51 } | |
| 52 | |
| 53 final Object data; | |
| 54 final String exception; | |
| 55 final bool expectRequest; | |
| 56 } | |
| 57 | |
| 58 void testEarlyClose() { | |
| 59 List<EarlyCloseTest> tests = new List<EarlyCloseTest>(); | |
| 60 void add(Object data, String exception, [bool expectRequest = false]) { | |
| 61 tests.add(new EarlyCloseTest(data, exception, expectRequest)); | |
| 62 } | |
| 63 // The empty packet is valid. | |
| 64 | |
| 65 // Close while sending header | |
| 66 add("G", "Connection closed before full header was received"); | |
| 67 add("GET /", "Connection closed before full header was received"); | |
| 68 add("GET / HTTP/1.1", "Connection closed before full header was received"); | |
| 69 add("GET / HTTP/1.1\r\n", "Connection closed before full header was received")
; | |
| 70 | |
| 71 // Close while sending content | |
| 72 add("GET / HTTP/1.1\r\nContent-Length: 100\r\n\r\n", | |
| 73 "Connection closed before full body was received", | |
| 74 expectRequest: true); | |
| 75 add("GET / HTTP/1.1\r\nContent-Length: 100\r\n\r\n1", | |
| 76 "Connection closed before full body was received", | |
| 77 expectRequest: true); | |
| 78 | |
| 79 | |
| 80 HttpServer server = new HttpServer(); | |
| 81 server.listen("127.0.0.1", 0); | |
| 82 void runTest(Iterator it) { | |
| 83 if (it.hasNext()) { | |
| 84 it.next().execute(server).then((_) => runTest(it)); | |
| 85 } else { | |
| 86 server.close(); | |
| 87 } | |
| 88 } | |
| 89 runTest(tests.iterator()); | |
| 90 } | |
| 91 | |
| 92 void main() { | |
| 93 testEarlyClose(); | |
| 94 } | |
| OLD | NEW |