OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // VMOptions= |
| 6 // VMOptions=--short_socket_read |
| 7 // VMOptions=--short_socket_write |
| 8 // VMOptions=--short_socket_read --short_socket_write |
| 9 |
| 10 import "dart:isolate"; |
| 11 import "dart:io"; |
| 12 |
| 13 // Client makes a HTTP 1.0 request without connection keep alive. The |
| 14 // server sets a content length but still needs to close the |
| 15 // connection as there is no keep alive. |
| 16 void testHttp10NoKeepAlive() { |
| 17 HttpServer.bind().then((server) { |
| 18 server.listen( |
| 19 (HttpRequest request) { |
| 20 Expect.isNull(request.headers.value('content-length')); |
| 21 Expect.equals(-1, request.contentLength); |
| 22 var response = request.response; |
| 23 response.contentLength = 1; |
| 24 Expect.equals("1.0", request.protocolVersion); |
| 25 response.done |
| 26 .then((_) => Expect.fail("Unexpected response completion")) |
| 27 .catchError((e) => Expect.isTrue(e.error is HttpException)); |
| 28 response.addString("Z"); |
| 29 response.addString("Z"); |
| 30 response.close(); |
| 31 Expect.throws(() => response.addString("x"), |
| 32 (e) => e is StateError); |
| 33 }, |
| 34 onError: (e) => Expect.fail("Unexpected error $e")); |
| 35 |
| 36 int count = 0; |
| 37 makeRequest() { |
| 38 Socket.connect("127.0.0.1", server.port).then((socket) { |
| 39 socket.addString("GET / HTTP/1.0\r\n\r\n"); |
| 40 |
| 41 List<int> response = []; |
| 42 socket.listen( |
| 43 response.addAll, |
| 44 onDone: () { |
| 45 count++; |
| 46 socket.destroy(); |
| 47 String s = new String.fromCharCodes(response).toLowerCase(); |
| 48 Expect.equals("z", s[s.length - 1]); |
| 49 Expect.isTrue(s.indexOf("\r\ncontent-length: 1\r\n") > 0); |
| 50 Expect.equals(-1, s.indexOf("keep-alive")); |
| 51 if (count < 10) { |
| 52 makeRequest(); |
| 53 } else { |
| 54 server.close(); |
| 55 } |
| 56 }); |
| 57 }); |
| 58 } |
| 59 makeRequest(); |
| 60 }); |
| 61 } |
| 62 |
| 63 |
| 64 // Client makes a HTTP 1.0 request and the server does not set a |
| 65 // content length so it has to close the connection to mark the end of |
| 66 // the response. |
| 67 void testHttp10ServerClose() { |
| 68 HttpServer.bind().then((server) { |
| 69 server.listen( |
| 70 (HttpRequest request) { |
| 71 Expect.isNull(request.headers.value('content-length')); |
| 72 Expect.equals(-1, request.contentLength); |
| 73 var response = request.response; |
| 74 Expect.equals("1.0", request.protocolVersion); |
| 75 response.addString("Z"); |
| 76 response.close(); |
| 77 }, |
| 78 onError: (e) => Expect.fail("Unexpected error $e")); |
| 79 |
| 80 int count = 0; |
| 81 makeRequest() { |
| 82 Socket.connect("127.0.0.1", server.port).then((socket) { |
| 83 socket.addString("GET / HTTP/1.0\r\n\r\n"); |
| 84 socket.addString("Connection: Keep-Alive\r\n\r\n"); |
| 85 |
| 86 List<int> response = []; |
| 87 socket.listen( |
| 88 response.addAll, |
| 89 onDone: () { |
| 90 socket.destroy(); |
| 91 count++; |
| 92 String s = new String.fromCharCodes(response).toLowerCase(); |
| 93 print(s); |
| 94 Expect.equals("z", s[s.length - 1]); |
| 95 Expect.equals(-1, s.indexOf("content-length:")); |
| 96 Expect.equals(-1, s.indexOf("keep-alive")); |
| 97 if (count < 10) { |
| 98 makeRequest(); |
| 99 } else { |
| 100 server.close(); |
| 101 } |
| 102 },onError: (e) => print(e)); |
| 103 }); |
| 104 } |
| 105 makeRequest(); |
| 106 }); |
| 107 } |
| 108 |
| 109 |
| 110 // Client makes a HTTP 1.0 request with connection keep alive. The |
| 111 // server sets a content length so the persistent connection can be |
| 112 // used. |
| 113 void testHttp10KeepAlive() { |
| 114 HttpServer.bind().then((server) { |
| 115 server.listen( |
| 116 (HttpRequest request) { |
| 117 Expect.isNull(request.headers.value('content-length')); |
| 118 Expect.equals(-1, request.contentLength); |
| 119 var response = request.response; |
| 120 response.contentLength = 1; |
| 121 Expect.equals("1.0", request.protocolVersion); |
| 122 response.addString("Z"); |
| 123 response.close(); |
| 124 }, |
| 125 onError: (e) => Expect.fail("Unexpected error $e")); |
| 126 |
| 127 Socket.connect("127.0.0.1", server.port).then((socket) { |
| 128 void sendRequest() { |
| 129 socket.addString("GET / HTTP/1.0\r\n"); |
| 130 socket.addString("Connection: Keep-Alive\r\n\r\n"); |
| 131 } |
| 132 |
| 133 List<int> response = []; |
| 134 int count = 0; |
| 135 socket.listen( |
| 136 (d) { |
| 137 response.addAll(d); |
| 138 if (response[response.length - 1] == "Z".codeUnitAt(0)) { |
| 139 String s = new String.fromCharCodes(response).toLowerCase(); |
| 140 Expect.isTrue(s.indexOf("\r\nconnection: keep-alive\r\n") > 0); |
| 141 Expect.isTrue(s.indexOf("\r\ncontent-length: 1\r\n") > 0); |
| 142 count++; |
| 143 if (count < 10) { |
| 144 response = []; |
| 145 sendRequest(); |
| 146 } else { |
| 147 socket.close(); |
| 148 } |
| 149 } |
| 150 }, |
| 151 onDone: () { |
| 152 socket.destroy(); |
| 153 server.close(); |
| 154 }); |
| 155 sendRequest(); |
| 156 }); |
| 157 }); |
| 158 } |
| 159 |
| 160 |
| 161 // Client makes a HTTP 1.0 request with connection keep alive. The |
| 162 // server does not set a content length so it cannot honor connection |
| 163 // keep alive. |
| 164 void testHttp10KeepAliveServerCloses() { |
| 165 HttpServer.bind().then((server) { |
| 166 server.listen( |
| 167 (HttpRequest request) { |
| 168 Expect.isNull(request.headers.value('content-length')); |
| 169 Expect.equals(-1, request.contentLength); |
| 170 var response = request.response; |
| 171 Expect.equals("1.0", request.protocolVersion); |
| 172 response.addString("Z"); |
| 173 response.close(); |
| 174 }, |
| 175 onError: (e) => Expect.fail("Unexpected error $e")); |
| 176 |
| 177 int count = 0; |
| 178 makeRequest() { |
| 179 Socket.connect("127.0.0.1", server.port).then((socket) { |
| 180 socket.addString("GET / HTTP/1.0\r\n"); |
| 181 socket.addString("Connection: Keep-Alive\r\n\r\n"); |
| 182 |
| 183 List<int> response = []; |
| 184 socket.listen( |
| 185 response.addAll, |
| 186 onDone: () { |
| 187 socket.destroy(); |
| 188 count++; |
| 189 String s = new String.fromCharCodes(response).toLowerCase(); |
| 190 Expect.equals("z", s[s.length - 1]); |
| 191 Expect.equals(-1, s.indexOf("content-length")); |
| 192 Expect.equals(-1, s.indexOf("connection")); |
| 193 if (count < 10) { |
| 194 makeRequest(); |
| 195 } else { |
| 196 server.close(); |
| 197 } |
| 198 }); |
| 199 }); |
| 200 } |
| 201 makeRequest(); |
| 202 }); |
| 203 } |
| 204 |
| 205 |
| 206 void main() { |
| 207 testHttp10NoKeepAlive(); |
| 208 // TODO(8871): This test fails with short socket writes. |
| 209 //testHttp10ServerClose(); |
| 210 testHttp10KeepAlive(); |
| 211 testHttp10KeepAliveServerCloses(); |
| 212 } |
OLD | NEW |