| 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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 bool get isError() => _state == ERROR; | 85 bool get isError() => _state == ERROR; |
| 86 | 86 |
| 87 int get port() => _port; | 87 int get port() => _port; |
| 88 | 88 |
| 89 int _state; | 89 int _state; |
| 90 int _port; | 90 int _port; |
| 91 } | 91 } |
| 92 | 92 |
| 93 | 93 |
| 94 class TestServer extends Isolate { | 94 class TestServer extends Isolate { |
| 95 // Echo the request content back to the response. | |
| 96 void _echoHandler(HttpRequest request, HttpResponse response) { | |
| 97 Expect.equals("POST", request.method); | |
| 98 response.contentLength = request.contentLength; | |
| 99 request.inputStream.pipe(response.outputStream); | |
| 100 } | |
| 101 | |
| 102 // Echo the request content back to the response. | |
| 103 void _zeroToTenHandler(HttpRequest request, HttpResponse response) { | |
| 104 Expect.equals("GET", request.method); | |
| 105 request.inputStream.onData = () {}; | |
| 106 request.inputStream.onClosed = () { | |
| 107 response.outputStream.writeString("01234567890"); | |
| 108 response.outputStream.close(); | |
| 109 }; | |
| 110 } | |
| 111 | |
| 112 // Return a 404. | 95 // Return a 404. |
| 113 void _notFoundHandler(HttpRequest request, HttpResponse response) { | 96 void _notFoundHandler(HttpRequest request, HttpResponse response) { |
| 114 response.statusCode = HttpStatus.NOT_FOUND; | 97 response.statusCode = HttpStatus.NOT_FOUND; |
| 115 response.headers.set("Content-Type", "text/html; charset=UTF-8"); | 98 response.headers.set("Content-Type", "text/html; charset=UTF-8"); |
| 116 response.outputStream.writeString("Page not found"); | 99 response.outputStream.writeString("Page not found"); |
| 117 response.outputStream.close(); | 100 response.outputStream.close(); |
| 118 } | 101 } |
| 119 | 102 |
| 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 | |
| 127 // Check the "Host" header. | 103 // Check the "Host" header. |
| 128 void _hostHandler(HttpRequest request, HttpResponse response) { | 104 void _hostHandler(HttpRequest request, HttpResponse response) { |
| 129 Expect.equals(1, request.headers["Host"].length); | 105 Expect.equals(1, request.headers["Host"].length); |
| 130 Expect.equals("www.dartlang.org:1234", request.headers["Host"][0]); | 106 Expect.equals("www.dartlang.org:1234", request.headers["Host"][0]); |
| 131 Expect.equals("www.dartlang.org", request.headers.host); | 107 Expect.equals("www.dartlang.org", request.headers.host); |
| 132 Expect.equals(1234, request.headers.port); | 108 Expect.equals(1234, request.headers.port); |
| 133 response.statusCode = HttpStatus.OK; | 109 response.statusCode = HttpStatus.OK; |
| 134 response.outputStream.close(); | 110 response.outputStream.close(); |
| 135 } | 111 } |
| 136 | 112 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 response.headers.set(HttpHeaders.CONTENT_TYPE, | 147 response.headers.set(HttpHeaders.CONTENT_TYPE, |
| 172 "text/html; charset = utf-8"); | 148 "text/html; charset = utf-8"); |
| 173 response.outputStream.close(); | 149 response.outputStream.close(); |
| 174 } | 150 } |
| 175 | 151 |
| 176 void _cookie1Handler(HttpRequest request, HttpResponse response) { | 152 void _cookie1Handler(HttpRequest request, HttpResponse response) { |
| 177 // No cookies passed with this request. | 153 // No cookies passed with this request. |
| 178 Expect.equals(0, request.cookies.length); | 154 Expect.equals(0, request.cookies.length); |
| 179 | 155 |
| 180 Cookie cookie1 = new Cookie("name1", "value1"); | 156 Cookie cookie1 = new Cookie("name1", "value1"); |
| 181 TimeZone utc = new TimeZone.utc(); | 157 Date date = new Date(2014, Date.JAN, 5, 23, 59, 59, 0, isUtc: true); |
| 182 Date date = new Date.withTimeZone(2014, Date.JAN, 5, 23, 59, 59, 0, utc); | |
| 183 cookie1.expires = date; | 158 cookie1.expires = date; |
| 184 cookie1.domain = "www.example.com"; | 159 cookie1.domain = "www.example.com"; |
| 185 cookie1.httpOnly = true; | 160 cookie1.httpOnly = true; |
| 186 response.cookies.add(cookie1); | 161 response.cookies.add(cookie1); |
| 187 Cookie cookie2 = new Cookie("name2", "value2"); | 162 Cookie cookie2 = new Cookie("name2", "value2"); |
| 188 cookie2.maxAge = 100; | 163 cookie2.maxAge = 100; |
| 189 cookie2.domain = ".example.com"; | 164 cookie2.domain = ".example.com"; |
| 190 cookie2.path = "/shop"; | 165 cookie2.path = "/shop"; |
| 191 response.cookies.add(cookie2); | 166 response.cookies.add(cookie2); |
| 192 response.outputStream.close(); | 167 response.outputStream.close(); |
| 193 } | 168 } |
| 194 | 169 |
| 195 void _cookie2Handler(HttpRequest request, HttpResponse response) { | 170 void _cookie2Handler(HttpRequest request, HttpResponse response) { |
| 196 // Two cookies passed with this request. | 171 // Two cookies passed with this request. |
| 197 Expect.equals(2, request.cookies.length); | 172 Expect.equals(2, request.cookies.length); |
| 198 response.outputStream.close(); | 173 response.outputStream.close(); |
| 199 } | 174 } |
| 200 | 175 |
| 201 void main() { | 176 void main() { |
| 202 // Setup request handlers. | 177 // Setup request handlers. |
| 203 _requestHandlers = new Map(); | 178 _requestHandlers = new Map(); |
| 204 _requestHandlers["/echo"] = (HttpRequest request, HttpResponse response) { | |
| 205 _echoHandler(request, response); | |
| 206 }; | |
| 207 _requestHandlers["/0123456789"] = | |
| 208 (HttpRequest request, HttpResponse response) { | |
| 209 _zeroToTenHandler(request, response); | |
| 210 }; | |
| 211 _requestHandlers["/reasonformoving"] = | |
| 212 (HttpRequest request, HttpResponse response) { | |
| 213 _reasonForMovingHandler(request, response); | |
| 214 }; | |
| 215 _requestHandlers["/host"] = | 179 _requestHandlers["/host"] = |
| 216 (HttpRequest request, HttpResponse response) { | 180 (HttpRequest request, HttpResponse response) { |
| 217 _hostHandler(request, response); | 181 _hostHandler(request, response); |
| 218 }; | 182 }; |
| 219 _requestHandlers["/expires1"] = | 183 _requestHandlers["/expires1"] = |
| 220 (HttpRequest request, HttpResponse response) { | 184 (HttpRequest request, HttpResponse response) { |
| 221 _expires1Handler(request, response); | 185 _expires1Handler(request, response); |
| 222 }; | 186 }; |
| 223 _requestHandlers["/expires2"] = | 187 _requestHandlers["/expires2"] = |
| 224 (HttpRequest request, HttpResponse response) { | 188 (HttpRequest request, HttpResponse response) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 } else { | 234 } else { |
| 271 _notFoundHandler(request, response); | 235 _notFoundHandler(request, response); |
| 272 } | 236 } |
| 273 } | 237 } |
| 274 | 238 |
| 275 HttpServer _server; // HTTP server instance. | 239 HttpServer _server; // HTTP server instance. |
| 276 Map _requestHandlers; | 240 Map _requestHandlers; |
| 277 bool _chunkedEncoding = false; | 241 bool _chunkedEncoding = false; |
| 278 } | 242 } |
| 279 | 243 |
| 280 | |
| 281 void testStartStop() { | |
| 282 TestServerMain testServerMain = new TestServerMain(); | |
| 283 testServerMain.setServerStartedHandler((int port) { | |
| 284 testServerMain.shutdown(); | |
| 285 }); | |
| 286 testServerMain.start(); | |
| 287 } | |
| 288 | |
| 289 | |
| 290 void testGET() { | |
| 291 TestServerMain testServerMain = new TestServerMain(); | |
| 292 testServerMain.setServerStartedHandler((int port) { | |
| 293 HttpClient httpClient = new HttpClient(); | |
| 294 HttpClientConnection conn = | |
| 295 httpClient.get("127.0.0.1", port, "/0123456789"); | |
| 296 conn.onResponse = (HttpClientResponse response) { | |
| 297 Expect.equals(HttpStatus.OK, response.statusCode); | |
| 298 StringInputStream stream = new StringInputStream(response.inputStream); | |
| 299 StringBuffer body = new StringBuffer(); | |
| 300 stream.onData = () => body.add(stream.read()); | |
| 301 stream.onClosed = () { | |
| 302 Expect.equals("01234567890", body.toString()); | |
| 303 httpClient.shutdown(); | |
| 304 testServerMain.shutdown(); | |
| 305 }; | |
| 306 }; | |
| 307 }); | |
| 308 testServerMain.start(); | |
| 309 } | |
| 310 | |
| 311 | |
| 312 void testPOST(bool chunkedEncoding) { | |
| 313 String data = "ABCDEFGHIJKLMONPQRSTUVWXYZ"; | |
| 314 final int kMessageCount = 10; | |
| 315 | |
| 316 TestServerMain testServerMain = new TestServerMain(); | |
| 317 | |
| 318 void runTest(int port) { | |
| 319 int count = 0; | |
| 320 HttpClient httpClient = new HttpClient(); | |
| 321 void sendRequest() { | |
| 322 HttpClientConnection conn = | |
| 323 httpClient.post("127.0.0.1", port, "/echo"); | |
| 324 conn.onRequest = (HttpClientRequest request) { | |
| 325 if (chunkedEncoding) { | |
| 326 request.outputStream.writeString(data.substring(0, 10)); | |
| 327 request.outputStream.writeString(data.substring(10, data.length)); | |
| 328 } else { | |
| 329 request.contentLength = data.length; | |
| 330 request.outputStream.write(data.charCodes()); | |
| 331 } | |
| 332 request.outputStream.close(); | |
| 333 }; | |
| 334 conn.onResponse = (HttpClientResponse response) { | |
| 335 Expect.equals(HttpStatus.OK, response.statusCode); | |
| 336 StringInputStream stream = new StringInputStream(response.inputStream); | |
| 337 StringBuffer body = new StringBuffer(); | |
| 338 stream.onData = () => body.add(stream.read()); | |
| 339 stream.onClosed = () { | |
| 340 Expect.equals(data, body.toString()); | |
| 341 count++; | |
| 342 if (count < kMessageCount) { | |
| 343 sendRequest(); | |
| 344 } else { | |
| 345 httpClient.shutdown(); | |
| 346 testServerMain.shutdown(); | |
| 347 } | |
| 348 }; | |
| 349 }; | |
| 350 } | |
| 351 | |
| 352 sendRequest(); | |
| 353 } | |
| 354 | |
| 355 testServerMain.setServerStartedHandler(runTest); | |
| 356 if (chunkedEncoding) { | |
| 357 testServerMain.chunkedEncoding(); | |
| 358 } | |
| 359 testServerMain.start(); | |
| 360 } | |
| 361 | |
| 362 | |
| 363 void testReadInto(bool chunkedEncoding) { | |
| 364 String data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
| 365 final int kMessageCount = 10; | |
| 366 | |
| 367 TestServerMain testServerMain = new TestServerMain(); | |
| 368 | |
| 369 void runTest(int port) { | |
| 370 int count = 0; | |
| 371 HttpClient httpClient = new HttpClient(); | |
| 372 void sendRequest() { | |
| 373 HttpClientConnection conn = | |
| 374 httpClient.post("127.0.0.1", port, "/echo"); | |
| 375 conn.onRequest = (HttpClientRequest request) { | |
| 376 if (chunkedEncoding) { | |
| 377 request.outputStream.writeString(data.substring(0, 10)); | |
| 378 request.outputStream.writeString(data.substring(10, data.length)); | |
| 379 } else { | |
| 380 request.contentLength = data.length; | |
| 381 request.outputStream.write(data.charCodes()); | |
| 382 } | |
| 383 request.outputStream.close(); | |
| 384 }; | |
| 385 conn.onResponse = (HttpClientResponse response) { | |
| 386 Expect.equals(HttpStatus.OK, response.statusCode); | |
| 387 InputStream stream = response.inputStream; | |
| 388 List<int> body = new List<int>(); | |
| 389 stream.onData = () { | |
| 390 List tmp = new List(3); | |
| 391 int bytes = stream.readInto(tmp); | |
| 392 body.addAll(tmp.getRange(0, bytes)); | |
| 393 }; | |
| 394 stream.onClosed = () { | |
| 395 Expect.equals(data, new String.fromCharCodes(body)); | |
| 396 count++; | |
| 397 if (count < kMessageCount) { | |
| 398 sendRequest(); | |
| 399 } else { | |
| 400 httpClient.shutdown(); | |
| 401 testServerMain.shutdown(); | |
| 402 } | |
| 403 }; | |
| 404 }; | |
| 405 } | |
| 406 | |
| 407 sendRequest(); | |
| 408 } | |
| 409 | |
| 410 testServerMain.setServerStartedHandler(runTest); | |
| 411 if (chunkedEncoding) { | |
| 412 testServerMain.chunkedEncoding(); | |
| 413 } | |
| 414 testServerMain.start(); | |
| 415 } | |
| 416 | |
| 417 | |
| 418 void testReadShort(bool chunkedEncoding) { | |
| 419 String data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
| 420 final int kMessageCount = 10; | |
| 421 | |
| 422 TestServerMain testServerMain = new TestServerMain(); | |
| 423 | |
| 424 void runTest(int port) { | |
| 425 int count = 0; | |
| 426 HttpClient httpClient = new HttpClient(); | |
| 427 void sendRequest() { | |
| 428 HttpClientConnection conn = | |
| 429 httpClient.post("127.0.0.1", port, "/echo"); | |
| 430 conn.onRequest = (HttpClientRequest request) { | |
| 431 if (chunkedEncoding) { | |
| 432 request.outputStream.writeString(data.substring(0, 10)); | |
| 433 request.outputStream.writeString(data.substring(10, data.length)); | |
| 434 } else { | |
| 435 request.contentLength = data.length; | |
| 436 request.outputStream.write(data.charCodes()); | |
| 437 } | |
| 438 request.outputStream.close(); | |
| 439 }; | |
| 440 conn.onResponse = (HttpClientResponse response) { | |
| 441 Expect.equals(HttpStatus.OK, response.statusCode); | |
| 442 InputStream stream = response.inputStream; | |
| 443 List<int> body = new List<int>(); | |
| 444 stream.onData = () { | |
| 445 List tmp = stream.read(2); | |
| 446 body.addAll(tmp); | |
| 447 }; | |
| 448 stream.onClosed = () { | |
| 449 Expect.equals(data, new String.fromCharCodes(body)); | |
| 450 count++; | |
| 451 if (count < kMessageCount) { | |
| 452 sendRequest(); | |
| 453 } else { | |
| 454 httpClient.shutdown(); | |
| 455 testServerMain.shutdown(); | |
| 456 } | |
| 457 }; | |
| 458 }; | |
| 459 } | |
| 460 | |
| 461 sendRequest(); | |
| 462 } | |
| 463 | |
| 464 testServerMain.setServerStartedHandler(runTest); | |
| 465 if (chunkedEncoding) { | |
| 466 testServerMain.chunkedEncoding(); | |
| 467 } | |
| 468 testServerMain.start(); | |
| 469 } | |
| 470 | |
| 471 | |
| 472 void test404() { | |
| 473 TestServerMain testServerMain = new TestServerMain(); | |
| 474 testServerMain.setServerStartedHandler((int port) { | |
| 475 HttpClient httpClient = new HttpClient(); | |
| 476 HttpClientConnection conn = | |
| 477 httpClient.get("127.0.0.1", port, "/thisisnotfound"); | |
| 478 conn.onResponse = (HttpClientResponse response) { | |
| 479 Expect.equals(HttpStatus.NOT_FOUND, response.statusCode); | |
| 480 httpClient.shutdown(); | |
| 481 testServerMain.shutdown(); | |
| 482 }; | |
| 483 }); | |
| 484 testServerMain.start(); | |
| 485 } | |
| 486 | |
| 487 | |
| 488 void testReasonPhrase() { | |
| 489 TestServerMain testServerMain = new TestServerMain(); | |
| 490 testServerMain.setServerStartedHandler((int port) { | |
| 491 HttpClient httpClient = new HttpClient(); | |
| 492 HttpClientConnection conn = | |
| 493 httpClient.get("127.0.0.1", port, "/reasonformoving"); | |
| 494 conn.followRedirects = false; | |
| 495 conn.onResponse = (HttpClientResponse response) { | |
| 496 Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode); | |
| 497 Expect.equals("Don't come looking here any more", response.reasonPhrase); | |
| 498 httpClient.shutdown(); | |
| 499 testServerMain.shutdown(); | |
| 500 }; | |
| 501 }); | |
| 502 testServerMain.start(); | |
| 503 } | |
| 504 | |
| 505 | |
| 506 void testHost() { | 244 void testHost() { |
| 507 TestServerMain testServerMain = new TestServerMain(); | 245 TestServerMain testServerMain = new TestServerMain(); |
| 508 testServerMain.setServerStartedHandler((int port) { | 246 testServerMain.setServerStartedHandler((int port) { |
| 509 HttpClient httpClient = new HttpClient(); | 247 HttpClient httpClient = new HttpClient(); |
| 510 HttpClientConnection conn = | 248 HttpClientConnection conn = |
| 511 httpClient.get("127.0.0.1", port, "/host"); | 249 httpClient.get("127.0.0.1", port, "/host"); |
| 512 conn.onRequest = (HttpClientRequest request) { | 250 conn.onRequest = (HttpClientRequest request) { |
| 513 Expect.equals("127.0.0.1:$port", request.headers["host"][0]); | 251 Expect.equals("127.0.0.1:$port", request.headers["host"][0]); |
| 514 request.headers.host = "www.dartlang.com"; | 252 request.headers.host = "www.dartlang.com"; |
| 515 Expect.equals("www.dartlang.com:$port", request.headers["host"][0]); | 253 Expect.equals("www.dartlang.com:$port", request.headers["host"][0]); |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 626 int responses = 0; | 364 int responses = 0; |
| 627 HttpClient httpClient = new HttpClient(); | 365 HttpClient httpClient = new HttpClient(); |
| 628 | 366 |
| 629 HttpClientConnection conn1 = | 367 HttpClientConnection conn1 = |
| 630 httpClient.get("127.0.0.1", port, "/cookie1"); | 368 httpClient.get("127.0.0.1", port, "/cookie1"); |
| 631 conn1.onResponse = (HttpClientResponse response) { | 369 conn1.onResponse = (HttpClientResponse response) { |
| 632 Expect.equals(2, response.cookies.length); | 370 Expect.equals(2, response.cookies.length); |
| 633 response.cookies.forEach((cookie) { | 371 response.cookies.forEach((cookie) { |
| 634 if (cookie.name == "name1") { | 372 if (cookie.name == "name1") { |
| 635 Expect.equals("value1", cookie.value); | 373 Expect.equals("value1", cookie.value); |
| 636 TimeZone utc = new TimeZone.utc(); | 374 Date date = new Date(2014, Date.JAN, 5, 23, 59, 59, 0, isUtc: true); |
| 637 Date date = | |
| 638 new Date.withTimeZone(2014, Date.JAN, 5, 23, 59, 59, 0, utc); | |
| 639 Expect.equals(date, cookie.expires); | 375 Expect.equals(date, cookie.expires); |
| 640 Expect.equals("www.example.com", cookie.domain); | 376 Expect.equals("www.example.com", cookie.domain); |
| 641 Expect.isTrue(cookie.httpOnly); | 377 Expect.isTrue(cookie.httpOnly); |
| 642 } else if (cookie.name == "name2") { | 378 } else if (cookie.name == "name2") { |
| 643 Expect.equals("value2", cookie.value); | 379 Expect.equals("value2", cookie.value); |
| 644 Expect.equals(100, cookie.maxAge); | 380 Expect.equals(100, cookie.maxAge); |
| 645 Expect.equals(".example.com", cookie.domain); | 381 Expect.equals(".example.com", cookie.domain); |
| 646 Expect.equals("/shop", cookie.path); | 382 Expect.equals("/shop", cookie.path); |
| 647 } else { | 383 } else { |
| 648 Expect.fail("Unexpected cookie"); | 384 Expect.fail("Unexpected cookie"); |
| 649 } | 385 } |
| 650 }); | 386 }); |
| 651 HttpClientConnection conn2 = | 387 HttpClientConnection conn2 = |
| 652 httpClient.get("127.0.0.1", port, "/cookie2"); | 388 httpClient.get("127.0.0.1", port, "/cookie2"); |
| 653 conn2.onRequest = (HttpClientRequest request) { | 389 conn2.onRequest = (HttpClientRequest request) { |
| 654 request.cookies.add(response.cookies[0]); | 390 request.cookies.add(response.cookies[0]); |
| 655 request.cookies.add(response.cookies[1]); | 391 request.cookies.add(response.cookies[1]); |
| 656 request.outputStream.close(); | 392 request.outputStream.close(); |
| 657 }; | 393 }; |
| 658 conn2.onResponse = (HttpClientRequest request) { | 394 conn2.onResponse = (HttpClientResponse ignored) { |
| 659 httpClient.shutdown(); | 395 httpClient.shutdown(); |
| 660 testServerMain.shutdown(); | 396 testServerMain.shutdown(); |
| 661 }; | 397 }; |
| 662 }; | 398 }; |
| 663 }); | 399 }); |
| 664 testServerMain.start(); | 400 testServerMain.start(); |
| 665 } | 401 } |
| 666 | 402 |
| 667 void main() { | 403 void main() { |
| 668 testStartStop(); | |
| 669 testGET(); | |
| 670 testPOST(true); | |
| 671 testPOST(false); | |
| 672 testReadInto(true); | |
| 673 testReadInto(false); | |
| 674 testReadShort(true); | |
| 675 testReadShort(false); | |
| 676 test404(); | |
| 677 testReasonPhrase(); | |
| 678 testHost(); | 404 testHost(); |
| 679 testExpires(); | 405 testExpires(); |
| 680 testContentType(); | 406 testContentType(); |
| 681 testCookies(); | 407 testCookies(); |
| 682 } | 408 } |
| OLD | NEW |