| 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:io"; | |
| 11 import "dart:uri"; | |
| 12 import "dart:isolate"; | |
| 13 | |
| 14 void testGoogle() { | |
| 15 HttpClient client = new HttpClient(); | |
| 16 client.get('www.google.com', 80, '/') | |
| 17 .then((request) => request.close()) | |
| 18 .then((response) { | |
| 19 Expect.isTrue(response.statusCode < 500); | |
| 20 response.listen((data) {}, onDone: client.close); | |
| 21 }) | |
| 22 .catchError((error) => Expect.fail("Unexpected IO error: $error")); | |
| 23 } | |
| 24 | |
| 25 int testGoogleUrlCount = 0; | |
| 26 void testGoogleUrl() { | |
| 27 HttpClient client = new HttpClient(); | |
| 28 | |
| 29 void testUrl(String url) { | |
| 30 var requestUri = Uri.parse(url); | |
| 31 client.getUrl(requestUri) | |
| 32 .then((request) => request.close()) | |
| 33 .then((response) { | |
| 34 testGoogleUrlCount++; | |
| 35 Expect.isTrue(response.statusCode < 500); | |
| 36 if (requestUri.path.length == 0) { | |
| 37 Expect.isTrue(response.statusCode != 404); | |
| 38 } | |
| 39 response.listen((data) {}, onDone: () { | |
| 40 if (testGoogleUrlCount == 5) client.close(); | |
| 41 }); | |
| 42 }) | |
| 43 .catchError((error) => Expect.fail("Unexpected IO error: $error")); | |
| 44 } | |
| 45 | |
| 46 testUrl('http://www.google.com'); | |
| 47 testUrl('http://www.google.com/abc'); | |
| 48 testUrl('http://www.google.com/?abc'); | |
| 49 testUrl('http://www.google.com/abc?abc'); | |
| 50 testUrl('http://www.google.com/abc?abc#abc'); | |
| 51 } | |
| 52 | |
| 53 void testInvalidUrl() { | |
| 54 HttpClient client = new HttpClient(); | |
| 55 Expect.throws( | |
| 56 () => client.getUrl(Uri.parse('ftp://www.google.com'))); | |
| 57 } | |
| 58 | |
| 59 void testBadHostName() { | |
| 60 HttpClient client = new HttpClient(); | |
| 61 ReceivePort port = new ReceivePort(); | |
| 62 client.get("some.bad.host.name.7654321", 0, "/") | |
| 63 .then((request) { | |
| 64 Expect.fail("Should not open a request on bad hostname"); | |
| 65 }).catchError((error) { | |
| 66 port.close(); // We expect onError to be called, due to bad host name. | |
| 67 }, test: (error) => error is! String); | |
| 68 } | |
| 69 | |
| 70 void main() { | |
| 71 testGoogle(); | |
| 72 testGoogleUrl(); | |
| 73 testInvalidUrl(); | |
| 74 testBadHostName(); | |
| 75 } | |
| OLD | NEW |