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 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 } else { | 248 } else { |
249 _notFoundHandler(request, response); | 249 _notFoundHandler(request, response); |
250 } | 250 } |
251 } | 251 } |
252 | 252 |
253 HttpServer _server; // HTTP server instance. | 253 HttpServer _server; // HTTP server instance. |
254 Map _requestHandlers; | 254 Map _requestHandlers; |
255 bool _chunkedEncoding = false; | 255 bool _chunkedEncoding = false; |
256 } | 256 } |
257 | 257 |
258 void testHost() { | 258 Future testHost() { |
| 259 Completer completer = new Completer(); |
259 TestServerMain testServerMain = new TestServerMain(); | 260 TestServerMain testServerMain = new TestServerMain(); |
260 testServerMain.setServerStartedHandler((int port) { | 261 testServerMain.setServerStartedHandler((int port) { |
261 HttpClient httpClient = new HttpClient(); | 262 HttpClient httpClient = new HttpClient(); |
262 HttpClientConnection conn = | 263 HttpClientConnection conn = |
263 httpClient.get("127.0.0.1", port, "/host"); | 264 httpClient.get("127.0.0.1", port, "/host"); |
264 conn.onRequest = (HttpClientRequest request) { | 265 conn.onRequest = (HttpClientRequest request) { |
265 Expect.equals("127.0.0.1:$port", request.headers["host"][0]); | 266 Expect.equals("127.0.0.1:$port", request.headers["host"][0]); |
266 request.headers.host = "www.dartlang.com"; | 267 request.headers.host = "www.dartlang.com"; |
267 Expect.equals("www.dartlang.com:$port", request.headers["host"][0]); | 268 Expect.equals("www.dartlang.com:$port", request.headers["host"][0]); |
268 Expect.equals("www.dartlang.com", request.headers.host); | 269 Expect.equals("www.dartlang.com", request.headers.host); |
(...skipping 12 matching lines...) Expand all Loading... |
281 Expect.equals(HttpClient.DEFAULT_HTTP_PORT, request.headers.port); | 282 Expect.equals(HttpClient.DEFAULT_HTTP_PORT, request.headers.port); |
282 request.headers.set("Host", "www.dartlang.org:1234"); | 283 request.headers.set("Host", "www.dartlang.org:1234"); |
283 Expect.equals("www.dartlang.org", request.headers.host); | 284 Expect.equals("www.dartlang.org", request.headers.host); |
284 Expect.equals(1234, request.headers.port); | 285 Expect.equals(1234, request.headers.port); |
285 request.outputStream.close(); | 286 request.outputStream.close(); |
286 }; | 287 }; |
287 conn.onResponse = (HttpClientResponse response) { | 288 conn.onResponse = (HttpClientResponse response) { |
288 Expect.equals(HttpStatus.OK, response.statusCode); | 289 Expect.equals(HttpStatus.OK, response.statusCode); |
289 httpClient.shutdown(); | 290 httpClient.shutdown(); |
290 testServerMain.shutdown(); | 291 testServerMain.shutdown(); |
| 292 completer.complete(true); |
291 }; | 293 }; |
292 }); | 294 }); |
293 testServerMain.start(); | 295 testServerMain.start(); |
| 296 return completer.future; |
294 } | 297 } |
295 | 298 |
296 void testExpires() { | 299 Future testExpires() { |
| 300 Completer completer = new Completer(); |
297 TestServerMain testServerMain = new TestServerMain(); | 301 TestServerMain testServerMain = new TestServerMain(); |
298 testServerMain.setServerStartedHandler((int port) { | 302 testServerMain.setServerStartedHandler((int port) { |
299 int responses = 0; | 303 int responses = 0; |
300 HttpClient httpClient = new HttpClient(); | 304 HttpClient httpClient = new HttpClient(); |
301 | 305 |
302 void processResponse(HttpClientResponse response) { | 306 void processResponse(HttpClientResponse response) { |
303 Expect.equals(HttpStatus.OK, response.statusCode); | 307 Expect.equals(HttpStatus.OK, response.statusCode); |
304 Expect.equals("Fri, 11 Jun 1999 18:46:53 GMT", | 308 Expect.equals("Fri, 11 Jun 1999 18:46:53 GMT", |
305 response.headers["expires"][0]); | 309 response.headers["expires"][0]); |
306 Expect.equals(new Date.utc(1999, Date.JUN, 11, 18, 46, 53, 0), | 310 Expect.equals(new Date.utc(1999, Date.JUN, 11, 18, 46, 53, 0), |
307 response.headers.expires); | 311 response.headers.expires); |
308 responses++; | 312 responses++; |
309 if (responses == 2) { | 313 if (responses == 2) { |
310 httpClient.shutdown(); | 314 httpClient.shutdown(); |
311 testServerMain.shutdown(); | 315 testServerMain.shutdown(); |
| 316 completer.complete(true); |
312 } | 317 } |
313 } | 318 } |
314 | 319 |
315 HttpClientConnection conn1 = httpClient.get("127.0.0.1", port, "/expires1"); | 320 HttpClientConnection conn1 = httpClient.get("127.0.0.1", port, "/expires1"); |
316 conn1.onResponse = (HttpClientResponse response) { | 321 conn1.onResponse = (HttpClientResponse response) { |
317 processResponse(response); | 322 processResponse(response); |
318 }; | 323 }; |
319 HttpClientConnection conn2 = httpClient.get("127.0.0.1", port, "/expires2"); | 324 HttpClientConnection conn2 = httpClient.get("127.0.0.1", port, "/expires2"); |
320 conn2.onResponse = (HttpClientResponse response) { | 325 conn2.onResponse = (HttpClientResponse response) { |
321 processResponse(response); | 326 processResponse(response); |
322 }; | 327 }; |
323 }); | 328 }); |
324 testServerMain.start(); | 329 testServerMain.start(); |
| 330 return completer.future; |
325 } | 331 } |
326 | 332 |
327 void testContentType() { | 333 Future testContentType() { |
| 334 Completer completer = new Completer(); |
328 TestServerMain testServerMain = new TestServerMain(); | 335 TestServerMain testServerMain = new TestServerMain(); |
329 testServerMain.setServerStartedHandler((int port) { | 336 testServerMain.setServerStartedHandler((int port) { |
330 int responses = 0; | 337 int responses = 0; |
331 HttpClient httpClient = new HttpClient(); | 338 HttpClient httpClient = new HttpClient(); |
332 | 339 |
333 void processResponse(HttpClientResponse response) { | 340 void processResponse(HttpClientResponse response) { |
334 Expect.equals(HttpStatus.OK, response.statusCode); | 341 Expect.equals(HttpStatus.OK, response.statusCode); |
335 Expect.equals("text/html; charset=utf-8", | 342 Expect.equals("text/html; charset=utf-8", |
336 response.headers.contentType.toString()); | 343 response.headers.contentType.toString()); |
337 Expect.equals("text/html", response.headers.contentType.value); | 344 Expect.equals("text/html", response.headers.contentType.value); |
338 Expect.equals("text", response.headers.contentType.primaryType); | 345 Expect.equals("text", response.headers.contentType.primaryType); |
339 Expect.equals("html", response.headers.contentType.subType); | 346 Expect.equals("html", response.headers.contentType.subType); |
340 Expect.equals("utf-8", | 347 Expect.equals("utf-8", |
341 response.headers.contentType.parameters["charset"]); | 348 response.headers.contentType.parameters["charset"]); |
342 responses++; | 349 responses++; |
343 if (responses == 2) { | 350 if (responses == 2) { |
344 httpClient.shutdown(); | 351 httpClient.shutdown(); |
345 testServerMain.shutdown(); | 352 testServerMain.shutdown(); |
| 353 completer.complete(true); |
346 } | 354 } |
347 } | 355 } |
348 | 356 |
349 HttpClientConnection conn1 = | 357 HttpClientConnection conn1 = |
350 httpClient.get("127.0.0.1", port, "/contenttype1"); | 358 httpClient.get("127.0.0.1", port, "/contenttype1"); |
351 conn1.onRequest = (HttpClientRequest request) { | 359 conn1.onRequest = (HttpClientRequest request) { |
352 ContentType contentType = new ContentType(); | 360 ContentType contentType = new ContentType(); |
353 contentType.value = "text/html"; | 361 contentType.value = "text/html"; |
354 contentType.parameters["charset"] = "utf-8"; | 362 contentType.parameters["charset"] = "utf-8"; |
355 request.headers.contentType = contentType; | 363 request.headers.contentType = contentType; |
356 request.outputStream.close(); | 364 request.outputStream.close(); |
357 }; | 365 }; |
358 conn1.onResponse = (HttpClientResponse response) { | 366 conn1.onResponse = (HttpClientResponse response) { |
359 processResponse(response); | 367 processResponse(response); |
360 }; | 368 }; |
361 HttpClientConnection conn2 = | 369 HttpClientConnection conn2 = |
362 httpClient.get("127.0.0.1", port, "/contenttype2"); | 370 httpClient.get("127.0.0.1", port, "/contenttype2"); |
363 conn2.onRequest = (HttpClientRequest request) { | 371 conn2.onRequest = (HttpClientRequest request) { |
364 request.headers.set(HttpHeaders.CONTENT_TYPE, | 372 request.headers.set(HttpHeaders.CONTENT_TYPE, |
365 "text/html; charset = utf-8"); | 373 "text/html; charset = utf-8"); |
366 request.outputStream.close(); | 374 request.outputStream.close(); |
367 }; | 375 }; |
368 conn2.onResponse = (HttpClientResponse response) { | 376 conn2.onResponse = (HttpClientResponse response) { |
369 processResponse(response); | 377 processResponse(response); |
370 }; | 378 }; |
371 }); | 379 }); |
372 testServerMain.start(); | 380 testServerMain.start(); |
| 381 return completer.future; |
373 } | 382 } |
374 | 383 |
375 void testCookies() { | 384 Future testCookies() { |
| 385 Completer completer = new Completer(); |
376 TestServerMain testServerMain = new TestServerMain(); | 386 TestServerMain testServerMain = new TestServerMain(); |
377 testServerMain.setServerStartedHandler((int port) { | 387 testServerMain.setServerStartedHandler((int port) { |
378 int responses = 0; | 388 int responses = 0; |
379 HttpClient httpClient = new HttpClient(); | 389 HttpClient httpClient = new HttpClient(); |
380 | 390 |
381 HttpClientConnection conn1 = | 391 HttpClientConnection conn1 = |
382 httpClient.get("127.0.0.1", port, "/cookie1"); | 392 httpClient.get("127.0.0.1", port, "/cookie1"); |
383 conn1.onResponse = (HttpClientResponse response) { | 393 conn1.onResponse = (HttpClientResponse response) { |
384 Expect.equals(2, response.cookies.length); | 394 Expect.equals(2, response.cookies.length); |
385 response.cookies.forEach((cookie) { | 395 response.cookies.forEach((cookie) { |
(...skipping 15 matching lines...) Expand all Loading... |
401 HttpClientConnection conn2 = | 411 HttpClientConnection conn2 = |
402 httpClient.get("127.0.0.1", port, "/cookie2"); | 412 httpClient.get("127.0.0.1", port, "/cookie2"); |
403 conn2.onRequest = (HttpClientRequest request) { | 413 conn2.onRequest = (HttpClientRequest request) { |
404 request.cookies.add(response.cookies[0]); | 414 request.cookies.add(response.cookies[0]); |
405 request.cookies.add(response.cookies[1]); | 415 request.cookies.add(response.cookies[1]); |
406 request.outputStream.close(); | 416 request.outputStream.close(); |
407 }; | 417 }; |
408 conn2.onResponse = (HttpClientResponse ignored) { | 418 conn2.onResponse = (HttpClientResponse ignored) { |
409 httpClient.shutdown(); | 419 httpClient.shutdown(); |
410 testServerMain.shutdown(); | 420 testServerMain.shutdown(); |
| 421 completer.complete(true); |
411 }; | 422 }; |
412 }; | 423 }; |
413 }); | 424 }); |
414 testServerMain.start(); | 425 testServerMain.start(); |
| 426 return completer.future; |
415 } | 427 } |
416 | 428 |
417 void testFlush() { | 429 Future testFlush() { |
| 430 Completer completer = new Completer(); |
418 TestServerMain testServerMain = new TestServerMain(); | 431 TestServerMain testServerMain = new TestServerMain(); |
419 testServerMain.setServerStartedHandler((int port) { | 432 testServerMain.setServerStartedHandler((int port) { |
420 HttpClient httpClient = new HttpClient(); | 433 HttpClient httpClient = new HttpClient(); |
421 | 434 |
422 HttpClientConnection conn = httpClient.get("127.0.0.1", port, "/flush"); | 435 HttpClientConnection conn = httpClient.get("127.0.0.1", port, "/flush"); |
423 conn.onRequest = (HttpClientRequest request) { | 436 conn.onRequest = (HttpClientRequest request) { |
424 request.outputStream.flush(); | 437 request.outputStream.flush(); |
425 request.outputStream.close(); | 438 request.outputStream.close(); |
426 }; | 439 }; |
427 conn.onResponse = (HttpClientResponse response) { | 440 conn.onResponse = (HttpClientResponse response) { |
428 Expect.equals(HttpStatus.OK, response.statusCode); | 441 Expect.equals(HttpStatus.OK, response.statusCode); |
429 httpClient.shutdown(); | 442 httpClient.shutdown(); |
430 testServerMain.shutdown(); | 443 testServerMain.shutdown(); |
| 444 completer.complete(true); |
431 }; | 445 }; |
432 }); | 446 }); |
433 testServerMain.start(); | 447 testServerMain.start(); |
| 448 return completer.future; |
434 } | 449 } |
435 | 450 |
436 void main() { | 451 void main() { |
437 testHost(); | 452 print('testHost()'); |
438 testExpires(); | 453 testHost().chain((_) { |
439 testContentType(); | 454 print('testExpires()'); |
440 testCookies(); | 455 return testExpires().chain((_) { |
441 testFlush(); | 456 print('testContentType()'); |
| 457 return testContentType().chain((_) { |
| 458 print('testCookies()'); |
| 459 return testCookies().chain((_) { |
| 460 print('testFlush()'); |
| 461 return testFlush(); |
| 462 }); |
| 463 }); |
| 464 }); |
| 465 }).then((_) { |
| 466 print('done'); |
| 467 }); |
442 } | 468 } |
OLD | NEW |