| 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 // VMOptions= | 5 // VMOptions= |
| 6 // VMOptions=--short_socket_read | 6 // VMOptions=--short_socket_read |
| 7 // VMOptions=--short_socket_write | 7 // VMOptions=--short_socket_write |
| 8 // VMOptions=--short_socket_read --short_socket_write | 8 // VMOptions=--short_socket_read --short_socket_write |
| 9 | 9 |
| 10 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
| 11 import "dart:async"; | 11 import "dart:async"; |
| 12 import "dart:io"; | 12 import "dart:io"; |
| 13 import "dart:isolate"; | 13 import "dart:isolate"; |
| 14 | 14 |
| 15 void sendData(List<int> data, int port) { | 15 Future sendData(List<int> data, int port) { |
| 16 Socket.connect("127.0.0.1", port).then((socket) { | 16 return Socket.connect("127.0.0.1", port).then((socket) { |
| 17 socket.listen((data) { | 17 socket.listen((data) { |
| 18 Expect.fail("No data response was expected"); | 18 Expect.fail("No data response was expected"); |
| 19 }); | 19 }); |
| 20 socket.add(data); | 20 socket.add(data); |
| 21 socket.close(); | |
| 22 socket.done.then((_) { | 21 socket.done.then((_) { |
| 23 socket.destroy(); | 22 socket.destroy(); |
| 24 }); | 23 }); |
| 24 return socket.close(); |
| 25 }); | 25 }); |
| 26 } | 26 } |
| 27 | 27 |
| 28 class EarlyCloseTest { | 28 class EarlyCloseTest { |
| 29 EarlyCloseTest(this.data, | 29 EarlyCloseTest(this.data, |
| 30 String this.exception, | 30 [String this.exception, |
| 31 [bool this.expectRequest = false]); | 31 bool this.expectRequest = false]); |
| 32 | 32 |
| 33 Future execute(HttpServer server) { | 33 Future execute() { |
| 34 Completer c = new Completer(); | 34 return HttpServer.bind("127.0.0.1", 0).then((server) { |
| 35 Completer c = new Completer(); |
| 35 | 36 |
| 36 bool calledOnRequest = false; | 37 bool calledOnRequest = false; |
| 37 bool calledOnError = false; | 38 bool calledOnError = false; |
| 38 ReceivePort port = new ReceivePort(); | 39 bool calledOnDone = false; |
| 39 server.listen( | 40 ReceivePort port = new ReceivePort(); |
| 40 (request) { | 41 server.listen( |
| 41 Expect.isTrue(expectRequest); | 42 (request) { |
| 42 Expect.isFalse(calledOnError); | 43 Expect.isTrue(expectRequest); |
| 43 Expect.isFalse(calledOnRequest, "onRequest called multiple times"); | 44 Expect.isFalse(calledOnError); |
| 44 calledOnRequest = true; | 45 Expect.isFalse(calledOnRequest, "onRequest called multiple times"); |
| 45 request.listen( | 46 calledOnRequest = true; |
| 46 (_) {}, | 47 request.listen( |
| 47 onError: (error) { | 48 (_) {}, |
| 48 Expect.isFalse(calledOnError); | 49 onError: (error) { |
| 49 Expect.equals(exception, error.message); | 50 Expect.isFalse(calledOnError); |
| 50 calledOnError = true; | 51 Expect.equals(exception, error.message); |
| 51 port.close(); | 52 calledOnError = true; |
| 52 c.complete(null); | 53 if (exception != null) port.close(); |
| 53 }); | 54 }); |
| 54 }, | 55 server.close(); |
| 55 onError: (error) { | 56 }, |
| 56 Expect.isFalse(calledOnError); | 57 onDone: () { |
| 57 Expect.equals(exception, error.message); | 58 Expect.equals(expectRequest, calledOnRequest); |
| 58 Expect.equals(expectRequest, calledOnRequest); | 59 calledOnDone = true; |
| 59 calledOnError = true; | 60 if (exception == null) port.close(); |
| 60 port.close(); | 61 c.complete(null); |
| 61 c.complete(null); | 62 }); |
| 62 }); | |
| 63 | 63 |
| 64 List<int> d; | 64 List<int> d; |
| 65 if (data is List<int>) d = data; | 65 if (data is List<int>) d = data; |
| 66 if (data is String) d = data.codeUnits; | 66 if (data is String) d = data.codeUnits; |
| 67 if (d == null) Expect.fail("Invalid data"); | 67 if (d == null) Expect.fail("Invalid data"); |
| 68 sendData(d, server.port); | 68 sendData(d, server.port) |
| 69 .then((_) { |
| 70 if (!expectRequest) server.close(); |
| 71 }); |
| 69 | 72 |
| 70 return c.future; | 73 return c.future; |
| 74 }); |
| 71 } | 75 } |
| 72 | 76 |
| 73 final data; | 77 final data; |
| 74 final String exception; | 78 final String exception; |
| 75 final bool expectRequest; | 79 final bool expectRequest; |
| 76 } | 80 } |
| 77 | 81 |
| 78 void testEarlyClose1() { | 82 void testEarlyClose1() { |
| 79 List<EarlyCloseTest> tests = new List<EarlyCloseTest>(); | 83 List<EarlyCloseTest> tests = new List<EarlyCloseTest>(); |
| 80 void add(Object data, String exception, {bool expectRequest: false}) { | 84 void add(Object data, [String exception, bool expectRequest = false]) { |
| 81 tests.add(new EarlyCloseTest(data, exception, expectRequest)); | 85 tests.add(new EarlyCloseTest(data, exception, expectRequest)); |
| 82 } | 86 } |
| 83 // The empty packet is valid. | 87 // The empty packet is valid. |
| 84 | 88 |
| 85 // Close while sending header | 89 // Close while sending header |
| 86 String message = "Connection closed before full header was received"; | 90 add("G"); |
| 87 add("G", message); | 91 add("GET /"); |
| 88 add("GET /", message); | 92 add("GET / HTTP/1.1"); |
| 89 add("GET / HTTP/1.1", message); | 93 add("GET / HTTP/1.1\r\n"); |
| 90 add("GET / HTTP/1.1\r\n", message); | |
| 91 | 94 |
| 92 // Close while sending content | 95 // Close while sending content |
| 93 add("GET / HTTP/1.1\r\nContent-Length: 100\r\n\r\n", | 96 add("GET / HTTP/1.1\r\nContent-Length: 100\r\n\r\n", |
| 94 "Connection closed while receiving data", | 97 "Connection closed while receiving data", |
| 95 expectRequest: true); | 98 true); |
| 96 add("GET / HTTP/1.1\r\nContent-Length: 100\r\n\r\n1", | 99 add("GET / HTTP/1.1\r\nContent-Length: 100\r\n\r\n1", |
| 97 "Connection closed while receiving data", | 100 "Connection closed while receiving data", |
| 98 expectRequest: true); | 101 true); |
| 99 | 102 |
| 100 void runTest(Iterator it) { | 103 void runTest(Iterator it) { |
| 101 if (it.moveNext()) { | 104 if (it.moveNext()) { |
| 102 HttpServer.bind("127.0.0.1", 0).then((server) { | 105 it.current.execute().then((_) { |
| 103 it.current.execute(server).then((_) { | 106 runTest(it); |
| 104 runTest(it); | |
| 105 server.close(); | |
| 106 }); | |
| 107 }); | 107 }); |
| 108 } | 108 } |
| 109 } | 109 } |
| 110 runTest(tests.iterator); | 110 runTest(tests.iterator); |
| 111 } | 111 } |
| 112 | 112 |
| 113 testEarlyClose2() { | 113 testEarlyClose2() { |
| 114 HttpServer.bind("127.0.0.1", 0).then((server) { | 114 HttpServer.bind("127.0.0.1", 0).then((server) { |
| 115 server.listen( | 115 server.listen( |
| 116 (request) { | 116 (request) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 socket.done.catchError((_) {}); | 162 socket.done.catchError((_) {}); |
| 163 }); | 163 }); |
| 164 }); | 164 }); |
| 165 } | 165 } |
| 166 | 166 |
| 167 void main() { | 167 void main() { |
| 168 testEarlyClose1(); | 168 testEarlyClose1(); |
| 169 testEarlyClose2(); | 169 testEarlyClose2(); |
| 170 testEarlyClose3(); | 170 testEarlyClose3(); |
| 171 } | 171 } |
| OLD | NEW |