| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 // | |
| 5 // VMOptions= | |
| 6 // VMOptions=--short_socket_read | |
| 7 // VMOptions=--short_socket_write | |
| 8 // VMOptions=--short_socket_read --short_socket_write | |
| 9 | |
| 10 #import("dart:isolate"); | |
| 11 #import("dart:io"); | |
| 12 | |
| 13 class TestServerMain { | |
| 14 TestServerMain() | |
| 15 : _statusPort = new ReceivePort(), | |
| 16 _serverPort = null { | |
| 17 new TestServer().spawn().then((SendPort port) { | |
| 18 _serverPort = port; | |
| 19 }); | |
| 20 } | |
| 21 | |
| 22 void setServerStartedHandler(void startedCallback(int port)) { | |
| 23 _startedCallback = startedCallback; | |
| 24 } | |
| 25 | |
| 26 void start() { | |
| 27 // Handle status messages from the server. | |
| 28 _statusPort.receive((var status, SendPort replyTo) { | |
| 29 if (status.isStarted) { | |
| 30 _startedCallback(status.port); | |
| 31 } | |
| 32 }); | |
| 33 | |
| 34 // Send server start message to the server. | |
| 35 var command = new TestServerCommand.start(); | |
| 36 _serverPort.send(command, _statusPort.toSendPort()); | |
| 37 } | |
| 38 | |
| 39 void shutdown() { | |
| 40 // Send server stop message to the server. | |
| 41 _serverPort.send(new TestServerCommand.stop(), _statusPort.toSendPort()); | |
| 42 _statusPort.close(); | |
| 43 } | |
| 44 | |
| 45 void chunkedEncoding() { | |
| 46 // Send chunked encoding message to the server. | |
| 47 _serverPort.send( | |
| 48 new TestServerCommand.chunkedEncoding(), _statusPort.toSendPort()); | |
| 49 } | |
| 50 | |
| 51 ReceivePort _statusPort; // Port for receiving messages from the server. | |
| 52 SendPort _serverPort; // Port for sending messages to the server. | |
| 53 var _startedCallback; | |
| 54 } | |
| 55 | |
| 56 | |
| 57 class TestServerCommand { | |
| 58 static final START = 0; | |
| 59 static final STOP = 1; | |
| 60 static final CHUNKED_ENCODING = 2; | |
| 61 | |
| 62 TestServerCommand.start() : _command = START; | |
| 63 TestServerCommand.stop() : _command = STOP; | |
| 64 TestServerCommand.chunkedEncoding() : _command = CHUNKED_ENCODING; | |
| 65 | |
| 66 bool get isStart() => _command == START; | |
| 67 bool get isStop() => _command == STOP; | |
| 68 bool get isChunkedEncoding() => _command == CHUNKED_ENCODING; | |
| 69 | |
| 70 int _command; | |
| 71 } | |
| 72 | |
| 73 | |
| 74 class TestServerStatus { | |
| 75 static final STARTED = 0; | |
| 76 static final STOPPED = 1; | |
| 77 static final ERROR = 2; | |
| 78 | |
| 79 TestServerStatus.started(this._port) : _state = STARTED; | |
| 80 TestServerStatus.stopped() : _state = STOPPED; | |
| 81 TestServerStatus.error() : _state = ERROR; | |
| 82 | |
| 83 bool get isStarted() => _state == STARTED; | |
| 84 bool get isStopped() => _state == STOPPED; | |
| 85 bool get isError() => _state == ERROR; | |
| 86 | |
| 87 int get port() => _port; | |
| 88 | |
| 89 int _state; | |
| 90 int _port; | |
| 91 } | |
| 92 | |
| 93 | |
| 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. | |
| 113 void _notFoundHandler(HttpRequest request, HttpResponse response) { | |
| 114 response.statusCode = HttpStatus.NOT_FOUND; | |
| 115 response.headers.set("Content-Type", "text/html; charset=UTF-8"); | |
| 116 response.outputStream.writeString("Page not found"); | |
| 117 response.outputStream.close(); | |
| 118 } | |
| 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 | |
| 127 // Check the "Host" header. | |
| 128 void _hostHandler(HttpRequest request, HttpResponse response) { | |
| 129 Expect.equals(1, request.headers["Host"].length); | |
| 130 Expect.equals("www.dartlang.org:1234", request.headers["Host"][0]); | |
| 131 Expect.equals("www.dartlang.org", request.headers.host); | |
| 132 Expect.equals(1234, request.headers.port); | |
| 133 response.statusCode = HttpStatus.OK; | |
| 134 response.outputStream.close(); | |
| 135 } | |
| 136 | |
| 137 // Set the "Expires" header using the expires property. | |
| 138 void _expires1Handler(HttpRequest request, HttpResponse response) { | |
| 139 Date date = new Date(1999, Date.JUN, 11, 18, 46, 53, 0, isUtc: true); | |
| 140 response.headers.expires = date; | |
| 141 Expect.equals(date, response.headers.expires); | |
| 142 response.outputStream.close(); | |
| 143 } | |
| 144 | |
| 145 // Set the "Expires" header. | |
| 146 void _expires2Handler(HttpRequest request, HttpResponse response) { | |
| 147 response.headers.set("Expires", "Fri, 11 Jun 1999 18:46:53 GMT"); | |
| 148 Date date = new Date(1999, Date.JUN, 11, 18, 46, 53, 0, isUtc: true); | |
| 149 Expect.equals(date, response.headers.expires); | |
| 150 response.outputStream.close(); | |
| 151 } | |
| 152 | |
| 153 void _contentType1Handler(HttpRequest request, HttpResponse response) { | |
| 154 Expect.equals("text/html", request.headers.contentType.value); | |
| 155 Expect.equals("text", request.headers.contentType.primaryType); | |
| 156 Expect.equals("html", request.headers.contentType.subType); | |
| 157 Expect.equals("utf-8", request.headers.contentType.parameters["charset"]); | |
| 158 | |
| 159 ContentType contentType = new ContentType("text", "html"); | |
| 160 contentType.parameters["charset"] = "utf-8"; | |
| 161 response.headers.contentType = contentType; | |
| 162 response.outputStream.close(); | |
| 163 } | |
| 164 | |
| 165 void _contentType2Handler(HttpRequest request, HttpResponse response) { | |
| 166 Expect.equals("text/html", request.headers.contentType.value); | |
| 167 Expect.equals("text", request.headers.contentType.primaryType); | |
| 168 Expect.equals("html", request.headers.contentType.subType); | |
| 169 Expect.equals("utf-8", request.headers.contentType.parameters["charset"]); | |
| 170 | |
| 171 response.headers.set(HttpHeaders.CONTENT_TYPE, | |
| 172 "text/html; charset = utf-8"); | |
| 173 response.outputStream.close(); | |
| 174 } | |
| 175 | |
| 176 void _cookie1Handler(HttpRequest request, HttpResponse response) { | |
| 177 // No cookies passed with this request. | |
| 178 Expect.equals(0, request.cookies.length); | |
| 179 | |
| 180 Cookie cookie1 = new Cookie("name1", "value1"); | |
| 181 TimeZone utc = new TimeZone.utc(); | |
| 182 Date date = new Date.withTimeZone(2014, Date.JAN, 5, 23, 59, 59, 0, utc); | |
| 183 cookie1.expires = date; | |
| 184 cookie1.domain = "www.example.com"; | |
| 185 cookie1.httpOnly = true; | |
| 186 response.cookies.add(cookie1); | |
| 187 Cookie cookie2 = new Cookie("name2", "value2"); | |
| 188 cookie2.maxAge = 100; | |
| 189 cookie2.domain = ".example.com"; | |
| 190 cookie2.path = "/shop"; | |
| 191 response.cookies.add(cookie2); | |
| 192 response.outputStream.close(); | |
| 193 } | |
| 194 | |
| 195 void _cookie2Handler(HttpRequest request, HttpResponse response) { | |
| 196 // Two cookies passed with this request. | |
| 197 Expect.equals(2, request.cookies.length); | |
| 198 response.outputStream.close(); | |
| 199 } | |
| 200 | |
| 201 void main() { | |
| 202 // Setup request handlers. | |
| 203 _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"] = | |
| 216 (HttpRequest request, HttpResponse response) { | |
| 217 _hostHandler(request, response); | |
| 218 }; | |
| 219 _requestHandlers["/expires1"] = | |
| 220 (HttpRequest request, HttpResponse response) { | |
| 221 _expires1Handler(request, response); | |
| 222 }; | |
| 223 _requestHandlers["/expires2"] = | |
| 224 (HttpRequest request, HttpResponse response) { | |
| 225 _expires2Handler(request, response); | |
| 226 }; | |
| 227 _requestHandlers["/contenttype1"] = | |
| 228 (HttpRequest request, HttpResponse response) { | |
| 229 _contentType1Handler(request, response); | |
| 230 }; | |
| 231 _requestHandlers["/contenttype2"] = | |
| 232 (HttpRequest request, HttpResponse response) { | |
| 233 _contentType2Handler(request, response); | |
| 234 }; | |
| 235 _requestHandlers["/cookie1"] = | |
| 236 (HttpRequest request, HttpResponse response) { | |
| 237 _cookie1Handler(request, response); | |
| 238 }; | |
| 239 _requestHandlers["/cookie2"] = | |
| 240 (HttpRequest request, HttpResponse response) { | |
| 241 _cookie2Handler(request, response); | |
| 242 }; | |
| 243 | |
| 244 this.port.receive((var message, SendPort replyTo) { | |
| 245 if (message.isStart) { | |
| 246 _server = new HttpServer(); | |
| 247 try { | |
| 248 _server.listen("127.0.0.1", 0); | |
| 249 _server.defaultRequestHandler = (HttpRequest req, HttpResponse rsp) { | |
| 250 _requestReceivedHandler(req, rsp); | |
| 251 }; | |
| 252 replyTo.send(new TestServerStatus.started(_server.port), null); | |
| 253 } catch (var e) { | |
| 254 replyTo.send(new TestServerStatus.error(), null); | |
| 255 } | |
| 256 } else if (message.isStop) { | |
| 257 _server.close(); | |
| 258 this.port.close(); | |
| 259 replyTo.send(new TestServerStatus.stopped(), null); | |
| 260 } else if (message.isChunkedEncoding) { | |
| 261 _chunkedEncoding = true; | |
| 262 } | |
| 263 }); | |
| 264 } | |
| 265 | |
| 266 void _requestReceivedHandler(HttpRequest request, HttpResponse response) { | |
| 267 var requestHandler =_requestHandlers[request.path]; | |
| 268 if (requestHandler != null) { | |
| 269 requestHandler(request, response); | |
| 270 } else { | |
| 271 _notFoundHandler(request, response); | |
| 272 } | |
| 273 } | |
| 274 | |
| 275 HttpServer _server; // HTTP server instance. | |
| 276 Map _requestHandlers; | |
| 277 bool _chunkedEncoding = false; | |
| 278 } | |
| 279 | |
| 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() { | |
| 507 TestServerMain testServerMain = new TestServerMain(); | |
| 508 testServerMain.setServerStartedHandler((int port) { | |
| 509 HttpClient httpClient = new HttpClient(); | |
| 510 HttpClientConnection conn = | |
| 511 httpClient.get("127.0.0.1", port, "/host"); | |
| 512 conn.onRequest = (HttpClientRequest request) { | |
| 513 Expect.equals("127.0.0.1:$port", request.headers["host"][0]); | |
| 514 request.headers.host = "www.dartlang.com"; | |
| 515 Expect.equals("www.dartlang.com:$port", request.headers["host"][0]); | |
| 516 Expect.equals("www.dartlang.com", request.headers.host); | |
| 517 Expect.equals(port, request.headers.port); | |
| 518 request.headers.port = 1234; | |
| 519 Expect.equals("www.dartlang.com:1234", request.headers["host"][0]); | |
| 520 Expect.equals(1234, request.headers.port); | |
| 521 request.headers.port = HttpClient.DEFAULT_HTTP_PORT; | |
| 522 Expect.equals(HttpClient.DEFAULT_HTTP_PORT, request.headers.port); | |
| 523 Expect.equals("www.dartlang.com", request.headers["host"][0]); | |
| 524 request.headers.set("Host", "www.dartlang.org"); | |
| 525 Expect.equals("www.dartlang.org", request.headers.host); | |
| 526 Expect.equals(HttpClient.DEFAULT_HTTP_PORT, request.headers.port); | |
| 527 request.headers.set("Host", "www.dartlang.org:"); | |
| 528 Expect.equals("www.dartlang.org", request.headers.host); | |
| 529 Expect.equals(HttpClient.DEFAULT_HTTP_PORT, request.headers.port); | |
| 530 request.headers.set("Host", "www.dartlang.org:1234"); | |
| 531 Expect.equals("www.dartlang.org", request.headers.host); | |
| 532 Expect.equals(1234, request.headers.port); | |
| 533 request.outputStream.close(); | |
| 534 }; | |
| 535 conn.onResponse = (HttpClientResponse response) { | |
| 536 Expect.equals(HttpStatus.OK, response.statusCode); | |
| 537 httpClient.shutdown(); | |
| 538 testServerMain.shutdown(); | |
| 539 }; | |
| 540 }); | |
| 541 testServerMain.start(); | |
| 542 } | |
| 543 | |
| 544 void testExpires() { | |
| 545 TestServerMain testServerMain = new TestServerMain(); | |
| 546 testServerMain.setServerStartedHandler((int port) { | |
| 547 int responses = 0; | |
| 548 HttpClient httpClient = new HttpClient(); | |
| 549 | |
| 550 void processResponse(HttpClientResponse response) { | |
| 551 Expect.equals(HttpStatus.OK, response.statusCode); | |
| 552 Expect.equals("Fri, 11 Jun 1999 18:46:53 GMT", | |
| 553 response.headers["expires"][0]); | |
| 554 Expect.equals(new Date(1999, Date.JUN, 11, 18, 46, 53, 0, isUtc: true), | |
| 555 response.headers.expires); | |
| 556 responses++; | |
| 557 if (responses == 2) { | |
| 558 httpClient.shutdown(); | |
| 559 testServerMain.shutdown(); | |
| 560 } | |
| 561 } | |
| 562 | |
| 563 HttpClientConnection conn1 = httpClient.get("127.0.0.1", port, "/expires1"); | |
| 564 conn1.onResponse = (HttpClientResponse response) { | |
| 565 processResponse(response); | |
| 566 }; | |
| 567 HttpClientConnection conn2 = httpClient.get("127.0.0.1", port, "/expires2"); | |
| 568 conn2.onResponse = (HttpClientResponse response) { | |
| 569 processResponse(response); | |
| 570 }; | |
| 571 }); | |
| 572 testServerMain.start(); | |
| 573 } | |
| 574 | |
| 575 void testContentType() { | |
| 576 TestServerMain testServerMain = new TestServerMain(); | |
| 577 testServerMain.setServerStartedHandler((int port) { | |
| 578 int responses = 0; | |
| 579 HttpClient httpClient = new HttpClient(); | |
| 580 | |
| 581 void processResponse(HttpClientResponse response) { | |
| 582 Expect.equals(HttpStatus.OK, response.statusCode); | |
| 583 Expect.equals("text/html; charset=utf-8", | |
| 584 response.headers.contentType.toString()); | |
| 585 Expect.equals("text/html", response.headers.contentType.value); | |
| 586 Expect.equals("text", response.headers.contentType.primaryType); | |
| 587 Expect.equals("html", response.headers.contentType.subType); | |
| 588 Expect.equals("utf-8", | |
| 589 response.headers.contentType.parameters["charset"]); | |
| 590 responses++; | |
| 591 if (responses == 2) { | |
| 592 httpClient.shutdown(); | |
| 593 testServerMain.shutdown(); | |
| 594 } | |
| 595 } | |
| 596 | |
| 597 HttpClientConnection conn1 = | |
| 598 httpClient.get("127.0.0.1", port, "/contenttype1"); | |
| 599 conn1.onRequest = (HttpClientRequest request) { | |
| 600 ContentType contentType = new ContentType(); | |
| 601 contentType.value = "text/html"; | |
| 602 contentType.parameters["charset"] = "utf-8"; | |
| 603 request.headers.contentType = contentType; | |
| 604 request.outputStream.close(); | |
| 605 }; | |
| 606 conn1.onResponse = (HttpClientResponse response) { | |
| 607 processResponse(response); | |
| 608 }; | |
| 609 HttpClientConnection conn2 = | |
| 610 httpClient.get("127.0.0.1", port, "/contenttype2"); | |
| 611 conn2.onRequest = (HttpClientRequest request) { | |
| 612 request.headers.set(HttpHeaders.CONTENT_TYPE, | |
| 613 "text/html; charset = utf-8"); | |
| 614 request.outputStream.close(); | |
| 615 }; | |
| 616 conn2.onResponse = (HttpClientResponse response) { | |
| 617 processResponse(response); | |
| 618 }; | |
| 619 }); | |
| 620 testServerMain.start(); | |
| 621 } | |
| 622 | |
| 623 void testCookies() { | |
| 624 TestServerMain testServerMain = new TestServerMain(); | |
| 625 testServerMain.setServerStartedHandler((int port) { | |
| 626 int responses = 0; | |
| 627 HttpClient httpClient = new HttpClient(); | |
| 628 | |
| 629 HttpClientConnection conn1 = | |
| 630 httpClient.get("127.0.0.1", port, "/cookie1"); | |
| 631 conn1.onResponse = (HttpClientResponse response) { | |
| 632 Expect.equals(2, response.cookies.length); | |
| 633 response.cookies.forEach((cookie) { | |
| 634 if (cookie.name == "name1") { | |
| 635 Expect.equals("value1", cookie.value); | |
| 636 TimeZone utc = new TimeZone.utc(); | |
| 637 Date date = | |
| 638 new Date.withTimeZone(2014, Date.JAN, 5, 23, 59, 59, 0, utc); | |
| 639 Expect.equals(date, cookie.expires); | |
| 640 Expect.equals("www.example.com", cookie.domain); | |
| 641 Expect.isTrue(cookie.httpOnly); | |
| 642 } else if (cookie.name == "name2") { | |
| 643 Expect.equals("value2", cookie.value); | |
| 644 Expect.equals(100, cookie.maxAge); | |
| 645 Expect.equals(".example.com", cookie.domain); | |
| 646 Expect.equals("/shop", cookie.path); | |
| 647 } else { | |
| 648 Expect.fail("Unexpected cookie"); | |
| 649 } | |
| 650 }); | |
| 651 HttpClientConnection conn2 = | |
| 652 httpClient.get("127.0.0.1", port, "/cookie2"); | |
| 653 conn2.onRequest = (HttpClientRequest request) { | |
| 654 request.cookies.add(response.cookies[0]); | |
| 655 request.cookies.add(response.cookies[1]); | |
| 656 request.outputStream.close(); | |
| 657 }; | |
| 658 conn2.onResponse = (HttpClientRequest request) { | |
| 659 httpClient.shutdown(); | |
| 660 testServerMain.shutdown(); | |
| 661 }; | |
| 662 }; | |
| 663 }); | |
| 664 testServerMain.start(); | |
| 665 } | |
| 666 | |
| 667 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(); | |
| 679 testExpires(); | |
| 680 testContentType(); | |
| 681 testCookies(); | |
| 682 } | |
| OLD | NEW |