Skip to content

Instantly share code, notes, and snippets.

@commuterjoy
Created August 3, 2012 09:07
Show Gist options
  • Save commuterjoy/3246120 to your computer and use it in GitHub Desktop.
Save commuterjoy/3246120 to your computer and use it in GitHub Desktop.
Override host header with HTMLUnit
import org.openqa.selenium._
import org.openqa.selenium.htmlunit._
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.{ GivenWhenThen, FeatureSpec }
import test.`package`._
// instance of htmlunit with request header setting ability
class FixedHtmlUnitDriver extends HtmlUnitDriver {
def setHeader(name: String, value: String) = {
getWebClient().addRequestHeader(name, value)
}
}
//
class HostOverrideTest extends FeatureSpec {
val driver = new FixedHtmlUnitDriver
driver.setHeader("X-Edition-Override", "usa")
driver.setHeader("Host", "example.com")
driver.get("http://localhost:4567/hello")
println(driver.getTitle())
println(driver.getPageSource())
}
require 'rubygems'
require 'sinatra'
get "/hello" do ||
request.env["HTTP_HOST"]
end
@amit-int
Copy link

amit-int commented Jun 14, 2018

Hi, please can you help me in doing this in java, it does not work.
On doing, it throws the following exception:

Thread-15] c.g.h.j.DefaultJavaScriptErrorListener : Error loading JavaScript from [https://code.jquery.com/jquery-latest.min.js]

Basically, it does not download any resource such as javascript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment