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 import "dart:io"; | 5 import "dart:io"; |
6 import "dart:uri"; | 6 import "dart:uri"; |
7 import "dart:isolate"; | 7 import "dart:isolate"; |
8 | 8 |
9 | 9 |
10 int testGoogleUrlCount = 0; | |
11 void testGoogleUrl() { | 10 void testGoogleUrl() { |
| 11 int testsStarted = 0; |
| 12 int testsFinished = 0; |
| 13 bool allStarted = false; |
12 HttpClient client = new HttpClient(); | 14 HttpClient client = new HttpClient(); |
13 | 15 |
14 void testUrl(String url) { | 16 void testUrl(String url) { |
| 17 testsStarted++; |
15 var requestUri = Uri.parse(url); | 18 var requestUri = Uri.parse(url); |
16 var conn = client.getUrl(requestUri); | 19 client.getUrl(requestUri) |
17 | 20 .then((HttpClientRequest request) => request.close()) |
18 conn.onRequest = (HttpClientRequest request) { | 21 .then((HttpClientResponse response) { |
19 request.outputStream.close(); | 22 Expect.isTrue(response.statusCode < 500); |
20 }; | 23 if (requestUri.path.length == 0) { |
21 conn.onResponse = (HttpClientResponse response) { | 24 Expect.isTrue(response.statusCode != 404); |
22 testGoogleUrlCount++; | 25 } |
23 Expect.isTrue(response.statusCode < 500); | 26 response.listen((data) { }, onDone: () { |
24 if (requestUri.path.length == 0) { | 27 if (++testsFinished == testsStarted && allStarted) client.close(); |
25 Expect.isTrue(response.statusCode != 404); | 28 }); |
26 } | 29 }) |
27 response.inputStream.onData = () { | 30 .catchError((error) => Expect.fail("Unexpected IO error: $error")); |
28 response.inputStream.read(); | |
29 }; | |
30 response.inputStream.onClosed = () { | |
31 if (testGoogleUrlCount == 4) client.shutdown(); | |
32 }; | |
33 }; | |
34 conn.onError = (error) => Expect.fail("Unexpected IO error $error"); | |
35 } | 31 } |
36 | 32 |
37 testUrl('https://www.google.dk'); | 33 testUrl('https://www.google.dk'); |
38 testUrl('https://www.google.dk'); | 34 testUrl('https://www.google.dk'); |
39 testUrl('https://www.google.dk/#q=foo'); | 35 testUrl('https://www.google.dk/#q=foo'); |
40 testUrl('https://www.google.dk/#hl=da&q=foo'); | 36 testUrl('https://www.google.dk/#hl=da&q=foo'); |
| 37 allStarted = true; |
41 } | 38 } |
42 | 39 |
43 void testBadHostName() { | 40 void testBadHostName() { |
44 HttpClient client = new HttpClient(); | 41 HttpClient client = new HttpClient(); |
45 HttpClientConnection connection = client.getUrl( | |
46 Uri.parse("https://some.bad.host.name.7654321/")); | |
47 connection.onRequest = (HttpClientRequest request) { | |
48 Expect.fail("Should not open a request on bad hostname"); | |
49 }; | |
50 ReceivePort port = new ReceivePort(); | 42 ReceivePort port = new ReceivePort(); |
51 connection.onError = (Exception error) { | 43 client.getUrl(Uri.parse("https://some.bad.host.name.7654321/")) |
52 port.close(); // We expect onError to be called, due to bad host name. | 44 .then((HttpClientRequest request) { |
53 }; | 45 Expect.fail("Should not open a request on bad hostname"); |
| 46 }) |
| 47 .catchError((error) { |
| 48 port.close(); // Should throw an error on bad hostname. |
| 49 }); |
54 } | 50 } |
55 | 51 |
56 void InitializeSSL() { | 52 void InitializeSSL() { |
57 SecureSocket.initialize(); | 53 SecureSocket.initialize(); |
58 } | 54 } |
59 | 55 |
60 void main() { | 56 void main() { |
61 testGoogleUrl(); | 57 testGoogleUrl(); |
62 testBadHostName(); | 58 testBadHostName(); |
63 Expect.throws(InitializeSSL); | 59 Expect.throws(InitializeSSL); |
64 } | 60 } |
OLD | NEW |