From 8a43be94efca3397f70ec20865d70862d5b94e9e Mon Sep 17 00:00:00 2001 From: Mike Harder Date: Mon, 11 Jul 2016 14:40:12 -0700 Subject: [PATCH] AddressRegistrationTests should get local IPs from System.Net.NetworkInformation (#796) --- .../AddressRegistrationTests.cs | 15 ++++++++++++--- .../project.json | 1 + 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/AddressRegistrationTests.cs b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/AddressRegistrationTests.cs index ee0651920c..b105a35044 100644 --- a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/AddressRegistrationTests.cs +++ b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/AddressRegistrationTests.cs @@ -2,9 +2,11 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; +using System.Net.NetworkInformation; using System.Net.Sockets; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; @@ -166,7 +168,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests dataset.Add("http://127.0.0.1:0/", GetTestUrls); dataset.Add($"http://{Dns.GetHostName()}:0/", GetTestUrls); - var ipv4Addresses = Dns.GetHostAddressesAsync(Dns.GetHostName()).Result + var ipv4Addresses = GetIPAddresses() .Where(ip => ip.AddressFamily == AddressFamily.InterNetwork); foreach (var ip in ipv4Addresses) { @@ -224,7 +226,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests dataset.Add($"http://[::1]:{port1}/base/path", _ => new[] { $"http://[::1]:{port1}/base/path" }); // Dynamic port and non-loopback addresses - var ipv6Addresses = Dns.GetHostAddressesAsync(Dns.GetHostName()).Result + var ipv6Addresses = GetIPAddresses() .Where(ip => ip.AddressFamily == AddressFamily.InterNetworkV6) .Where(ip => ip.ScopeId == 0); foreach (var ip in ipv6Addresses) @@ -258,7 +260,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests var dataset = new TheoryData>(); // Dynamic port - var ipv6Addresses = Dns.GetHostAddressesAsync(Dns.GetHostName()).Result + var ipv6Addresses = GetIPAddresses() .Where(ip => ip.AddressFamily == AddressFamily.InterNetworkV6) .Where(ip => ip.ScopeId != 0); foreach (var ip in ipv6Addresses) @@ -270,6 +272,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests } } + private static IEnumerable GetIPAddresses() + { + return NetworkInterface.GetAllNetworkInterfaces() + .SelectMany(i => i.GetIPProperties().UnicastAddresses) + .Select(a => a.Address); + } + private static string[] GetTestUrls(IServerAddressesFeature addressesFeature) { return addressesFeature.Addresses diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/project.json b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/project.json index 3b8c4e3337..9b247ae340 100644 --- a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/project.json +++ b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/project.json @@ -19,6 +19,7 @@ }, "System.Net.Http": "4.1.0-*", "System.Net.Http.WinHttpHandler": "4.0.0-*", + "System.Net.NetworkInformation": "4.1.0-*", "System.Runtime.Serialization.Primitives": "4.1.1-*" } },