OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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"); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 // Echo the request content back to the response. | 95 // Echo the request content back to the response. |
96 void _echoHandler(HttpRequest request, HttpResponse response) { | 96 void _echoHandler(HttpRequest request, HttpResponse response) { |
97 Expect.equals("POST", request.method); | 97 Expect.equals("POST", request.method); |
98 response.contentLength = request.contentLength; | 98 response.contentLength = request.contentLength; |
99 request.inputStream.pipe(response.outputStream); | 99 request.inputStream.pipe(response.outputStream); |
100 } | 100 } |
101 | 101 |
102 // Echo the request content back to the response. | 102 // Echo the request content back to the response. |
103 void _zeroToTenHandler(HttpRequest request, HttpResponse response) { | 103 void _zeroToTenHandler(HttpRequest request, HttpResponse response) { |
104 Expect.equals("GET", request.method); | 104 Expect.equals("GET", request.method); |
105 request.inputStream.dataHandler = () {}; | 105 request.inputStream.onData = () {}; |
106 request.inputStream.closeHandler = () { | 106 request.inputStream.onClosed = () { |
107 response.writeString("01234567890"); | 107 response.writeString("01234567890"); |
108 response.outputStream.close(); | 108 response.outputStream.close(); |
109 }; | 109 }; |
110 } | 110 } |
111 | 111 |
112 // Return a 404. | 112 // Return a 404. |
113 void _notFoundHandler(HttpRequest request, HttpResponse response) { | 113 void _notFoundHandler(HttpRequest request, HttpResponse response) { |
114 response.statusCode = HttpStatus.NOT_FOUND; | 114 response.statusCode = HttpStatus.NOT_FOUND; |
115 response.setHeader("Content-Type", "text/html; charset=UTF-8"); | 115 response.setHeader("Content-Type", "text/html; charset=UTF-8"); |
116 response.writeString("Page not found"); | 116 response.writeString("Page not found"); |
(...skipping 20 matching lines...) Expand all Loading... |
137 _requestHandlers["/reasonformoving"] = | 137 _requestHandlers["/reasonformoving"] = |
138 (HttpRequest request, HttpResponse response) { | 138 (HttpRequest request, HttpResponse response) { |
139 _reasonForMovingHandler(request, response); | 139 _reasonForMovingHandler(request, response); |
140 }; | 140 }; |
141 | 141 |
142 this.port.receive((var message, SendPort replyTo) { | 142 this.port.receive((var message, SendPort replyTo) { |
143 if (message.isStart) { | 143 if (message.isStart) { |
144 _server = new HttpServer(); | 144 _server = new HttpServer(); |
145 try { | 145 try { |
146 _server.listen("127.0.0.1", 0); | 146 _server.listen("127.0.0.1", 0); |
147 _server.requestHandler = (HttpRequest req, HttpResponse rsp) { | 147 _server.onRequest = (HttpRequest req, HttpResponse rsp) { |
148 _requestReceivedHandler(req, rsp); | 148 _requestReceivedHandler(req, rsp); |
149 }; | 149 }; |
150 replyTo.send(new TestServerStatus.started(_server.port), null); | 150 replyTo.send(new TestServerStatus.started(_server.port), null); |
151 } catch (var e) { | 151 } catch (var e) { |
152 replyTo.send(new TestServerStatus.error(), null); | 152 replyTo.send(new TestServerStatus.error(), null); |
153 } | 153 } |
154 } else if (message.isStop) { | 154 } else if (message.isStop) { |
155 _server.close(); | 155 _server.close(); |
156 this.port.close(); | 156 this.port.close(); |
157 replyTo.send(new TestServerStatus.stopped(), null); | 157 replyTo.send(new TestServerStatus.stopped(), null); |
(...skipping 26 matching lines...) Expand all Loading... |
184 testServerMain.start(); | 184 testServerMain.start(); |
185 } | 185 } |
186 | 186 |
187 | 187 |
188 void testGET() { | 188 void testGET() { |
189 TestServerMain testServerMain = new TestServerMain(); | 189 TestServerMain testServerMain = new TestServerMain(); |
190 testServerMain.setServerStartedHandler((int port) { | 190 testServerMain.setServerStartedHandler((int port) { |
191 HttpClient httpClient = new HttpClient(); | 191 HttpClient httpClient = new HttpClient(); |
192 HttpClientConnection conn = | 192 HttpClientConnection conn = |
193 httpClient.get("127.0.0.1", port, "/0123456789"); | 193 httpClient.get("127.0.0.1", port, "/0123456789"); |
194 conn.responseHandler = (HttpClientResponse response) { | 194 conn.onResponse = (HttpClientResponse response) { |
195 Expect.equals(HttpStatus.OK, response.statusCode); | 195 Expect.equals(HttpStatus.OK, response.statusCode); |
196 StringInputStream stream = new StringInputStream(response.inputStream); | 196 StringInputStream stream = new StringInputStream(response.inputStream); |
197 StringBuffer body = new StringBuffer(); | 197 StringBuffer body = new StringBuffer(); |
198 stream.dataHandler = () => body.add(stream.read()); | 198 stream.onData = () => body.add(stream.read()); |
199 stream.closeHandler = () { | 199 stream.onClosed = () { |
200 Expect.equals("01234567890", body.toString()); | 200 Expect.equals("01234567890", body.toString()); |
201 httpClient.shutdown(); | 201 httpClient.shutdown(); |
202 testServerMain.shutdown(); | 202 testServerMain.shutdown(); |
203 }; | 203 }; |
204 }; | 204 }; |
205 }); | 205 }); |
206 testServerMain.start(); | 206 testServerMain.start(); |
207 } | 207 } |
208 | 208 |
209 | 209 |
210 void testPOST(bool chunkedEncoding) { | 210 void testPOST(bool chunkedEncoding) { |
211 String data = "ABCDEFGHIJKLMONPQRSTUVWXYZ"; | 211 String data = "ABCDEFGHIJKLMONPQRSTUVWXYZ"; |
212 final int kMessageCount = 10; | 212 final int kMessageCount = 10; |
213 | 213 |
214 TestServerMain testServerMain = new TestServerMain(); | 214 TestServerMain testServerMain = new TestServerMain(); |
215 | 215 |
216 void runTest(int port) { | 216 void runTest(int port) { |
217 int count = 0; | 217 int count = 0; |
218 HttpClient httpClient = new HttpClient(); | 218 HttpClient httpClient = new HttpClient(); |
219 void sendRequest() { | 219 void sendRequest() { |
220 HttpClientConnection conn = | 220 HttpClientConnection conn = |
221 httpClient.post("127.0.0.1", port, "/echo"); | 221 httpClient.post("127.0.0.1", port, "/echo"); |
222 conn.requestHandler = (HttpClientRequest request) { | 222 conn.onRequest = (HttpClientRequest request) { |
223 if (chunkedEncoding) { | 223 if (chunkedEncoding) { |
224 request.writeString(data.substring(0, 10)); | 224 request.writeString(data.substring(0, 10)); |
225 request.writeString(data.substring(10, data.length)); | 225 request.writeString(data.substring(10, data.length)); |
226 } else { | 226 } else { |
227 request.contentLength = data.length; | 227 request.contentLength = data.length; |
228 request.outputStream.write(data.charCodes()); | 228 request.outputStream.write(data.charCodes()); |
229 } | 229 } |
230 request.outputStream.close(); | 230 request.outputStream.close(); |
231 }; | 231 }; |
232 conn.responseHandler = (HttpClientResponse response) { | 232 conn.onResponse = (HttpClientResponse response) { |
233 Expect.equals(HttpStatus.OK, response.statusCode); | 233 Expect.equals(HttpStatus.OK, response.statusCode); |
234 StringInputStream stream = new StringInputStream(response.inputStream); | 234 StringInputStream stream = new StringInputStream(response.inputStream); |
235 StringBuffer body = new StringBuffer(); | 235 StringBuffer body = new StringBuffer(); |
236 stream.dataHandler = () => body.add(stream.read()); | 236 stream.onData = () => body.add(stream.read()); |
237 stream.closeHandler = () { | 237 stream.onClosed = () { |
238 Expect.equals(data, body.toString()); | 238 Expect.equals(data, body.toString()); |
239 count++; | 239 count++; |
240 if (count < kMessageCount) { | 240 if (count < kMessageCount) { |
241 sendRequest(); | 241 sendRequest(); |
242 } else { | 242 } else { |
243 httpClient.shutdown(); | 243 httpClient.shutdown(); |
244 testServerMain.shutdown(); | 244 testServerMain.shutdown(); |
245 } | 245 } |
246 }; | 246 }; |
247 }; | 247 }; |
(...skipping 15 matching lines...) Expand all Loading... |
263 final int kMessageCount = 10; | 263 final int kMessageCount = 10; |
264 | 264 |
265 TestServerMain testServerMain = new TestServerMain(); | 265 TestServerMain testServerMain = new TestServerMain(); |
266 | 266 |
267 void runTest(int port) { | 267 void runTest(int port) { |
268 int count = 0; | 268 int count = 0; |
269 HttpClient httpClient = new HttpClient(); | 269 HttpClient httpClient = new HttpClient(); |
270 void sendRequest() { | 270 void sendRequest() { |
271 HttpClientConnection conn = | 271 HttpClientConnection conn = |
272 httpClient.post("127.0.0.1", port, "/echo"); | 272 httpClient.post("127.0.0.1", port, "/echo"); |
273 conn.requestHandler = (HttpClientRequest request) { | 273 conn.onRequest = (HttpClientRequest request) { |
274 if (chunkedEncoding) { | 274 if (chunkedEncoding) { |
275 request.writeString(data.substring(0, 10)); | 275 request.writeString(data.substring(0, 10)); |
276 request.writeString(data.substring(10, data.length)); | 276 request.writeString(data.substring(10, data.length)); |
277 } else { | 277 } else { |
278 request.contentLength = data.length; | 278 request.contentLength = data.length; |
279 request.outputStream.write(data.charCodes()); | 279 request.outputStream.write(data.charCodes()); |
280 } | 280 } |
281 request.outputStream.close(); | 281 request.outputStream.close(); |
282 }; | 282 }; |
283 conn.responseHandler = (HttpClientResponse response) { | 283 conn.onResponse = (HttpClientResponse response) { |
284 Expect.equals(HttpStatus.OK, response.statusCode); | 284 Expect.equals(HttpStatus.OK, response.statusCode); |
285 InputStream stream = response.inputStream; | 285 InputStream stream = response.inputStream; |
286 List<int> body = new List<int>(); | 286 List<int> body = new List<int>(); |
287 stream.dataHandler = () { | 287 stream.onData = () { |
288 List tmp = new List(3); | 288 List tmp = new List(3); |
289 int bytes = stream.readInto(tmp); | 289 int bytes = stream.readInto(tmp); |
290 body.addAll(tmp.getRange(0, bytes)); | 290 body.addAll(tmp.getRange(0, bytes)); |
291 }; | 291 }; |
292 stream.closeHandler = () { | 292 stream.onClosed = () { |
293 Expect.equals(data, new String.fromCharCodes(body)); | 293 Expect.equals(data, new String.fromCharCodes(body)); |
294 count++; | 294 count++; |
295 if (count < kMessageCount) { | 295 if (count < kMessageCount) { |
296 sendRequest(); | 296 sendRequest(); |
297 } else { | 297 } else { |
298 httpClient.shutdown(); | 298 httpClient.shutdown(); |
299 testServerMain.shutdown(); | 299 testServerMain.shutdown(); |
300 } | 300 } |
301 }; | 301 }; |
302 }; | 302 }; |
(...skipping 15 matching lines...) Expand all Loading... |
318 final int kMessageCount = 10; | 318 final int kMessageCount = 10; |
319 | 319 |
320 TestServerMain testServerMain = new TestServerMain(); | 320 TestServerMain testServerMain = new TestServerMain(); |
321 | 321 |
322 void runTest(int port) { | 322 void runTest(int port) { |
323 int count = 0; | 323 int count = 0; |
324 HttpClient httpClient = new HttpClient(); | 324 HttpClient httpClient = new HttpClient(); |
325 void sendRequest() { | 325 void sendRequest() { |
326 HttpClientConnection conn = | 326 HttpClientConnection conn = |
327 httpClient.post("127.0.0.1", port, "/echo"); | 327 httpClient.post("127.0.0.1", port, "/echo"); |
328 conn.requestHandler = (HttpClientRequest request) { | 328 conn.onRequest = (HttpClientRequest request) { |
329 if (chunkedEncoding) { | 329 if (chunkedEncoding) { |
330 request.writeString(data.substring(0, 10)); | 330 request.writeString(data.substring(0, 10)); |
331 request.writeString(data.substring(10, data.length)); | 331 request.writeString(data.substring(10, data.length)); |
332 } else { | 332 } else { |
333 request.contentLength = data.length; | 333 request.contentLength = data.length; |
334 request.outputStream.write(data.charCodes()); | 334 request.outputStream.write(data.charCodes()); |
335 } | 335 } |
336 request.outputStream.close(); | 336 request.outputStream.close(); |
337 }; | 337 }; |
338 conn.responseHandler = (HttpClientResponse response) { | 338 conn.onResponse = (HttpClientResponse response) { |
339 Expect.equals(HttpStatus.OK, response.statusCode); | 339 Expect.equals(HttpStatus.OK, response.statusCode); |
340 InputStream stream = response.inputStream; | 340 InputStream stream = response.inputStream; |
341 List<int> body = new List<int>(); | 341 List<int> body = new List<int>(); |
342 stream.dataHandler = () { | 342 stream.onData = () { |
343 List tmp = stream.read(2); | 343 List tmp = stream.read(2); |
344 body.addAll(tmp); | 344 body.addAll(tmp); |
345 }; | 345 }; |
346 stream.closeHandler = () { | 346 stream.onClosed = () { |
347 Expect.equals(data, new String.fromCharCodes(body)); | 347 Expect.equals(data, new String.fromCharCodes(body)); |
348 count++; | 348 count++; |
349 if (count < kMessageCount) { | 349 if (count < kMessageCount) { |
350 sendRequest(); | 350 sendRequest(); |
351 } else { | 351 } else { |
352 httpClient.shutdown(); | 352 httpClient.shutdown(); |
353 testServerMain.shutdown(); | 353 testServerMain.shutdown(); |
354 } | 354 } |
355 }; | 355 }; |
356 }; | 356 }; |
357 } | 357 } |
358 | 358 |
359 sendRequest(); | 359 sendRequest(); |
360 } | 360 } |
361 | 361 |
362 testServerMain.setServerStartedHandler(runTest); | 362 testServerMain.setServerStartedHandler(runTest); |
363 if (chunkedEncoding) { | 363 if (chunkedEncoding) { |
364 testServerMain.chunkedEncoding(); | 364 testServerMain.chunkedEncoding(); |
365 } | 365 } |
366 testServerMain.start(); | 366 testServerMain.start(); |
367 } | 367 } |
368 | 368 |
369 | 369 |
370 void test404() { | 370 void test404() { |
371 TestServerMain testServerMain = new TestServerMain(); | 371 TestServerMain testServerMain = new TestServerMain(); |
372 testServerMain.setServerStartedHandler((int port) { | 372 testServerMain.setServerStartedHandler((int port) { |
373 HttpClient httpClient = new HttpClient(); | 373 HttpClient httpClient = new HttpClient(); |
374 HttpClientConnection conn = | 374 HttpClientConnection conn = |
375 httpClient.get("127.0.0.1", port, "/thisisnotfound"); | 375 httpClient.get("127.0.0.1", port, "/thisisnotfound"); |
376 conn.responseHandler = (HttpClientResponse response) { | 376 conn.onResponse = (HttpClientResponse response) { |
377 Expect.equals(HttpStatus.NOT_FOUND, response.statusCode); | 377 Expect.equals(HttpStatus.NOT_FOUND, response.statusCode); |
378 httpClient.shutdown(); | 378 httpClient.shutdown(); |
379 testServerMain.shutdown(); | 379 testServerMain.shutdown(); |
380 }; | 380 }; |
381 }); | 381 }); |
382 testServerMain.start(); | 382 testServerMain.start(); |
383 } | 383 } |
384 | 384 |
385 | 385 |
386 void testReasonPhrase() { | 386 void testReasonPhrase() { |
387 TestServerMain testServerMain = new TestServerMain(); | 387 TestServerMain testServerMain = new TestServerMain(); |
388 testServerMain.setServerStartedHandler((int port) { | 388 testServerMain.setServerStartedHandler((int port) { |
389 HttpClient httpClient = new HttpClient(); | 389 HttpClient httpClient = new HttpClient(); |
390 HttpClientConnection conn = | 390 HttpClientConnection conn = |
391 httpClient.get("127.0.0.1", port, "/reasonformoving"); | 391 httpClient.get("127.0.0.1", port, "/reasonformoving"); |
392 conn.responseHandler = (HttpClientResponse response) { | 392 conn.onResponse = (HttpClientResponse response) { |
393 Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode); | 393 Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode); |
394 Expect.equals("Don't come looking here any more", response.reasonPhrase); | 394 Expect.equals("Don't come looking here any more", response.reasonPhrase); |
395 httpClient.shutdown(); | 395 httpClient.shutdown(); |
396 testServerMain.shutdown(); | 396 testServerMain.shutdown(); |
397 }; | 397 }; |
398 }); | 398 }); |
399 testServerMain.start(); | 399 testServerMain.start(); |
400 } | 400 } |
401 | 401 |
402 | 402 |
403 void main() { | 403 void main() { |
404 testStartStop(); | 404 testStartStop(); |
405 testGET(); | 405 testGET(); |
406 testPOST(true); | 406 testPOST(true); |
407 testPOST(false); | 407 testPOST(false); |
408 testReadInto(true); | 408 testReadInto(true); |
409 testReadInto(false); | 409 testReadInto(false); |
410 testReadShort(true); | 410 testReadShort(true); |
411 testReadShort(false); | 411 testReadShort(false); |
412 test404(); | 412 test404(); |
413 testReasonPhrase(); | 413 testReasonPhrase(); |
414 } | 414 } |
OLD | NEW |