2

I'm working on an automated web test stack using Selenium, Java and testNG. For authentication and safety reasons, I need to enrich the headers of the website I am accessing through Kubernetes. For example, I can successfully use this CURL command in terminal to retrieve the page I want to access: curl -H 'Host: staging.myapp.com' -H 'X-Forwarded-Proto: https' http://nginx.myapp.svc.cluster.local. So as you can see, I only need to add 2 headers for Host and X-Forwarded-Proto.

I'm trying to create a proxy that will enrich headers in my @BeforeMethod method for a couple of days, but I'm still stuck, and there are so many shadow areas that I can't find a way to debug anything and understand what's wrong. For now, no matter my code, I keep getting a "No internet" (ERR_PROXY_CONNECTION_FAILED) error page in my driver when I launch it.

For example, one version of my code:

        BrowserMobProxy browserMobProxy = new BrowserMobProxyServer();
        browserMobProxy.setTrustAllServers(true);
        browserMobProxy.addHeader("Host", "staging.myapp.com");
        browserMobProxy.addHeader("X-Forwarded-Proto", "https");
        browserMobProxy.start();

        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.setProxy(ClientUtil.createSeleniumProxy(browserMobProxy));
        driver = new ChromeDriver(chromeOptions);
        driver.get("http://nginx.myapp.svc.cluster.local");

I tried several other code structures like:

  • defining browserMobProxyServer.addRequestFilter to add headers in requests
  • using only org.openqa.selenium.Proxy
  • setting up proxy with setHttpProxy("http://nginx.myapp.svc.cluster.local:8888");

But nothing works, I always get ERR_PROXY_CONNECTION_FAILED. Anybody have any clue about that? Thanks!

Batou
  • 48
  • 6

1 Answers1

0

OK so, after days of research, I found out 2 things:

  • Due to whatever configuration on my Mac, I need to force host Address (other people running the same code had no issue...):

proxy.setHttpProxy(Inet4Address.getLocalHost().getHostAddress() + ":" + browserMobProxyServer.getPort());

  • I have to manually alter headers via a response filter instead of using .addHeader method:

browserMobProxyServer.addResponseFilter((response, content, messageInfo)->{

//Do something here related to response.headers()

});

I hope it will help some lost souls here.

Batou
  • 48
  • 6