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:async"; | 6 import "dart:async"; |
7 | 7 |
8 import "package:async_helper/async_helper.dart"; | 8 import "package:async_helper/async_helper.dart"; |
9 import "package:expect/expect.dart"; | 9 import "package:expect/expect.dart"; |
10 | 10 |
11 Future testGoogleUrl(SecurityContext context, String outcome) async { | 11 Future testGoogleUrl(SecurityContext context, String outcome) async { |
12 var client = new HttpClient(context: context); | 12 var client = new HttpClient(context: context); |
13 // We need to use an external server that is backed by a | 13 // We need to use an external server that is backed by a |
14 // built-in root certificate authority. | 14 // built-in root certificate authority. |
15 try { | 15 try { |
16 // First, check if the lookup works. | 16 // First, check if the lookup works. |
17 await InternetAddress.lookup('www.google.com'); | 17 await InternetAddress.lookup('www.google.com'); |
18 var request = await client.getUrl(Uri.parse('https://www.google.com')); | 18 var request = await client.getUrl(Uri.parse('https://www.google.com')); |
19 request.followRedirects = false; | 19 request.followRedirects = false; |
20 var response = await request.close(); | 20 var response = await request.close(); |
21 Expect.equals('pass', outcome, 'Unexpected successful connection'); | 21 Expect.equals('pass', outcome, 'Unexpected successful connection'); |
22 try { await response.drain(); } catch (e) { } | 22 try { await response.drain(); } catch (e) { } |
23 } on HandshakeException { | 23 } on HandshakeException { |
24 Expect.equals('fail', outcome, 'Unexpected failed connection'); | 24 Expect.equals('fail', outcome, 'Unexpected failed connection'); |
25 } on SocketException { | 25 } on SocketException { |
26 // Lookup failed or connection failed. Don't report a failure. | 26 // Lookup failed or connection failed. Don't report a failure. |
27 } finally { | 27 } finally { |
28 client.close(); | 28 client.close(); |
29 } | 29 } |
30 } | 30 } |
31 | 31 |
32 | 32 |
33 main() async { | 33 main() async { |
34 asyncStart(); | 34 asyncStart(); |
35 await testGoogleUrl(null, "pass"); | 35 await testGoogleUrl(null, "pass"); |
36 await testGoogleUrl(SecurityContext.defaultContext, "pass"); | 36 await testGoogleUrl(SecurityContext.defaultContext, "pass"); |
37 await testGoogleUrl(new SecurityContext(), "fail"); | 37 await testGoogleUrl(new SecurityContext(), "fail"); |
38 asyncEnd(); | 38 asyncEnd(); |
39 } | 39 } |
OLD | NEW |