Hi,
here at work we're using BrowserMob Proxy to run our selenium tests against different environments.
When running theses tests on a developer or QA machine, we change the /etc/hosts file so that
hostnames get the correct IP for each environment (staging of each team, integration, QA).
To run theses tests on Jenkins we're using BrowserMob Proxy to point these hostnames the same way.
Doing that, I've noticed that when I add a host mapping for a host not reachable nor defined in /etc/hosts,
I get UnknownHostException, like this:
INFO 11/13 13:19:31 n.l.b.p.h.BrowserMo~ - java.net.UnknownHostException: testedehost: Name or service not known when requesting
http://testedehost/The way I've made it work here was change BrowserMobHttpClient.java, creating a DnsResolver that tries
the hostNameResolver (which respects the mappings defined in the proxy) first and after that trying
normal resolution:
diff -r bmghbeta8/browsermob-proxy/src/main/java/net/lightbody/bmp/proxy/http/BrowserMobHttpClient.java browsermob-proxy-browsermob-proxy-2.0-beta-8/src/main/java/net/lightbody/bmp/proxy/http/BrowserMobHttpClient.java
16,18c16
< import org.apache.http.conn.ClientConnectionRequest;
< import org.apache.http.conn.ConnectionPoolTimeoutException;
< import org.apache.http.conn.ManagedClientConnection;
---
> import org.apache.http.conn.*;
27a26,27
> import org.apache.http.impl.conn.PoolingClientConnectionManager;
> import org.apache.http.impl.conn.SystemDefaultDnsResolver;
43a44
> import java.net.InetAddress;
45a47
> import java.net.UnknownHostException;
88c90
< private ThreadSafeClientConnManager httpClientConnMgr;
---
> private PoolingClientConnectionManager httpClientConnMgr;
124c126,138
< httpClientConnMgr = new ThreadSafeClientConnManager(schemeRegistry) {
---
> DnsResolver dnsResolver = new SystemDefaultDnsResolver() {
> @Override public InetAddress[] resolve(String host) throws UnknownHostException {
> try {
> InetAddress a = hostNameResolver.resolve(host);
> return new InetAddress[] {a};
> } catch (IOException e) {
>
> }
> return super.resolve(host);
> }
> };
>
> httpClientConnMgr = new PoolingClientConnectionManager(schemeRegistry, dnsResolver) {
I've commited these changes to a fork I've created at
https://github.com/andrematheus/browsermob-proxyI would like to know if this line of reasoning is correct and if so, if I can merge these changes to
BrowserMob Proxy. If I haven't made myself clear please ask, I'll be glad to help.
I've made some tests using this script:
------------------------------------------------------------------------------------------------------------------------------------------
#!/bin/bash
TESTHOST=testedehost
TESTMAPPING=127.0.0.1
# start browsermob-proxy
sh ./bin/browsermob-proxy --port=25000 &
BMOBPID=$!
sleep 5
# setup proxy
curl -X POST -d "port=25100"
http://localhost:25000/proxy# setup one host
curl -X POST -v --silent --output /dev/stderr --write-out "%{http_code}" \
-H 'Accept: application/json' \
-H 'Content-type: application/json' \
http://localhost:25000/proxy/25100/hosts -d "{ \"${TESTHOST}\" : \"${TESTMAPPING}\" }"
curl --proxy
http://localhost:25100 http://$TESTHOST
kill -9 ${BMOBPID}
------------------------------------------------------------------------------------------------------------------------------------------
Just save somewhere then run it from the directory where you unzipped browsermob.
The curl, assuming you don't have "testedehost" in your /etc/hosts file, will return an error page:
------------------------------------------------------------------------------------------------------------------------------------------
200<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
<title>Problem loading page</title>
<link rel="stylesheet" href="chrome://global/skin/netError.css" type="text/css" media="all" />
<link rel="icon" type="image/png" id="favicon" href="chrome://global/skin/icons/warning-16.png"/>
</head>
<body>
<!-- PAGE CONTAINER (for styling purposes only) -->
<div id="errorPageContainer">
<!-- Error Title -->
<div id="errorTitle">
<h1 id="errorTitleText">Server not found</h1>
.....
------------------------------------------------------------------------------------------------------------------------------------------
Thanks,
André