Skip to content

Commit

Permalink
Added more tests for WebProxyClient
Browse files Browse the repository at this point in the history
  • Loading branch information
jstedfast committed Dec 10, 2024
1 parent 402bbb2 commit a82daa2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
25 changes: 23 additions & 2 deletions MailKit/Net/Proxy/WebProxyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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}.");
}

/// <summary>
Expand Down
23 changes: 23 additions & 0 deletions UnitTests/Net/Proxy/WebProxyClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@ public void TestArgumentExceptions ()
Assert.ThrowsAsync<ArgumentOutOfRangeException> (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<NotSupportedException> (() => WebProxyClient.GetProxyClient (new Uri (proxyUri), credentials));
}
}

[Test]
public void TestConnect ()
{
Expand Down

0 comments on commit a82daa2

Please sign in to comment.