| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library http_multi_server.test; | 5 library http_multi_server.test; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:http/http.dart' as http; | 10 import 'package:http/http.dart' as http; |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 | 101 |
| 102 test("listens on all localhost interfaces", () { | 102 test("listens on all localhost interfaces", () { |
| 103 server.listen((request) { | 103 server.listen((request) { |
| 104 request.response.write("got request"); | 104 request.response.write("got request"); |
| 105 request.response.close(); | 105 request.response.close(); |
| 106 }); | 106 }); |
| 107 | 107 |
| 108 expect(http.read("http://127.0.0.1:${server.port}/"), | 108 expect(http.read("http://127.0.0.1:${server.port}/"), |
| 109 completion(equals("got request"))); | 109 completion(equals("got request"))); |
| 110 | 110 |
| 111 return supportsIpV6.then((supportsIpV6) { | 111 return _supportsIpV6.then((supportsIpV6) { |
| 112 if (!supportsIpV6) return; | 112 if (!supportsIpV6) return; |
| 113 expect(http.read("http://[::1]:${server.port}/"), | 113 expect(http.read("http://[::1]:${server.port}/"), |
| 114 completion(equals("got request"))); | 114 completion(equals("got request"))); |
| 115 }); | 115 }); |
| 116 }); | 116 }); |
| 117 }); | 117 }); |
| 118 } | 118 } |
| 119 | 119 |
| 120 /// A cache for [supportsIpV6]. |
| 121 bool _supportsIpV6Cache; |
| 122 |
| 123 // TODO(nweiz): This is known to be inaccurate on Windows machines with IPv6 |
| 124 // disabled (issue 19815). Tests will fail on such machines. |
| 125 /// Returns whether this computer supports binding to IPv6 addresses. |
| 126 Future<bool> get _supportsIpV6 { |
| 127 if (_supportsIpV6Cache != null) return new Future.value(_supportsIpV6Cache); |
| 128 |
| 129 return ServerSocket.bind(InternetAddress.LOOPBACK_IP_V6, 0).then((socket) { |
| 130 _supportsIpV6Cache = true; |
| 131 socket.close(); |
| 132 return true; |
| 133 }).catchError((error) { |
| 134 if (error is! SocketException) throw error; |
| 135 _supportsIpV6Cache = false; |
| 136 return false; |
| 137 }); |
| 138 } |
| 139 |
| 120 /// Makes a GET request to the root of [server] and returns the response. | 140 /// Makes a GET request to the root of [server] and returns the response. |
| 121 Future<http.Response> _get(HttpServer server) => http.get(_urlFor(server)); | 141 Future<http.Response> _get(HttpServer server) => http.get(_urlFor(server)); |
| 122 | 142 |
| 123 /// Makes a GET request to the root of [server] and returns the response body. | 143 /// Makes a GET request to the root of [server] and returns the response body. |
| 124 Future<String> _read(HttpServer server) => http.read(_urlFor(server)); | 144 Future<String> _read(HttpServer server) => http.read(_urlFor(server)); |
| 125 | 145 |
| 126 /// Returns the URL for the root of [server]. | 146 /// Returns the URL for the root of [server]. |
| 127 String _urlFor(HttpServer server) => | 147 String _urlFor(HttpServer server) => |
| 128 "http://${server.address.host}:${server.port}/"; | 148 "http://${server.address.host}:${server.port}/"; |
| OLD | NEW |