| 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 #import("dart:io"); | |
| 6 #import("dart:uri"); | |
| 7 #import("dart:isolate"); | |
| 8 | |
| 9 void testGoogle() { | |
| 10 HttpClient client = new HttpClient(); | |
| 11 var conn = client.get('www.google.com', 80, '/'); | |
| 12 | |
| 13 conn.onRequest = (HttpClientRequest request) { | |
| 14 request.outputStream.close(); | |
| 15 }; | |
| 16 conn.onResponse = (HttpClientResponse response) { | |
| 17 Expect.isTrue(response.statusCode < 500); | |
| 18 response.inputStream.onData = () { | |
| 19 response.inputStream.read(); | |
| 20 }; | |
| 21 response.inputStream.onClosed = () { | |
| 22 client.shutdown(); | |
| 23 }; | |
| 24 }; | |
| 25 conn.onError = (error) => Expect.fail("Unexpected IO error"); | |
| 26 } | |
| 27 | |
| 28 void testGoogleUrl() { | |
| 29 HttpClient client = new HttpClient(); | |
| 30 | |
| 31 void testUrl(String url) { | |
| 32 var conn = client.getUrl(new Uri.fromString(url)); | |
| 33 | |
| 34 conn.onRequest = (HttpClientRequest request) { | |
| 35 request.outputStream.close(); | |
| 36 }; | |
| 37 conn.onResponse = (HttpClientResponse response) { | |
| 38 Expect.isTrue(response.statusCode < 500); | |
| 39 response.inputStream.onData = () { | |
| 40 response.inputStream.read(); | |
| 41 }; | |
| 42 response.inputStream.onClosed = () { | |
| 43 client.shutdown(); | |
| 44 }; | |
| 45 }; | |
| 46 conn.onError = (error) => Expect.fail("Unexpected IO error"); | |
| 47 } | |
| 48 | |
| 49 testUrl('http://www.google.com'); | |
| 50 testUrl('http://www.google.com/abc'); | |
| 51 testUrl('http://www.google.com/?abc'); | |
| 52 testUrl('http://www.google.com/abc?abc'); | |
| 53 testUrl('http://www.google.com/abc?abc#abc'); | |
| 54 } | |
| 55 | |
| 56 void testInvalidUrl() { | |
| 57 HttpClient client = new HttpClient(); | |
| 58 Expect.throws( | |
| 59 () => client.getUrl(new Uri.fromString('ftp://www.google.com'))); | |
| 60 Expect.throws( | |
| 61 () => client.getUrl(new Uri.fromString('http://usr:pwd@www.google.com'))); | |
| 62 } | |
| 63 | |
| 64 void testBadHostName() { | |
| 65 HttpClient client = new HttpClient(); | |
| 66 HttpClientConnection connection = | |
| 67 client.get("some.bad.host.name.7654321", 0, "/"); | |
| 68 connection.onRequest = (HttpClientRequest request) { | |
| 69 Expect.fail("Should not open a request on bad hostname"); | |
| 70 }; | |
| 71 ReceivePort port = new ReceivePort(); | |
| 72 connection.onError = (Exception error) { | |
| 73 port.close(); // We expect onError to be called, due to bad host name. | |
| 74 }; | |
| 75 } | |
| 76 | |
| 77 void main() { | |
| 78 // TODO(sgjesse): Making empty www.google.com requests seems to fail | |
| 79 //on buildbot. | |
| 80 //testGoogle(); | |
| 81 //testGoogleUrl(); | |
| 82 testInvalidUrl(); | |
| 83 testBadHostName(); | |
| 84 } | |
| OLD | NEW |