Skip to content

Commit 2aa7d70

Browse files
committed
Added automatic detection of the host type
1 parent 2af04d0 commit 2aa7d70

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/DotNext.Tests/Net/Http/HttpEndPointTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,13 @@ static bool TryParse<T>(string input, out T result)
108108
where T : IParsable<T>
109109
=> T.TryParse(input, provider: null, out result);
110110
}
111+
112+
[Theory]
113+
[InlineData("192.168.0.1", AddressFamily.InterNetwork)]
114+
[InlineData("2001:0db8:0000:0000:0000:8a2e:0370:7334", AddressFamily.InterNetworkV6)]
115+
[InlineData("host", AddressFamily.Unspecified)]
116+
public static void AutoDetectAddressFamily(string hostName, AddressFamily expected)
117+
{
118+
Equal(expected, new HttpEndPoint(hostName, 8080, false).AddressFamily);
119+
}
111120
}

src/DotNext/Net/Http/HttpEndPoint.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public HttpEndPoint(Uri uri)
3030
/// <param name="secure"><see langword="true"/> for HTTPS; <see langword="false"/> for HTTP.</param>
3131
/// <param name="family">The type of the host name.</param>
3232
public HttpEndPoint(string hostName, int port, bool secure, AddressFamily family = AddressFamily.Unspecified)
33-
: base(hostName, port, family)
33+
: base(hostName, port, family is AddressFamily.Unspecified ? ToAddressFamily(hostName) : family)
3434
=> IsSecure = secure;
3535

3636
/// <summary>
@@ -69,6 +69,8 @@ private static int GetPort(Uri uri, out bool secure)
6969
_ => AddressFamily.Unspecified,
7070
};
7171

72+
private static AddressFamily ToAddressFamily(string? hostName) => ToAddressFamily(Uri.CheckHostName(hostName));
73+
7274
/// <summary>
7375
/// Gets a value indicating that HTTP over TLS should be used (HTTPS).
7476
/// </summary>

0 commit comments

Comments
 (0)