mirror of https://github.com/winsw/winsw
				
				
				
			Unit tests for custom proxy server
							parent
							
								
									c45a316008
								
							
						
					
					
						commit
						d03e80cea6
					
				| 
						 | 
				
			
			@ -153,24 +153,14 @@ namespace winsw
 | 
			
		|||
            WebRequest request = WebRequest.Create(From);
 | 
			
		||||
            if (!string.IsNullOrEmpty(Proxy))
 | 
			
		||||
            {
 | 
			
		||||
                if (Proxy.Contains("@"))
 | 
			
		||||
                CustomProxyInformation proxyInformation = new CustomProxyInformation(Proxy);
 | 
			
		||||
                if (proxyInformation.Credentials != null)
 | 
			
		||||
                {
 | 
			
		||||
                    // Extract proxy credentials
 | 
			
		||||
                    int credsFrom = Proxy.IndexOf("://") + 3;
 | 
			
		||||
                    int credsTo = Proxy.IndexOf("@");
 | 
			
		||||
                    string completeCredsStr = Proxy.Substring(credsFrom, credsTo - credsFrom);
 | 
			
		||||
                    int credsSeparator = completeCredsStr.IndexOf(":");
 | 
			
		||||
 | 
			
		||||
                    string proxyAddress = Proxy.Replace(completeCredsStr + "@", "");
 | 
			
		||||
                    string username = completeCredsStr.Substring(0, credsSeparator);
 | 
			
		||||
                    string password = completeCredsStr.Substring(credsSeparator + 1);
 | 
			
		||||
                    ICredentials credentials = new NetworkCredential(username, password);
 | 
			
		||||
 | 
			
		||||
                    request.Proxy = new WebProxy(proxyAddress, false, null, credentials);
 | 
			
		||||
                    request.Proxy = new WebProxy(proxyInformation.ServerAddress, false, null, proxyInformation.Credentials);
 | 
			
		||||
                }
 | 
			
		||||
                else
 | 
			
		||||
                {
 | 
			
		||||
                    request.Proxy = new WebProxy(Proxy);
 | 
			
		||||
                    request.Proxy = new WebProxy(proxyInformation.ServerAddress);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -246,8 +236,8 @@ namespace winsw
 | 
			
		|||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
#if NET20
 | 
			
		||||
 | 
			
		||||
#if NET20
 | 
			
		||||
        private static void CopyStream(Stream source, Stream destination)
 | 
			
		||||
        {
 | 
			
		||||
            byte[] buffer = new byte[8192];
 | 
			
		||||
| 
						 | 
				
			
			@ -259,4 +249,31 @@ namespace winsw
 | 
			
		|||
        }
 | 
			
		||||
#endif
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public class CustomProxyInformation
 | 
			
		||||
    {
 | 
			
		||||
        public string ServerAddress { get; set; }
 | 
			
		||||
        public NetworkCredential? Credentials { get; set; }
 | 
			
		||||
 | 
			
		||||
        public CustomProxyInformation(string proxy)
 | 
			
		||||
        {
 | 
			
		||||
            if (proxy.Contains("@"))
 | 
			
		||||
            {
 | 
			
		||||
                // Extract proxy credentials
 | 
			
		||||
                int credsFrom = proxy.IndexOf("://") + 3;
 | 
			
		||||
                int credsTo = proxy.LastIndexOf("@");
 | 
			
		||||
                string completeCredsStr = proxy.Substring(credsFrom, credsTo - credsFrom);
 | 
			
		||||
                int credsSeparator = completeCredsStr.IndexOf(":");
 | 
			
		||||
 | 
			
		||||
                string username = completeCredsStr.Substring(0, credsSeparator);
 | 
			
		||||
                string password = completeCredsStr.Substring(credsSeparator + 1);
 | 
			
		||||
                Credentials = new NetworkCredential(username, password);
 | 
			
		||||
                ServerAddress = proxy.Replace(completeCredsStr + "@", "");
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                ServerAddress = proxy;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -202,6 +202,35 @@ namespace winswTests
 | 
			
		|||
            Assert.That(() => GetSingleEntry(sd), Throws.TypeOf<InvalidDataException>().With.Message.StartsWith("Cannot parse <auth> Enum value from string 'digest'"));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [TestCase("http://", "127.0.0.1:80", "egarcia", "Passw0rd")]
 | 
			
		||||
        [TestCase("https://", "myurl.com.co:2298", "MyUsername", "P@ssw:rd")]
 | 
			
		||||
        [TestCase("http://", "192.168.0.8:3030")]
 | 
			
		||||
        public void Proxy_Credentials(string protocol, string address, string username = null, string password = null)
 | 
			
		||||
        {
 | 
			
		||||
            CustomProxyInformation cpi;
 | 
			
		||||
            if (string.IsNullOrEmpty(username))
 | 
			
		||||
            {
 | 
			
		||||
                cpi = new CustomProxyInformation(protocol + address + "/");
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                cpi = new CustomProxyInformation(protocol + username + ":" + password + "@" + address + "/");
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            Assert.That(cpi.ServerAddress, Is.EqualTo(protocol + address + "/"));
 | 
			
		||||
 | 
			
		||||
            if (string.IsNullOrEmpty(username))
 | 
			
		||||
            {
 | 
			
		||||
                Assert.IsNull(cpi.Credentials);
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                Assert.IsNotNull(cpi.Credentials);
 | 
			
		||||
                Assert.That(cpi.Credentials.UserName, Is.EqualTo(username));
 | 
			
		||||
                Assert.That(cpi.Credentials.Password, Is.EqualTo(password));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private Download GetSingleEntry(ServiceDescriptor sd)
 | 
			
		||||
        {
 | 
			
		||||
            var downloads = sd.Downloads.ToArray();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue