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

Side by Side Diff: tests/standalone/io/http_basic_test.dart

Issue 12316036: Merge IO v2 branch to bleeding edge (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 10 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
OLDNEW
1 // Copyright (c) 2012, 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 "dart:isolate"; 10 import "dart:isolate";
11 import "dart:io"; 11 import "dart:io";
(...skipping 15 matching lines...) Expand all
27 if (status.isStarted) { 27 if (status.isStarted) {
28 _startedCallback(status.port); 28 _startedCallback(status.port);
29 } 29 }
30 }); 30 });
31 31
32 // Send server start message to the server. 32 // Send server start message to the server.
33 var command = new TestServerCommand.start(); 33 var command = new TestServerCommand.start();
34 _serverPort.send(command, _statusPort.toSendPort()); 34 _serverPort.send(command, _statusPort.toSendPort());
35 } 35 }
36 36
37 void shutdown() { 37 void close() {
38 // Send server stop message to the server. 38 // Send server stop message to the server.
39 _serverPort.send(new TestServerCommand.stop(), _statusPort.toSendPort()); 39 _serverPort.send(new TestServerCommand.stop(), _statusPort.toSendPort());
40 _statusPort.close(); 40 _statusPort.close();
41 } 41 }
42 42
43 void chunkedEncoding() { 43 void chunkedEncoding() {
44 // Send chunked encoding message to the server. 44 // Send chunked encoding message to the server.
45 _serverPort.send( 45 _serverPort.send(
46 new TestServerCommand.chunkedEncoding(), _statusPort.toSendPort()); 46 new TestServerCommand.chunkedEncoding(), _statusPort.toSendPort());
47 } 47 }
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 91
92 void startTestServer() { 92 void startTestServer() {
93 var server = new TestServer(); 93 var server = new TestServer();
94 server.init(); 94 server.init();
95 port.receive(server.dispatch); 95 port.receive(server.dispatch);
96 } 96 }
97 97
98 98
99 class TestServer { 99 class TestServer {
100 // Echo the request content back to the response. 100 // Echo the request content back to the response.
101 void _echoHandler(HttpRequest request, HttpResponse response) { 101 void _echoHandler(HttpRequest request) {
102 var response = request.response;
102 Expect.equals("POST", request.method); 103 Expect.equals("POST", request.method);
103 response.contentLength = request.contentLength; 104 response.contentLength = request.contentLength;
104 request.inputStream.pipe(response.outputStream); 105 request.pipe(response);
105 } 106 }
106 107
107 // Echo the request content back to the response. 108 // Echo the request content back to the response.
108 void _zeroToTenHandler(HttpRequest request, HttpResponse response) { 109 void _zeroToTenHandler(HttpRequest request) {
110 var response = request.response;
109 Expect.equals("GET", request.method); 111 Expect.equals("GET", request.method);
110 request.inputStream.onData = () {}; 112 request.listen((_) {}, onDone: () {
111 request.inputStream.onClosed = () { 113 response.addString("01234567890");
112 response.outputStream.writeString("01234567890"); 114 response.close();
113 response.outputStream.close(); 115 });
114 };
115 } 116 }
116 117
117 // Return a 404. 118 // Return a 404.
118 void _notFoundHandler(HttpRequest request, HttpResponse response) { 119 void _notFoundHandler(HttpRequest request) {
120 var response = request.response;
119 response.statusCode = HttpStatus.NOT_FOUND; 121 response.statusCode = HttpStatus.NOT_FOUND;
120 response.headers.set("Content-Type", "text/html; charset=UTF-8"); 122 response.headers.set("Content-Type", "text/html; charset=UTF-8");
121 response.outputStream.writeString("Page not found"); 123 response.addString("Page not found");
122 response.outputStream.close(); 124 response.close();
123 } 125 }
124 126
125 // Return a 301 with a custom reason phrase. 127 // Return a 301 with a custom reason phrase.
126 void _reasonForMovingHandler(HttpRequest request, HttpResponse response) { 128 void _reasonForMovingHandler(HttpRequest request) {
129 var response = request.response;
127 response.statusCode = HttpStatus.MOVED_PERMANENTLY; 130 response.statusCode = HttpStatus.MOVED_PERMANENTLY;
128 response.reasonPhrase = "Don't come looking here any more"; 131 response.reasonPhrase = "Don't come looking here any more";
129 response.outputStream.close(); 132 response.close();
130 } 133 }
131 134
132 // Check the "Host" header. 135 // Check the "Host" header.
133 void _hostHandler(HttpRequest request, HttpResponse response) { 136 void _hostHandler(HttpRequest request) {
137 var response = request.response;
134 Expect.equals(1, request.headers["Host"].length); 138 Expect.equals(1, request.headers["Host"].length);
135 Expect.equals("www.dartlang.org:1234", request.headers["Host"][0]); 139 Expect.equals("www.dartlang.org:1234", request.headers["Host"][0]);
136 Expect.equals("www.dartlang.org", request.headers.host); 140 Expect.equals("www.dartlang.org", request.headers.host);
137 Expect.equals(1234, request.headers.port); 141 Expect.equals(1234, request.headers.port);
138 response.statusCode = HttpStatus.OK; 142 response.statusCode = HttpStatus.OK;
139 response.outputStream.close(); 143 response.close();
140 } 144 }
141 145
142 void init() { 146 void init() {
143 // Setup request handlers. 147 // Setup request handlers.
144 _requestHandlers = new Map(); 148 _requestHandlers = new Map();
145 _requestHandlers["/echo"] = _echoHandler; 149 _requestHandlers["/echo"] = _echoHandler;
146 _requestHandlers["/0123456789"] = _zeroToTenHandler; 150 _requestHandlers["/0123456789"] = _zeroToTenHandler;
147 _requestHandlers["/reasonformoving"] = _reasonForMovingHandler; 151 _requestHandlers["/reasonformoving"] = _reasonForMovingHandler;
148 _requestHandlers["/host"] = _hostHandler; 152 _requestHandlers["/host"] = _hostHandler;
149 } 153 }
150 154
151 void dispatch(var message, SendPort replyTo) { 155 void dispatch(var message, SendPort replyTo) {
152 if (message.isStart) { 156 if (message.isStart) {
153 _server = new HttpServer();
154 try { 157 try {
155 _server.listen("127.0.0.1", 0); 158 HttpServer.bind().then((server) {
156 _server.defaultRequestHandler = _requestReceivedHandler; 159 _server = server;
157 replyTo.send(new TestServerStatus.started(_server.port), null); 160 _server.listen(_requestReceivedHandler);
161 replyTo.send(new TestServerStatus.started(_server.port), null);
162 });
158 } catch (e) { 163 } catch (e) {
159 replyTo.send(new TestServerStatus.error(), null); 164 replyTo.send(new TestServerStatus.error(), null);
160 } 165 }
161 } else if (message.isStop) { 166 } else if (message.isStop) {
162 _server.close(); 167 _server.close();
163 port.close(); 168 port.close();
164 replyTo.send(new TestServerStatus.stopped(), null); 169 replyTo.send(new TestServerStatus.stopped(), null);
165 } else if (message.isChunkedEncoding) { 170 } else if (message.isChunkedEncoding) {
166 _chunkedEncoding = true; 171 _chunkedEncoding = true;
167 } 172 }
168 } 173 }
169 174
170 void _requestReceivedHandler(HttpRequest request, HttpResponse response) { 175 void _requestReceivedHandler(HttpRequest request) {
171 var requestHandler =_requestHandlers[request.path]; 176 var requestHandler =_requestHandlers[request.uri.path];
172 if (requestHandler != null) { 177 if (requestHandler != null) {
173 requestHandler(request, response); 178 requestHandler(request);
174 } else { 179 } else {
175 _notFoundHandler(request, response); 180 _notFoundHandler(request);
176 } 181 }
177 } 182 }
178 183
179 HttpServer _server; // HTTP server instance. 184 HttpServer _server; // HTTP server instance.
180 Map _requestHandlers; 185 Map _requestHandlers;
181 bool _chunkedEncoding = false; 186 bool _chunkedEncoding = false;
182 } 187 }
183 188
184 void testStartStop() { 189 void testStartStop() {
185 TestServerMain testServerMain = new TestServerMain(); 190 TestServerMain testServerMain = new TestServerMain();
186 testServerMain.setServerStartedHandler((int port) { 191 testServerMain.setServerStartedHandler((int port) {
187 testServerMain.shutdown(); 192 testServerMain.close();
188 }); 193 });
189 testServerMain.start(); 194 testServerMain.start();
190 } 195 }
191 196
192 void testGET() { 197 void testGET() {
193 TestServerMain testServerMain = new TestServerMain(); 198 TestServerMain testServerMain = new TestServerMain();
194 testServerMain.setServerStartedHandler((int port) { 199 testServerMain.setServerStartedHandler((int port) {
195 HttpClient httpClient = new HttpClient(); 200 HttpClient httpClient = new HttpClient();
196 HttpClientConnection conn = 201 httpClient.get("127.0.0.1", port, "/0123456789")
197 httpClient.get("127.0.0.1", port, "/0123456789"); 202 .then((request) => request.close())
198 conn.onResponse = (HttpClientResponse response) { 203 .then((response) {
199 Expect.equals(HttpStatus.OK, response.statusCode); 204 Expect.equals(HttpStatus.OK, response.statusCode);
200 StringInputStream stream = new StringInputStream(response.inputStream); 205 StringBuffer body = new StringBuffer();
201 StringBuffer body = new StringBuffer(); 206 response.listen(
202 stream.onData = () => body.add(stream.read()); 207 (data) => body.add(new String.fromCharCodes(data)),
203 stream.onClosed = () { 208 onDone: () {
204 Expect.equals("01234567890", body.toString()); 209 Expect.equals("01234567890", body.toString());
205 httpClient.shutdown(); 210 httpClient.close();
206 testServerMain.shutdown(); 211 testServerMain.close();
207 }; 212 });
208 }; 213 });
209 }); 214 });
210 testServerMain.start(); 215 testServerMain.start();
211 } 216 }
212 217
213 void testPOST(bool chunkedEncoding) { 218 void testPOST(bool chunkedEncoding) {
214 String data = "ABCDEFGHIJKLMONPQRSTUVWXYZ"; 219 String data = "ABCDEFGHIJKLMONPQRSTUVWXYZ";
215 final int kMessageCount = 10; 220 final int kMessageCount = 10;
216 221
217 TestServerMain testServerMain = new TestServerMain(); 222 TestServerMain testServerMain = new TestServerMain();
218 223
219 void runTest(int port) { 224 void runTest(int port) {
220 int count = 0; 225 int count = 0;
221 HttpClient httpClient = new HttpClient(); 226 HttpClient httpClient = new HttpClient();
222 void sendRequest() { 227 void sendRequest() {
223 HttpClientConnection conn = 228 httpClient.post("127.0.0.1", port, "/echo")
224 httpClient.post("127.0.0.1", port, "/echo"); 229 .then((request) {
225 conn.onRequest = (HttpClientRequest request) { 230 if (chunkedEncoding) {
226 if (chunkedEncoding) { 231 request.addString(data.substring(0, 10));
227 request.outputStream.writeString(data.substring(0, 10)); 232 request.addString(data.substring(10, data.length));
228 request.outputStream.writeString(data.substring(10, data.length)); 233 } else {
229 } else { 234 request.contentLength = data.length;
230 request.contentLength = data.length; 235 request.addString(data);
231 request.outputStream.write(data.charCodes); 236 }
232 } 237 return request.close();
233 request.outputStream.close(); 238 })
234 }; 239 .then((response) {
235 conn.onResponse = (HttpClientResponse response) { 240 Expect.equals(HttpStatus.OK, response.statusCode);
236 Expect.equals(HttpStatus.OK, response.statusCode); 241 StringBuffer body = new StringBuffer();
237 StringInputStream stream = new StringInputStream(response.inputStream); 242 response.listen(
238 StringBuffer body = new StringBuffer(); 243 (data) => body.add(new String.fromCharCodes(data)),
239 stream.onData = () => body.add(stream.read()); 244 onDone: () {
240 stream.onClosed = () { 245 Expect.equals(data, body.toString());
241 Expect.equals(data, body.toString()); 246 count++;
242 count++; 247 if (count < kMessageCount) {
243 if (count < kMessageCount) { 248 sendRequest();
244 sendRequest(); 249 } else {
245 } else { 250 httpClient.close();
246 httpClient.shutdown(); 251 testServerMain.close();
247 testServerMain.shutdown(); 252 }
248 } 253 });
249 }; 254 });
250 };
251 } 255 }
252 256
253 sendRequest(); 257 sendRequest();
254 } 258 }
255 259
256 testServerMain.setServerStartedHandler(runTest); 260 testServerMain.setServerStartedHandler(runTest);
257 if (chunkedEncoding) { 261 if (chunkedEncoding) {
258 testServerMain.chunkedEncoding(); 262 testServerMain.chunkedEncoding();
259 } 263 }
260 testServerMain.start(); 264 testServerMain.start();
261 } 265 }
262 266
263 void test404() { 267 void test404() {
264 TestServerMain testServerMain = new TestServerMain(); 268 TestServerMain testServerMain = new TestServerMain();
265 testServerMain.setServerStartedHandler((int port) { 269 testServerMain.setServerStartedHandler((int port) {
266 HttpClient httpClient = new HttpClient(); 270 HttpClient httpClient = new HttpClient();
267 HttpClientConnection conn = 271 httpClient.get("127.0.0.1", port, "/thisisnotfound")
268 httpClient.get("127.0.0.1", port, "/thisisnotfound"); 272 .then((request) => request.close())
269 conn.onResponse = (HttpClientResponse response) { 273 .then((response) {
270 Expect.equals(HttpStatus.NOT_FOUND, response.statusCode); 274 Expect.equals(HttpStatus.NOT_FOUND, response.statusCode);
271 var body = new StringBuffer(); 275 var body = new StringBuffer();
272 var stream = response.inputStream; 276 response.listen(
273 stream.onData = () => body.add(new String.fromCharCodes(stream.read())); 277 (data) => body.add(new String.fromCharCodes(data)),
274 stream.onClosed = () { 278 onDone: () {
275 Expect.equals("Page not found", body.toString()); 279 Expect.equals("Page not found", body.toString());
276 httpClient.shutdown(); 280 httpClient.close();
277 testServerMain.shutdown(); 281 testServerMain.close();
278 }; 282 });
279 }; 283 });
280 }); 284 });
281 testServerMain.start(); 285 testServerMain.start();
282 } 286 }
283 287
284 void testReasonPhrase() { 288 void testReasonPhrase() {
285 TestServerMain testServerMain = new TestServerMain(); 289 TestServerMain testServerMain = new TestServerMain();
286 testServerMain.setServerStartedHandler((int port) { 290 testServerMain.setServerStartedHandler((int port) {
287 HttpClient httpClient = new HttpClient(); 291 HttpClient httpClient = new HttpClient();
288 HttpClientConnection conn = 292 httpClient.get("127.0.0.1", port, "/reasonformoving")
289 httpClient.get("127.0.0.1", port, "/reasonformoving"); 293 .then((request) {
290 conn.followRedirects = false; 294 request.followRedirects = false;
291 conn.onResponse = (HttpClientResponse response) { 295 return request.close();
292 Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode); 296 })
293 Expect.equals("Don't come looking here any more", response.reasonPhrase); 297 .then((response) {
294 var stream = response.inputStream; 298 Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode);
295 stream.onData = () => Expect.fail("No data expected"); 299 Expect.equals("Don't come looking here any more",
296 stream.onClosed = () { 300 response.reasonPhrase);
297 httpClient.shutdown(); 301 response.listen(
298 testServerMain.shutdown(); 302 (data) => Expect.fail("No data expected"),
299 }; 303 onDone: () {
300 }; 304 httpClient.close();
305 testServerMain.close();
306 });
307 });
301 }); 308 });
302 testServerMain.start(); 309 testServerMain.start();
303 } 310 }
304 311
305 void main() { 312 void main() {
306 testStartStop(); 313 testStartStop();
307 testGET(); 314 testGET();
308 testPOST(true); 315 testPOST(true);
309 testPOST(false); 316 testPOST(false);
310 test404(); 317 test404();
311 testReasonPhrase(); 318 testReasonPhrase();
312 } 319 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698