0

I am using Selenium and SimpleBrowser to do some headless browser testing of my website. I want to manually set the 'host' entry in all the requests. This is so I can have an instance of IIS set up with bindings to a domain name that does not actually exist in dns.

I want to do exactly this (which works):

var req = (HttpWebRequest)WebRequest.Create("http://204.144.122.223");
req.Host = "www.asldkfhjawoeij.com";
Console.WriteLine(req.GetResponse().GetResponseStream().ReadToEnd());

But using Selenium and SimpleBrowser. Where in the code below do I override the host header, if I even can?

[Test]
[TestCase("https://204.144.122.223.com/")]
public void CanGetHomepageAndVariousOtherUrls(string server)
{
  using (var browser = new SimpleBrowserDriver())
  {
    browser.Url = server;
    browser.FindElement(By.Id("home"));
  }
}
Josh Buedel
  • 1,853
  • 16
  • 41
  • Look at [the Browsermob-proxy project](http://bmp.lightbody.net/). There is [an API for Java which can be easily used for this task, but the documentation says they will soon add it to their REST-API, too.](https://github.com/lightbody/browsermob-proxy#http-request-manipulation) And since there is a 2.0 beta 8 version out there, it might have this functionality already included (I'm not sure, though. I'm a happy user of the Java API.), so take a look. – Petr Janeček Jul 24 '13 at 21:32

1 Answers1

-1

In the Selenium way of thinking, this should not be something that you should do anything for. Your test code should mimic user behavior. The Selenium driver should connect you up to the browser and the browser (be it Firefox, Chrome or SimpleBrowser) should automatically add the Host header. Always actually. We should incorporate this in the default headers SimpleBrowser adds.

As a workaround, SimpleBrowser has a SetHeader() method that you could use to add any headers you need. You can instantiate the driver passing in a specific version of the Browser as a parameter, so you could call the SetHeader method at the right time.

But really, we should fix this in SimpleBrowser itself. https://github.com/axefrog/SimpleBrowser/pull/60

On some further inspection: SimpleBrowser does this just fine. If you navigate to a Url (ie. http://www.asldkfhjawoeij.com/), it will automatically add the Host header to the request. What you ask for is incorrect behavior for a web browser. If the server is expecting a host header for a specific site on the IP address, you should use Selenium/SimpleBrowser to do from code exactly what you would do from a normal browser. Entering a full URL with domain names. Not adding fake headers.

If your problem is that the host name is not (yet) known in the DNS, you might set up your test runner environment to fake this. You could add it to your local DNS or to your host file (http://en.wikipedia.org/wiki/Hosts_(file)).

Teun D
  • 5,045
  • 1
  • 34
  • 44
  • So I tried SimpleBrowser.Browser.SetHeader("host:www.asldkfhjawoeij.com") but I've run into this http://stackoverflow.com/questions/239725/cannot-set-some-http-headers-when-using-system-net-webrequest problem where 'host' is a restricted header and cannot be set with a simple httpWebRequest.Headers.Add("host:www.asldkfhjawoeij.com") which is what SimpleBrowser does with the SetHeader() parameter. – Josh Buedel Jul 25 '13 at 19:13