1

I am writing a function to test proxy server connection. User need to fill a web form and need to provide following:

  1. Proxy Server Ip Address
  2. Proxy Server Port
  3. Check a chekbox if authentication is required
  4. If authentication checkbox checked then user need to provide the username and password.

The Proxy Server I am using to unit test needs Authentication.

It will work for both the following scenarios throw exception:

  1. TestProxy.TestProxySettingsWithWebclient(url, "Correct_IP_Address", "Correct_Port", false); // Will throw error as authentication is required
  2. TestProxy.TestProxySettingsWithWebclient(url, "Correct_IP_Address", "Correct_Port", true, "Incorrect_Username", "Incorrect_Password");// Will throw error as authentication is required

The strange behavior I am getting is that when I am doing following:

  1. I call the method with correct IP and correct credentials - TestProxy.TestProxySettingsWithWebclient(url, "Correct_IP_Address", "Correct_Port", true, "Correct_Username", "Correct_Password");. It will not fail as everything is correct.
  2. Then I try with correct IP and with no credentials. TestProxy.TestProxySettingsWithWebclient(url, "Correct_IP_Address", "Correct_Port", false);. It passes and no exception occurs and which is wrong as this proxy server needs authentication. d
  3. Then I try with correct IP and with no credentials. TestProxy.TestProxySettingsWithWebclient(url, "Correct_IP_Address", "Correct_Port", true, "Incorrect_Username", "Incorrect_Password");. It also not throws any exception which is wrong.

Basically once I use the correct IP, correct port, correct username and password then after that if try with same IP and same port but with wrong credentials or, with no credentials the test always pass and no exception occurs. Which is wrong.

Can anybody suggests me what I am doing wrong?

Following is the function which I wrote to test the proxy server connection:

public static bool TestProxySettings(string urlToHit, string proxyAddress, int? port,
        bool isAuthenticationRequired,
        string userName = null, string password = null)
    {
        string url = urlToHit + "?time=" + DateTime.Now.Ticks;
        NetworkCredential networkCredential = null;

        if (port.HasValue)
            proxyAddress += ":" + port.Value;

        if (isAuthenticationRequired)
            networkCredential = new NetworkCredential(userName, password);

        try
        {
            WebProxy proxy = new WebProxy(proxyAddress)
            {
                UseDefaultCredentials = false,
                BypassProxyOnLocal = false,
                Credentials = networkCredential
            };

            using (WebClient client = new WebClient())
            {
                client.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
                client.Proxy = new WebProxy();
                client.Proxy = proxy;
                var downloadString = client.DownloadString(url);
            }
        }
        catch (WebException ex)
        {
            // ex.Response will be null if it fails at proxy server level
            if (ex.Response == null || ((HttpWebResponse) ex.Response).StatusCode ==
                HttpStatusCode.ProxyAuthenticationRequired)
            {
                Console.Write(@"An error has occurred while contacting the proxy server: ""{0}""", ex.Message);
            }
            else
            {
                Console.Write(@"An error has occurred while contacting ""{0}"" using the proxy server: ""{1}""",
                    url, ex.Message);
            }

            return false;
        }
        catch (Exception ex)
        {
            Console.Write(@"An error has occurred while contacting ""{0}"" using the proxy server: ""{1}""", url,
                ex.Message);

            return false;
        }

        return true;
    }

Note: I tried using HttpClient and HttpWebRequest but having same problem.

Following the example of HttpClient:

public static bool TestProxySettingsWithHttpClient(string urlToHit, string proxyAddress, int? port,
            bool isAuthenticationRequired,
            string userName = null, string password = null)
        {
            string url = urlToHit + "?time=" + DateTime.Now.Ticks;
            NetworkCredential networkCredential = null;

            if (port.HasValue)
            {
                proxyAddress += ":" + port.Value;
            }

            if (isAuthenticationRequired)
                networkCredential = new NetworkCredential(userName, password);

            try
            {
                WebProxy proxy = new WebProxy(proxyAddress)
                {
                    UseDefaultCredentials = false,
                    BypassProxyOnLocal = false,
                    Credentials = networkCredential
                };

                using (var handler = new HttpClientHandler() {Proxy = proxy, UseProxy = true})
                using (HttpClient client = new HttpClient(handler) {BaseAddress = new Uri(urlToHit)})
                {
                    var downloadString = client.GetAsync("/").Result;
                }
            }
            catch (WebException ex)
            {
                // ex.Response will be null if it fails at proxy server level
                if (ex.Response == null || ((HttpWebResponse) ex.Response).StatusCode ==
                    HttpStatusCode.ProxyAuthenticationRequired)
                {
                    Console.Write(@"An error has occurred while contacting the proxy server: ""{0}""", ex.Message);
                }
                else
                {
                    Console.Write(@"An error has occurred while contacting ""{0}"" using the proxy server: ""{1}""",
                        url, ex.Message);
                }

                return false;
            }
            catch (Exception ex)
            {
                Console.Write(@"An error has occurred while contacting ""{0}"" using the proxy server: ""{1}""", url,
                    ex.Message);

                return false;
            }

            return true;
        }

Following is the example of HttpWebRequest and HttpWebResponse:

public static bool TestProxySettingsWithHttpWebRequest(string urlToHit, string proxyAddress, int? port,
            string userName, string password, bool isAuthenticationRequired)
        {
            string url = urlToHit + "?time=" + DateTime.Now.Ticks;
            var uriBuilder = new UriBuilder(url);
            NetworkCredential networkCredential = null;
            HttpWebRequest request = null;
            WebProxy proxy = null;
            var timeoutSettings = 50000;

            if (port.HasValue)
            {
                proxyAddress += ":" + port.Value;
            }

            if (isAuthenticationRequired)
            {
                networkCredential = new NetworkCredential(userName, password);
            }

            try
            {
                request = (HttpWebRequest) WebRequest.Create(uriBuilder.Uri);

                proxy = new WebProxy(proxyAddress);
                proxy.UseDefaultCredentials = false;
                proxy.BypassProxyOnLocal = false;
                proxy.Credentials = networkCredential;

                request.Timeout = timeoutSettings;
                request.Accept = "*/*";
                request.KeepAlive = false;
                request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
                request.Proxy = proxy;

                using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
                {
                    // no action required
                }
            }
            catch (WebException ex)
            {
                // ex.Response will be null if it fails at proxy server level
                if (ex.Response == null || ((HttpWebResponse) ex.Response).StatusCode ==
                    HttpStatusCode.ProxyAuthenticationRequired)
                {
                    Console.Write(@"An error has occurred while contacting the proxy server: ""{0}""", ex.Message);
                }
                else
                {
                    Console.Write(@"An error has occurred while contacting ""{0}"" using the proxy server: ""{1}""",
                        url, ex.Message);
                }

                return false;
            }
            catch (Exception ex)
            {
                Console.Write(@"An error has occurred while contacting ""{0}"" using the proxy server: ""{1}""", url,
                    ex.Message);

                return false;
            }
            finally
            {
                request = null;
            }

            return true;
        }
Rummy
  • 31
  • 3

0 Answers0