From a82daa221ad3c966484debc051ff548d59bc09b4 Mon Sep 17 00:00:00 2001 From: Jeffrey Stedfast Date: Mon, 9 Dec 2024 19:33:12 -0500 Subject: [PATCH] Added more tests for WebProxyClient --- MailKit/Net/Proxy/WebProxyClient.cs | 25 ++++++++++++++++++++-- UnitTests/Net/Proxy/WebProxyClientTests.cs | 23 ++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/MailKit/Net/Proxy/WebProxyClient.cs b/MailKit/Net/Proxy/WebProxyClient.cs index faabdc6618..fb782fd7bf 100644 --- a/MailKit/Net/Proxy/WebProxyClient.cs +++ b/MailKit/Net/Proxy/WebProxyClient.cs @@ -87,7 +87,7 @@ static NetworkCredential GetNetworkCredential (ICredentials credentials, Uri uri return credentials.GetCredential (uri, "Basic"); } - static ProxyClient GetProxyClient (Uri proxyUri, ICredentials credentials) + internal static ProxyClient GetProxyClient (Uri proxyUri, ICredentials credentials) { var credential = GetNetworkCredential (credentials, proxyUri); @@ -105,7 +105,28 @@ static ProxyClient GetProxyClient (Uri proxyUri, ICredentials credentials) return new HttpProxyClient (proxyUri.Host, proxyUri.Port); } - throw new NotImplementedException ($"The default system proxy does not support {proxyUri.Scheme}."); + if (proxyUri.Scheme.Equals ("socks4", StringComparison.OrdinalIgnoreCase)) { + if (credential != null) + return new Socks4Client (proxyUri.Host, proxyUri.Port, credential); + + return new Socks4Client (proxyUri.Host, proxyUri.Port); + } + + if (proxyUri.Scheme.Equals ("socks4a", StringComparison.OrdinalIgnoreCase)) { + if (credential != null) + return new Socks4aClient (proxyUri.Host, proxyUri.Port, credential); + + return new Socks4aClient (proxyUri.Host, proxyUri.Port); + } + + if (proxyUri.Scheme.Equals ("socks5", StringComparison.OrdinalIgnoreCase)) { + if (credential != null) + return new Socks5Client (proxyUri.Host, proxyUri.Port, credential); + + return new Socks5Client (proxyUri.Host, proxyUri.Port); + } + + throw new NotSupportedException ($"The default system proxy does not support {proxyUri.Scheme}."); } /// diff --git a/UnitTests/Net/Proxy/WebProxyClientTests.cs b/UnitTests/Net/Proxy/WebProxyClientTests.cs index 936b9365d9..da538f4f8e 100644 --- a/UnitTests/Net/Proxy/WebProxyClientTests.cs +++ b/UnitTests/Net/Proxy/WebProxyClientTests.cs @@ -66,6 +66,29 @@ public void TestArgumentExceptions () Assert.ThrowsAsync (async () => await proxy.ConnectAsync ("www.google.com", 80, -ConnectTimeout)); } + [TestCase ("http://proxy:8080", null, null, typeof (HttpProxyClient))] + [TestCase ("http://proxy:8080", "user", "password", typeof (HttpProxyClient))] + [TestCase ("https://proxy:8080", null, null, typeof (HttpsProxyClient))] + [TestCase ("https://proxy:8080", "user", "password", typeof (HttpsProxyClient))] + [TestCase ("socks4://proxy:1080", null, null, typeof (Socks4Client))] + [TestCase ("socks4://proxy:1080", "user", "password", typeof (Socks4Client))] + [TestCase ("socks4a://proxy:1080", null, null, typeof (Socks4aClient))] + [TestCase ("socks4a://proxy:1080", "user", "password", typeof (Socks4aClient))] + [TestCase ("socks5://proxy:1080", null, null, typeof (Socks5Client))] + [TestCase ("socks5://proxy:1080", "user", "password", typeof (Socks5Client))] + [TestCase ("unsupported://proxy:1080", null, null, null)] + public void TestGetProxyClient (string proxyUri, string user, string password, Type expectedType) + { + var credentials = user != null ? new NetworkCredential (user, password) : null; + + if (expectedType != null) { + var proxy = WebProxyClient.GetProxyClient (new Uri (proxyUri), credentials); + Assert.That (proxy, Is.InstanceOf (expectedType)); + } else { + Assert.Throws (() => WebProxyClient.GetProxyClient (new Uri (proxyUri), credentials)); + } + } + [Test] public void TestConnect () {