public CaptchaProxy(CaptchaProxyType type, string address) { Validate(type, address); Type = type; Address = address; }
public CaptchaProxy(string type, string address) { bool typeIsValid = Enum.TryParse(type.Trim().ToUpper(), out CaptchaProxyType proxyType); if (!typeIsValid) { throw new ArgumentException(@"Proxy type is invalid. Available: HTTP, HTTPS, SOCKS4, SOCKS5", nameof(address)); } Validate(proxyType, address); Type = proxyType; Address = address; }
// ReSharper disable once UnusedParameter.Local private static void Validate(CaptchaProxyType type, string address) { if (string.IsNullOrEmpty(address)) { throw new ArgumentException($@"{nameof(CaptchaProxy)} should contain {nameof(address)}", nameof(address)); } // Simple validate port int portIndex = address.IndexOf(':'); const int minPortLength = 2; if (portIndex == -1 || address.Length - 1 - portIndex < minPortLength) { throw new ArgumentException($@"{nameof(address)} should contain port", nameof(address)); } }
public void CaptchaProxy__Invalid_Address() { const CaptchaProxyType type = CaptchaProxyType.HTTPS; Assert.ThrowsException <ArgumentException>(() => { var cp = new CaptchaProxy(type, null); }); Assert.ThrowsException <ArgumentException>(() => { var cp = new CaptchaProxy(type, string.Empty); }); Assert.ThrowsException <ArgumentException>(() => { var cp = new CaptchaProxy(type, "127.0.0.1"); }); Assert.ThrowsException <ArgumentException>(() => { var cp = new CaptchaProxy(type, "127.0.0.1:"); }); Assert.ThrowsException <ArgumentException>(() => { var cp = new CaptchaProxy(type, "127.0.0.1:8"); }); }