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 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 | 9 |
28 int testGoogleUrlCount = 0; | 10 int testGoogleUrlCount = 0; |
29 void testGoogleUrl() { | 11 void testGoogleUrl() { |
30 HttpClient client = new HttpClient(); | 12 HttpClient client = new HttpClient(); |
31 | 13 |
32 void testUrl(String url) { | 14 void testUrl(String url) { |
33 var requestUri = new Uri.fromString(url); | 15 var requestUri = new Uri.fromString(url); |
34 var conn = client.getUrl(requestUri); | 16 var conn = client.getUrl(requestUri); |
35 | 17 |
36 conn.onRequest = (HttpClientRequest request) { | 18 conn.onRequest = (HttpClientRequest request) { |
37 request.outputStream.close(); | 19 request.outputStream.close(); |
38 }; | 20 }; |
39 conn.onResponse = (HttpClientResponse response) { | 21 conn.onResponse = (HttpClientResponse response) { |
40 testGoogleUrlCount++; | 22 testGoogleUrlCount++; |
41 Expect.isTrue(response.statusCode < 500); | 23 Expect.isTrue(response.statusCode < 500); |
42 if (requestUri.path.length == 0) { | 24 if (requestUri.path.length == 0) { |
43 Expect.isTrue(response.statusCode != 404); | 25 Expect.isTrue(response.statusCode != 404); |
44 } | 26 } |
45 response.inputStream.onData = () { | 27 response.inputStream.onData = () { |
46 response.inputStream.read(); | 28 response.inputStream.read(); |
47 }; | 29 }; |
48 response.inputStream.onClosed = () { | 30 response.inputStream.onClosed = () { |
49 if (testGoogleUrlCount == 5) client.shutdown(); | 31 if (testGoogleUrlCount == 4) client.shutdown(); |
50 }; | 32 }; |
51 }; | 33 }; |
52 conn.onError = (error) => Expect.fail("Unexpected IO error $error"); | 34 conn.onError = (error) => Expect.fail("Unexpected IO error $error"); |
53 } | 35 } |
54 | 36 |
55 testUrl('http://www.google.com'); | 37 testUrl('https://www.google.dk'); |
56 testUrl('http://www.google.com/abc'); | 38 testUrl('https://www.google.dk'); |
57 testUrl('http://www.google.com/?abc'); | 39 testUrl('https://www.google.dk/#q=foo'); |
58 testUrl('http://www.google.com/abc?abc'); | 40 testUrl('https://www.google.dk/#hl=da&q=foo'); |
59 testUrl('http://www.google.com/abc?abc#abc'); | |
60 } | |
61 | |
62 void testInvalidUrl() { | |
63 HttpClient client = new HttpClient(); | |
64 Expect.throws( | |
65 () => client.getUrl(new Uri.fromString('ftp://www.google.com'))); | |
66 } | 41 } |
67 | 42 |
68 void testBadHostName() { | 43 void testBadHostName() { |
69 HttpClient client = new HttpClient(); | 44 HttpClient client = new HttpClient(); |
70 HttpClientConnection connection = | 45 HttpClientConnection connection = client.getUrl( |
71 client.get("some.bad.host.name.7654321", 0, "/"); | 46 new Uri.fromString("https://some.bad.host.name.7654321/")); |
72 connection.onRequest = (HttpClientRequest request) { | 47 connection.onRequest = (HttpClientRequest request) { |
73 Expect.fail("Should not open a request on bad hostname"); | 48 Expect.fail("Should not open a request on bad hostname"); |
74 }; | 49 }; |
75 ReceivePort port = new ReceivePort(); | 50 ReceivePort port = new ReceivePort(); |
76 connection.onError = (Exception error) { | 51 connection.onError = (Exception error) { |
77 port.close(); // We expect onError to be called, due to bad host name. | 52 port.close(); // We expect onError to be called, due to bad host name. |
78 }; | 53 }; |
79 } | 54 } |
80 | 55 |
| 56 void InitializeSSL() { |
| 57 var testPkcertDatabase = |
| 58 new Path.fromNative(new Options().script).directoryPath.append('pkcert/'); |
| 59 SecureSocket.setCertificateDatabase(testPkcertDatabase.toNativePath()); |
| 60 } |
| 61 |
81 void main() { | 62 void main() { |
82 testGoogle(); | 63 InitializeSSL(); |
83 testGoogleUrl(); | 64 testGoogleUrl(); |
84 testInvalidUrl(); | |
85 testBadHostName(); | 65 testBadHostName(); |
86 } | 66 } |
OLD | NEW |