| 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 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 response.outputStream.close(); | 117 response.outputStream.close(); |
| 118 } | 118 } |
| 119 | 119 |
| 120 // Return a 301 with a custom reason phrase. | 120 // Return a 301 with a custom reason phrase. |
| 121 void _reasonForMovingHandler(HttpRequest request, HttpResponse response) { | 121 void _reasonForMovingHandler(HttpRequest request, HttpResponse response) { |
| 122 response.statusCode = HttpStatus.MOVED_PERMANENTLY; | 122 response.statusCode = HttpStatus.MOVED_PERMANENTLY; |
| 123 response.reasonPhrase = "Don't come looking here any more"; | 123 response.reasonPhrase = "Don't come looking here any more"; |
| 124 response.outputStream.close(); | 124 response.outputStream.close(); |
| 125 } | 125 } |
| 126 | 126 |
| 127 // Return a 301 with a custom reason phrase. |
| 128 void _hostHandler(HttpRequest request, HttpResponse response) { |
| 129 Expect.equals("www.dartlang.org:1234", request.headers["host"]); |
| 130 response.statusCode = HttpStatus.OK; |
| 131 response.outputStream.close(); |
| 132 } |
| 133 |
| 127 void main() { | 134 void main() { |
| 128 // Setup request handlers. | 135 // Setup request handlers. |
| 129 _requestHandlers = new Map(); | 136 _requestHandlers = new Map(); |
| 130 _requestHandlers["/echo"] = (HttpRequest request, HttpResponse response) { | 137 _requestHandlers["/echo"] = (HttpRequest request, HttpResponse response) { |
| 131 _echoHandler(request, response); | 138 _echoHandler(request, response); |
| 132 }; | 139 }; |
| 133 _requestHandlers["/0123456789"] = | 140 _requestHandlers["/0123456789"] = |
| 134 (HttpRequest request, HttpResponse response) { | 141 (HttpRequest request, HttpResponse response) { |
| 135 _zeroToTenHandler(request, response); | 142 _zeroToTenHandler(request, response); |
| 136 }; | 143 }; |
| 137 _requestHandlers["/reasonformoving"] = | 144 _requestHandlers["/reasonformoving"] = |
| 138 (HttpRequest request, HttpResponse response) { | 145 (HttpRequest request, HttpResponse response) { |
| 139 _reasonForMovingHandler(request, response); | 146 _reasonForMovingHandler(request, response); |
| 140 }; | 147 }; |
| 148 _requestHandlers["/host"] = |
| 149 (HttpRequest request, HttpResponse response) { |
| 150 _hostHandler(request, response); |
| 151 }; |
| 141 | 152 |
| 142 this.port.receive((var message, SendPort replyTo) { | 153 this.port.receive((var message, SendPort replyTo) { |
| 143 if (message.isStart) { | 154 if (message.isStart) { |
| 144 _server = new HttpServer(); | 155 _server = new HttpServer(); |
| 145 try { | 156 try { |
| 146 _server.listen("127.0.0.1", 0); | 157 _server.listen("127.0.0.1", 0); |
| 147 _server.onRequest = (HttpRequest req, HttpResponse rsp) { | 158 _server.onRequest = (HttpRequest req, HttpResponse rsp) { |
| 148 _requestReceivedHandler(req, rsp); | 159 _requestReceivedHandler(req, rsp); |
| 149 }; | 160 }; |
| 150 replyTo.send(new TestServerStatus.started(_server.port), null); | 161 replyTo.send(new TestServerStatus.started(_server.port), null); |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode); | 404 Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode); |
| 394 Expect.equals("Don't come looking here any more", response.reasonPhrase); | 405 Expect.equals("Don't come looking here any more", response.reasonPhrase); |
| 395 httpClient.shutdown(); | 406 httpClient.shutdown(); |
| 396 testServerMain.shutdown(); | 407 testServerMain.shutdown(); |
| 397 }; | 408 }; |
| 398 }); | 409 }); |
| 399 testServerMain.start(); | 410 testServerMain.start(); |
| 400 } | 411 } |
| 401 | 412 |
| 402 | 413 |
| 414 void testHost() { |
| 415 TestServerMain testServerMain = new TestServerMain(); |
| 416 testServerMain.setServerStartedHandler((int port) { |
| 417 HttpClient httpClient = new HttpClient(); |
| 418 HttpClientConnection conn = |
| 419 httpClient.get("127.0.0.1", port, "/host"); |
| 420 conn.onRequest = (HttpClientRequest request) { |
| 421 Expect.equals("127.0.0.1:$port", request.headers["host"]); |
| 422 request.host = "www.dartlang.com"; |
| 423 Expect.equals("www.dartlang.com:$port", request.headers["host"]); |
| 424 request.port = 1234; |
| 425 Expect.equals("www.dartlang.com:1234", request.headers["host"]); |
| 426 request.port = HttpClient.DEFAULT_HTTP_PORT; |
| 427 Expect.equals("www.dartlang.com", request.headers["host"]); |
| 428 request.setHeader("Host", "www.dartlang.org"); |
| 429 Expect.equals("www.dartlang.org", request.host); |
| 430 Expect.equals(HttpClient.DEFAULT_HTTP_PORT, request.port); |
| 431 request.setHeader("Host", "www.dartlang.org:"); |
| 432 Expect.equals("www.dartlang.org", request.host); |
| 433 Expect.equals(HttpClient.DEFAULT_HTTP_PORT, request.port); |
| 434 request.setHeader("Host", "www.dartlang.org:1234"); |
| 435 Expect.equals("www.dartlang.org", request.host); |
| 436 Expect.equals(1234, request.port); |
| 437 request.outputStream.close(); |
| 438 }; |
| 439 conn.onResponse = (HttpClientResponse response) { |
| 440 Expect.equals(HttpStatus.OK, response.statusCode); |
| 441 httpClient.shutdown(); |
| 442 testServerMain.shutdown(); |
| 443 }; |
| 444 }); |
| 445 testServerMain.start(); |
| 446 } |
| 447 |
| 448 |
| 403 void main() { | 449 void main() { |
| 404 testStartStop(); | 450 testStartStop(); |
| 405 testGET(); | 451 testGET(); |
| 406 testPOST(true); | 452 testPOST(true); |
| 407 testPOST(false); | 453 testPOST(false); |
| 408 testReadInto(true); | 454 testReadInto(true); |
| 409 testReadInto(false); | 455 testReadInto(false); |
| 410 testReadShort(true); | 456 testReadShort(true); |
| 411 testReadShort(false); | 457 testReadShort(false); |
| 412 test404(); | 458 test404(); |
| 413 testReasonPhrase(); | 459 testReasonPhrase(); |
| 460 testHost(); |
| 414 } | 461 } |
| OLD | NEW |