| 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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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"); |
| 117 response.outputStream.close(); | 117 response.outputStream.close(); |
| 118 } | 118 } |
| 119 | 119 |
| 120 // Return a 301 with a custom reason phrase. |
| 121 void _reasonForMovingHandler(HttpRequest request, HttpResponse response) { |
| 122 response.statusCode = HttpStatus.MOVED_PERMANENTLY; |
| 123 response.reasonPhrase = "Don't come looking here any more"; |
| 124 response.outputStream.close(); |
| 125 } |
| 126 |
| 120 void main() { | 127 void main() { |
| 121 // Setup request handlers. | 128 // Setup request handlers. |
| 122 _requestHandlers = new Map(); | 129 _requestHandlers = new Map(); |
| 123 _requestHandlers["/echo"] = (HttpRequest request, HttpResponse response) { | 130 _requestHandlers["/echo"] = (HttpRequest request, HttpResponse response) { |
| 124 _echoHandler(request, response); | 131 _echoHandler(request, response); |
| 125 }; | 132 }; |
| 126 _requestHandlers["/0123456789"] = | 133 _requestHandlers["/0123456789"] = |
| 127 (HttpRequest request, HttpResponse response) { | 134 (HttpRequest request, HttpResponse response) { |
| 128 _zeroToTenHandler(request, response); | 135 _zeroToTenHandler(request, response); |
| 129 }; | 136 }; |
| 137 _requestHandlers["/reasonformoving"] = |
| 138 (HttpRequest request, HttpResponse response) { |
| 139 _reasonForMovingHandler(request, response); |
| 140 }; |
| 130 | 141 |
| 131 this.port.receive((var message, SendPort replyTo) { | 142 this.port.receive((var message, SendPort replyTo) { |
| 132 if (message.isStart) { | 143 if (message.isStart) { |
| 133 _server = new HttpServer(); | 144 _server = new HttpServer(); |
| 134 try { | 145 try { |
| 135 _server.listen("127.0.0.1", 0); | 146 _server.listen("127.0.0.1", 0); |
| 136 _server.requestHandler = (HttpRequest req, HttpResponse rsp) { | 147 _server.requestHandler = (HttpRequest req, HttpResponse rsp) { |
| 137 _requestReceivedHandler(req, rsp); | 148 _requestReceivedHandler(req, rsp); |
| 138 }; | 149 }; |
| 139 replyTo.send(new TestServerStatus.started(_server.port), null); | 150 replyTo.send(new TestServerStatus.started(_server.port), null); |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 conn.responseHandler = (HttpClientResponse response) { | 376 conn.responseHandler = (HttpClientResponse response) { |
| 366 Expect.equals(HttpStatus.NOT_FOUND, response.statusCode); | 377 Expect.equals(HttpStatus.NOT_FOUND, response.statusCode); |
| 367 httpClient.shutdown(); | 378 httpClient.shutdown(); |
| 368 testServerMain.shutdown(); | 379 testServerMain.shutdown(); |
| 369 }; | 380 }; |
| 370 }); | 381 }); |
| 371 testServerMain.start(); | 382 testServerMain.start(); |
| 372 } | 383 } |
| 373 | 384 |
| 374 | 385 |
| 386 void testReasonPhrase() { |
| 387 TestServerMain testServerMain = new TestServerMain(); |
| 388 testServerMain.setServerStartedHandler((int port) { |
| 389 HttpClient httpClient = new HttpClient(); |
| 390 HttpClientConnection conn = |
| 391 httpClient.get("127.0.0.1", port, "/reasonformoving"); |
| 392 conn.responseHandler = (HttpClientResponse response) { |
| 393 Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode); |
| 394 Expect.equals("Don't come looking here any more", response.reasonPhrase); |
| 395 httpClient.shutdown(); |
| 396 testServerMain.shutdown(); |
| 397 }; |
| 398 }); |
| 399 testServerMain.start(); |
| 400 } |
| 401 |
| 402 |
| 375 void main() { | 403 void main() { |
| 376 testStartStop(); | 404 testStartStop(); |
| 377 testGET(); | 405 testGET(); |
| 378 testPOST(true); | 406 testPOST(true); |
| 379 testPOST(false); | 407 testPOST(false); |
| 380 testReadInto(true); | 408 testReadInto(true); |
| 381 testReadInto(false); | 409 testReadInto(false); |
| 382 testReadShort(true); | 410 testReadShort(true); |
| 383 testReadShort(false); | 411 testReadShort(false); |
| 384 test404(); | 412 test404(); |
| 413 testReasonPhrase(); |
| 385 } | 414 } |
| OLD | NEW |