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