From 2b11ad4585f9caaa050b58077da56bca37764049 Mon Sep 17 00:00:00 2001 From: Lloyd Sparkes Date: Mon, 2 May 2016 11:24:51 +0100 Subject: [PATCH] Proxy BypassList tests Lightly refactored and updated documentation to make it easier for users --- .../Http/Proxy/HttpProxySettings.cs | 12 +++++- .../Http/Proxy/ManagedWebProxyFactory.cs | 4 +- .../Http/HttpProxySettingsProviderFixture.cs | 40 +++++++++++++++++++ .../NzbDrone.Core.Test.csproj | 1 + .../Http/HttpProxySettingsProvider.cs | 2 +- .../Settings/General/GeneralViewTemplate.hbs | 2 +- 6 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 src/NzbDrone.Core.Test/Http/HttpProxySettingsProviderFixture.cs diff --git a/src/NzbDrone.Common/Http/Proxy/HttpProxySettings.cs b/src/NzbDrone.Common/Http/Proxy/HttpProxySettings.cs index 0c2e3ce3d..84664bf8a 100644 --- a/src/NzbDrone.Common/Http/Proxy/HttpProxySettings.cs +++ b/src/NzbDrone.Common/Http/Proxy/HttpProxySettings.cs @@ -25,13 +25,21 @@ namespace NzbDrone.Common.Http.Proxy public string BypassFilter { get; private set; } public bool BypassLocalAddress { get; private set; } - public string[] SubnetFilterAsArray + public string[] BypassListAsArray { get { if (!string.IsNullOrWhiteSpace(BypassFilter)) { - return BypassFilter.Split(';'); + var hostlist = BypassFilter.Split(','); + for(int i = 0; i < hostlist.Length; i++) + { + if(hostlist[i].StartsWith("*")) + { + hostlist[i] = ";" + hostlist[i]; + } + } + return hostlist; } return new string[] { }; } diff --git a/src/NzbDrone.Common/Http/Proxy/ManagedWebProxyFactory.cs b/src/NzbDrone.Common/Http/Proxy/ManagedWebProxyFactory.cs index a2992131d..163435e84 100644 --- a/src/NzbDrone.Common/Http/Proxy/ManagedWebProxyFactory.cs +++ b/src/NzbDrone.Common/Http/Proxy/ManagedWebProxyFactory.cs @@ -40,11 +40,11 @@ namespace NzbDrone.Common.Http.Proxy case ProxyType.Http: if (proxySettings.Username.IsNotNullOrWhiteSpace() && proxySettings.Password.IsNotNullOrWhiteSpace()) { - return new WebProxy(proxySettings.Host + ":" + proxySettings.Port, proxySettings.BypassLocalAddress, proxySettings.SubnetFilterAsArray, new NetworkCredential(proxySettings.Username, proxySettings.Password)); + return new WebProxy(proxySettings.Host + ":" + proxySettings.Port, proxySettings.BypassLocalAddress, proxySettings.BypassListAsArray, new NetworkCredential(proxySettings.Username, proxySettings.Password)); } else { - return new WebProxy(proxySettings.Host + ":" + proxySettings.Port, proxySettings.BypassLocalAddress, proxySettings.SubnetFilterAsArray); + return new WebProxy(proxySettings.Host + ":" + proxySettings.Port, proxySettings.BypassLocalAddress, proxySettings.BypassListAsArray); } case ProxyType.Socks4: return new SocksWebProxy(new ProxyConfig(IPAddress.Loopback, GetNextFreePort(), GetProxyIpAddress(proxySettings.Host), proxySettings.Port, ProxyConfig.SocksVersion.Four, proxySettings.Username, proxySettings.Password), false); diff --git a/src/NzbDrone.Core.Test/Http/HttpProxySettingsProviderFixture.cs b/src/NzbDrone.Core.Test/Http/HttpProxySettingsProviderFixture.cs new file mode 100644 index 000000000..20679a417 --- /dev/null +++ b/src/NzbDrone.Core.Test/Http/HttpProxySettingsProviderFixture.cs @@ -0,0 +1,40 @@ +using NzbDrone.Core.Http; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NUnit.Framework; +using FluentAssertions; +using NzbDrone.Test.Common; +using NzbDrone.Common.Http.Proxy; +using NzbDrone.Common.Http; + +namespace NzbDrone.Core.Test.Http +{ + [TestFixture] + public class HttpProxySettingsProviderFixture : TestBase + { + private HttpProxySettings GetProxySettings() + { + return new HttpProxySettings(ProxyType.Socks5, "localhost", 8080, "*.httpbin.org,google.com", true, null, null); + } + + [Test] + public void should_bypass_proxy() + { + var settings = GetProxySettings(); + + Subject.ShouldProxyBeBypassed(settings, new HttpUri("http://eu.httpbin.org/get")).Should().BeTrue(); + Subject.ShouldProxyBeBypassed(settings, new HttpUri("http://google.com/get")).Should().BeTrue(); + Subject.ShouldProxyBeBypassed(settings, new HttpUri("http://localhost:8654/get")).Should().BeTrue(); + } + + [Test] + public void should_not_bypass_proxy() + { + var settings = GetProxySettings(); + + Subject.ShouldProxyBeBypassed(settings, new HttpUri("http://bing.com/get")).Should().BeFalse(); + } + } +} diff --git a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index fa4a78235..9ac293d55 100644 --- a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -217,6 +217,7 @@ + diff --git a/src/NzbDrone.Core/Http/HttpProxySettingsProvider.cs b/src/NzbDrone.Core/Http/HttpProxySettingsProvider.cs index 0ca11bf12..6e5a23e07 100644 --- a/src/NzbDrone.Core/Http/HttpProxySettingsProvider.cs +++ b/src/NzbDrone.Core/Http/HttpProxySettingsProvider.cs @@ -41,7 +41,7 @@ namespace NzbDrone.Core.Http public bool ShouldProxyBeBypassed(HttpProxySettings proxySettings, HttpUri url) { //We are utilising the WebProxy implementation here to save us having to reimplement it. This way we use Microsofts implementation - var proxy = new WebProxy(proxySettings.Host + ":" + proxySettings.Port, proxySettings.BypassLocalAddress, proxySettings.SubnetFilterAsArray); + var proxy = new WebProxy(proxySettings.Host + ":" + proxySettings.Port, proxySettings.BypassLocalAddress, proxySettings.BypassListAsArray); return proxy.IsBypassed((Uri)url); } diff --git a/src/UI/Settings/General/GeneralViewTemplate.hbs b/src/UI/Settings/General/GeneralViewTemplate.hbs index 99b5930bc..b22abe9a6 100644 --- a/src/UI/Settings/General/GeneralViewTemplate.hbs +++ b/src/UI/Settings/General/GeneralViewTemplate.hbs @@ -242,7 +242,7 @@
- +