OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // (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 "dart:isolate"; | 10 import "dart:isolate"; |
11 import "dart:io"; | 11 import "dart:io"; |
12 | 12 |
13 // Client makes a HTTP 1.0 request without connection keep alive. The | 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 | 14 // server sets a content length but still needs to close the |
15 // connection as there is no keep alive. | 15 // connection as there is no keep alive. |
16 void testHttp10NoKeepAlive() { | 16 void testHttp10NoKeepAlive() { |
17 HttpServer.bind().then((server) { | 17 HttpServer.bind().then((server) { |
18 server.listen( | 18 server.listen( |
19 (HttpRequest request) { | 19 (HttpRequest request) { |
20 Expect.isNull(request.headers.value('content-length')); | 20 Expect.isNull(request.headers.value('content-length')); |
21 Expect.equals(-1, request.contentLength); | 21 Expect.equals(-1, request.contentLength); |
22 var response = request.response; | 22 var response = request.response; |
23 response.contentLength = 1; | 23 response.contentLength = 1; |
24 Expect.equals("1.0", request.protocolVersion); | 24 Expect.equals("1.0", request.protocolVersion); |
25 response.done | 25 response.done |
26 .then((_) => Expect.fail("Unexpected response completion")) | 26 .then((_) => Expect.fail("Unexpected response completion")) |
27 .catchError((e) => Expect.isTrue(e.error is HttpException)); | 27 .catchError((e) => Expect.isTrue(e.error is HttpException)); |
28 response.addString("Z"); | 28 response.write("Z"); |
29 response.addString("Z"); | 29 response.write("Z"); |
30 response.close(); | 30 response.close(); |
31 Expect.throws(() => response.addString("x"), | 31 Expect.throws(() => response.write("x"), |
32 (e) => e is StateError); | 32 (e) => e is StateError); |
33 }, | 33 }, |
34 onError: (e) => Expect.fail("Unexpected error $e")); | 34 onError: (e) => Expect.fail("Unexpected error $e")); |
35 | 35 |
36 int count = 0; | 36 int count = 0; |
37 makeRequest() { | 37 makeRequest() { |
38 Socket.connect("127.0.0.1", server.port).then((socket) { | 38 Socket.connect("127.0.0.1", server.port).then((socket) { |
39 socket.addString("GET / HTTP/1.0\r\n\r\n"); | 39 socket.write("GET / HTTP/1.0\r\n\r\n"); |
40 | 40 |
41 List<int> response = []; | 41 List<int> response = []; |
42 socket.listen( | 42 socket.listen( |
43 response.addAll, | 43 response.addAll, |
44 onDone: () { | 44 onDone: () { |
45 count++; | 45 count++; |
46 socket.destroy(); | 46 socket.destroy(); |
47 String s = new String.fromCharCodes(response).toLowerCase(); | 47 String s = new String.fromCharCodes(response).toLowerCase(); |
48 Expect.equals("z", s[s.length - 1]); | 48 Expect.equals("z", s[s.length - 1]); |
49 Expect.isTrue(s.indexOf("\r\ncontent-length: 1\r\n") > 0); | 49 Expect.isTrue(s.indexOf("\r\ncontent-length: 1\r\n") > 0); |
(...skipping 15 matching lines...) Expand all Loading... |
65 // content length so it has to close the connection to mark the end of | 65 // content length so it has to close the connection to mark the end of |
66 // the response. | 66 // the response. |
67 void testHttp10ServerClose() { | 67 void testHttp10ServerClose() { |
68 HttpServer.bind().then((server) { | 68 HttpServer.bind().then((server) { |
69 server.listen( | 69 server.listen( |
70 (HttpRequest request) { | 70 (HttpRequest request) { |
71 Expect.isNull(request.headers.value('content-length')); | 71 Expect.isNull(request.headers.value('content-length')); |
72 Expect.equals(-1, request.contentLength); | 72 Expect.equals(-1, request.contentLength); |
73 var response = request.response; | 73 var response = request.response; |
74 Expect.equals("1.0", request.protocolVersion); | 74 Expect.equals("1.0", request.protocolVersion); |
75 response.addString("Z"); | 75 response.write("Z"); |
76 response.close(); | 76 response.close(); |
77 }, | 77 }, |
78 onError: (e) => Expect.fail("Unexpected error $e")); | 78 onError: (e) => Expect.fail("Unexpected error $e")); |
79 | 79 |
80 int count = 0; | 80 int count = 0; |
81 makeRequest() { | 81 makeRequest() { |
82 Socket.connect("127.0.0.1", server.port).then((socket) { | 82 Socket.connect("127.0.0.1", server.port).then((socket) { |
83 socket.addString("GET / HTTP/1.0\r\n\r\n"); | 83 socket.write("GET / HTTP/1.0\r\n\r\n"); |
84 socket.addString("Connection: Keep-Alive\r\n\r\n"); | 84 socket.write("Connection: Keep-Alive\r\n\r\n"); |
85 | 85 |
86 List<int> response = []; | 86 List<int> response = []; |
87 socket.listen( | 87 socket.listen( |
88 response.addAll, | 88 response.addAll, |
89 onDone: () { | 89 onDone: () { |
90 socket.destroy(); | 90 socket.destroy(); |
91 count++; | 91 count++; |
92 String s = new String.fromCharCodes(response).toLowerCase(); | 92 String s = new String.fromCharCodes(response).toLowerCase(); |
93 print(s); | 93 print(s); |
94 Expect.equals("z", s[s.length - 1]); | 94 Expect.equals("z", s[s.length - 1]); |
(...skipping 17 matching lines...) Expand all Loading... |
112 // used. | 112 // used. |
113 void testHttp10KeepAlive() { | 113 void testHttp10KeepAlive() { |
114 HttpServer.bind().then((server) { | 114 HttpServer.bind().then((server) { |
115 server.listen( | 115 server.listen( |
116 (HttpRequest request) { | 116 (HttpRequest request) { |
117 Expect.isNull(request.headers.value('content-length')); | 117 Expect.isNull(request.headers.value('content-length')); |
118 Expect.equals(-1, request.contentLength); | 118 Expect.equals(-1, request.contentLength); |
119 var response = request.response; | 119 var response = request.response; |
120 response.contentLength = 1; | 120 response.contentLength = 1; |
121 Expect.equals("1.0", request.protocolVersion); | 121 Expect.equals("1.0", request.protocolVersion); |
122 response.addString("Z"); | 122 response.write("Z"); |
123 response.close(); | 123 response.close(); |
124 }, | 124 }, |
125 onError: (e) => Expect.fail("Unexpected error $e")); | 125 onError: (e) => Expect.fail("Unexpected error $e")); |
126 | 126 |
127 Socket.connect("127.0.0.1", server.port).then((socket) { | 127 Socket.connect("127.0.0.1", server.port).then((socket) { |
128 void sendRequest() { | 128 void sendRequest() { |
129 socket.addString("GET / HTTP/1.0\r\n"); | 129 socket.write("GET / HTTP/1.0\r\n"); |
130 socket.addString("Connection: Keep-Alive\r\n\r\n"); | 130 socket.write("Connection: Keep-Alive\r\n\r\n"); |
131 } | 131 } |
132 | 132 |
133 List<int> response = []; | 133 List<int> response = []; |
134 int count = 0; | 134 int count = 0; |
135 socket.listen( | 135 socket.listen( |
136 (d) { | 136 (d) { |
137 response.addAll(d); | 137 response.addAll(d); |
138 if (response[response.length - 1] == "Z".codeUnitAt(0)) { | 138 if (response[response.length - 1] == "Z".codeUnitAt(0)) { |
139 String s = new String.fromCharCodes(response).toLowerCase(); | 139 String s = new String.fromCharCodes(response).toLowerCase(); |
140 Expect.isTrue(s.indexOf("\r\nconnection: keep-alive\r\n") > 0); | 140 Expect.isTrue(s.indexOf("\r\nconnection: keep-alive\r\n") > 0); |
(...skipping 21 matching lines...) Expand all Loading... |
162 // server does not set a content length so it cannot honor connection | 162 // server does not set a content length so it cannot honor connection |
163 // keep alive. | 163 // keep alive. |
164 void testHttp10KeepAliveServerCloses() { | 164 void testHttp10KeepAliveServerCloses() { |
165 HttpServer.bind().then((server) { | 165 HttpServer.bind().then((server) { |
166 server.listen( | 166 server.listen( |
167 (HttpRequest request) { | 167 (HttpRequest request) { |
168 Expect.isNull(request.headers.value('content-length')); | 168 Expect.isNull(request.headers.value('content-length')); |
169 Expect.equals(-1, request.contentLength); | 169 Expect.equals(-1, request.contentLength); |
170 var response = request.response; | 170 var response = request.response; |
171 Expect.equals("1.0", request.protocolVersion); | 171 Expect.equals("1.0", request.protocolVersion); |
172 response.addString("Z"); | 172 response.write("Z"); |
173 response.close(); | 173 response.close(); |
174 }, | 174 }, |
175 onError: (e) => Expect.fail("Unexpected error $e")); | 175 onError: (e) => Expect.fail("Unexpected error $e")); |
176 | 176 |
177 int count = 0; | 177 int count = 0; |
178 makeRequest() { | 178 makeRequest() { |
179 Socket.connect("127.0.0.1", server.port).then((socket) { | 179 Socket.connect("127.0.0.1", server.port).then((socket) { |
180 socket.addString("GET / HTTP/1.0\r\n"); | 180 socket.write("GET / HTTP/1.0\r\n"); |
181 socket.addString("Connection: Keep-Alive\r\n\r\n"); | 181 socket.write("Connection: Keep-Alive\r\n\r\n"); |
182 | 182 |
183 List<int> response = []; | 183 List<int> response = []; |
184 socket.listen( | 184 socket.listen( |
185 response.addAll, | 185 response.addAll, |
186 onDone: () { | 186 onDone: () { |
187 socket.destroy(); | 187 socket.destroy(); |
188 count++; | 188 count++; |
189 String s = new String.fromCharCodes(response).toLowerCase(); | 189 String s = new String.fromCharCodes(response).toLowerCase(); |
190 Expect.equals("z", s[s.length - 1]); | 190 Expect.equals("z", s[s.length - 1]); |
191 Expect.equals(-1, s.indexOf("content-length")); | 191 Expect.equals(-1, s.indexOf("content-length")); |
(...skipping 11 matching lines...) Expand all Loading... |
203 } | 203 } |
204 | 204 |
205 | 205 |
206 void main() { | 206 void main() { |
207 testHttp10NoKeepAlive(); | 207 testHttp10NoKeepAlive(); |
208 // TODO(8871): This test fails with short socket writes. | 208 // TODO(8871): This test fails with short socket writes. |
209 //testHttp10ServerClose(); | 209 //testHttp10ServerClose(); |
210 testHttp10KeepAlive(); | 210 testHttp10KeepAlive(); |
211 testHttp10KeepAliveServerCloses(); | 211 testHttp10KeepAliveServerCloses(); |
212 } | 212 } |
OLD | NEW |