0

I am working on tests that verify that our public-facing websites are operational. To do that, I have a piece of code I run to make an HttpWebRequest. How do I simulate being a user outside the intranet of the organization? Is there a way I can specify a user on the other side of the state? Would I add extra information to the header?

private async Task RunURLRequestAsync(string input)
{
    string methodName = Utils.getCurrentMethod();
    Log("In:  " + methodName);
    try
    {
        var client = new HttpClient();
        var uri = new Uri(input);
        Log("URI is " + uri.ToString());
        HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(uri);
        webrequest.CookieContainer = new CookieContainer();
        webrequest.Method = "GET";
        webrequest.Timeout = 2000;
        HttpWebResponse response = (HttpWebResponse) webrequest.GetResponse();
        pageStatusCode = response.StatusCode.ToString();
        pageStatusDescription = response.StatusDescription.ToString();
        if (response.StatusCode == HttpStatusCode.Unauthorized)
        {
            pageStatusCode = "401 Unauthorized";
        }
        using (Stream stream = response.GetResponseStream())
        {
            StreamReader reader = new StreamReader(stream);
            string result = reader.ReadToEnd();
            Log("Web Page Contents: " + result);

        }
    }
    catch(Exception exc)
    {
        if(exc.Message.Contains("Unauthorized"))
        {
            pageStatusCode = "401";
            pageStatusDescription = "Unauthorized (Expected)";
            return;
        }
        else
        {
            Log(exc.Message);
            Log(exc.StackTrace);
        }
    }

}
FaizanHussainRabbani
  • 3,256
  • 3
  • 26
  • 46
Su Llewellyn
  • 2,660
  • 2
  • 19
  • 33
  • I learned that users outside our domain can't access our web application. I guess that means I need to test for inside the WAN, but in a different DOMAIN. – Su Llewellyn Feb 24 '18 at 18:57
  • You need to connect to the same public interface available to your users. This code, however, is messy. It wouldn't even compile in my VS setup. In your `RunURLRequestAsync()` async method you're not awating anything because no other async method is used (e.g. .`GetResponseAsync()`). A `HttpClient()` is instantiated but never used (and never disposed - so is the WebResponse). `response.StatusCode == HttpStatusCode.Unauthorized` can never be reached, that's an exception. There's no HttpHeaders setup. This is not the usual browser setup, so you're testing your own errors here (...). – Jimi Feb 24 '18 at 19:29
  • Thank you, Jimi. I'm fixing those ASAP! – Su Llewellyn Feb 26 '18 at 15:54

1 Answers1

0

I would write automated tests using Selenium. John Sonmez has a good getting started course on Pluralsight. Alternatively, you could start here.

Mitch Stewart
  • 1,253
  • 10
  • 12