Skip to content

Commit 49280d5

Browse files
author
Kendar
committed
Added some test coverage to 49%
1 parent 485cec1 commit 49280d5

File tree

41 files changed

+551
-299
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+551
-299
lines changed

ham/api.ham/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<parent>
6-
<artifactId>HttpAnsweringMachine</artifactId>
6+
<artifactId>ham</artifactId>
77
<groupId>org.kendar</groupId>
88
<version>4.2.0</version>
99
<relativePath>../pom.xml</relativePath>

ham/api.ham/src/main/java/org/kendar/ham/HamBasicBuilder.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ public interface HamBasicBuilder {
3636
*/
3737
HamBasicBuilder withSocksProxy(String ip, int port);
3838

39+
/**
40+
* The http(s) proxy to use for connections. Usually the internal HAM proxy
41+
*
42+
* @param ip
43+
* @param port defaults to 1081
44+
* @return
45+
*/
46+
HamBasicBuilder withHttpProxy(String ip, int port);
47+
3948
/**
4049
* The dns to use for the requests, default port 53
4150
*

ham/api.ham/src/main/java/org/kendar/ham/HamBuilder.java

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.fasterxml.jackson.databind.ObjectMapper;
66
import com.fasterxml.jackson.databind.type.CollectionType;
77
import org.apache.commons.io.IOUtils;
8+
import org.apache.commons.lang3.NotImplementedException;
9+
import org.apache.http.HttpHost;
810
import org.apache.http.client.methods.CloseableHttpResponse;
911
import org.apache.http.client.methods.HttpPost;
1012
import org.apache.http.client.methods.HttpUriRequest;
@@ -21,6 +23,7 @@
2123
import org.apache.http.impl.client.CloseableHttpClient;
2224
import org.apache.http.impl.client.HttpClientBuilder;
2325
import org.apache.http.impl.client.HttpClients;
26+
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
2427
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
2528
import org.apache.http.impl.conn.SystemDefaultDnsResolver;
2629
import org.apache.http.protocol.HttpContext;
@@ -62,6 +65,7 @@ public class HamBuilder implements HamInternalBuilder {
6265
private int dnsPort;
6366
private int proxyPort;
6467
private String proxyIp;
68+
private boolean proxyHttp =false;
6569

6670
private HamBuilder() {
6771
}
@@ -96,9 +100,19 @@ public HamBasicBuilder withDns(String ip, int port) {
96100
public HamBasicBuilder withSocksProxy(String ip, int port) {
97101
this.proxyIp = ip;
98102
this.proxyPort = port;
103+
this.proxyHttp=false;
99104
return this;
100105
}
101106

107+
@Override
108+
public HamBasicBuilder withHttpProxy(String ip, int port) {
109+
/*this.proxyIp = ip;
110+
this.proxyPort = port;
111+
this.proxyHttp=true;
112+
return this;*/
113+
throw new NotImplementedException();
114+
}
115+
102116
@Override
103117
public HamBasicBuilder withDns(String ip) {
104118
return withDns(ip, 53);
@@ -219,17 +233,29 @@ public CloseableHttpResponse execute(HttpUriRequest request, boolean ignoreSSLCe
219233
SSLConnectionSocketFactory ssfs = null;
220234

221235
if (this.proxyIp != null) {
222-
HttpClientBuilder custom = getHttpClientBuilder(ignoreSSLCertificates, dnsResolver);
223-
224-
CloseableHttpClient httpclient = custom.build();
225-
226-
InetSocketAddress socksaddr = new InetSocketAddress(this.proxyIp, this.proxyPort);
227-
HttpClientContext context = HttpClientContext.create();
228-
context.setAttribute("socks.address", socksaddr);
236+
if(this.proxyHttp){
237+
HttpHost socksaddr = new HttpHost(this.proxyIp, this.proxyPort,"http");
238+
var routePlanner = new DefaultProxyRoutePlanner(socksaddr);
239+
HttpClientBuilder custom = getHttpClientBuilder(ignoreSSLCertificates, dnsResolver);
240+
custom.setRoutePlanner(routePlanner);
241+
242+
CloseableHttpClient httpclient = custom.build();
243+
244+
HttpClientContext context = HttpClientContext.create();
245+
//context.setAttribute("http.route.default-proxy", socksaddr);
246+
return httpclient.execute(request, context);
247+
}else {
248+
HttpClientBuilder custom = getHttpClientBuilder(ignoreSSLCertificates, dnsResolver);
249+
250+
CloseableHttpClient httpclient = custom.build();
251+
252+
InetSocketAddress socksaddr = new InetSocketAddress(this.proxyIp, this.proxyPort);
253+
HttpClientContext context = HttpClientContext.create();
254+
context.setAttribute("socks.address", socksaddr);
255+
return httpclient.execute(request, context);
256+
}
229257

230-
return httpclient.execute(request, context);
231258
} else {
232-
HttpClientBuilder custom = null;
233259
CloseableHttpClient httpClient = getHttpClientBuilderNoProxy(ignoreSSLCertificates, dnsResolver, ssfs).build();
234260

235261
return httpClient.execute(request);

ham/api.ham/src/main/java/org/kendar/ham/LocalDnsResolver.java

Lines changed: 0 additions & 192 deletions
This file was deleted.

ham/api.ham/src/main/java/org/kendar/ham/ProxyBuilder.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ public void setForce(boolean force) {
7676
*/
7777
String addProxy(String when, String where, String test) throws HamException;
7878

79+
/**
80+
* Retrieve proxy by id
81+
* @param id
82+
* @return
83+
* @throws HamException
84+
*/
85+
Proxy retrieveProxy(String id) throws HamException;
86+
7987
/**
8088
* Add a db proxy, specify the db to be proxied
8189
*

ham/api.ham/src/main/java/org/kendar/ham/ProxyBuilderImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public String addProxy(String when, String where, String test) throws HamExcepti
5050
throw new HamException("Missing id");
5151
}
5252

53+
54+
5355
@Override
5456
public DbProxyBuilder addRemoteDbProxy(String dbName, String login, String password, String dbDriver) throws HamException {
5557

@@ -80,6 +82,13 @@ public List<Proxy> retrieveProxies() throws HamException {
8082
return hamBuilder.callJsonList(request.build(), Proxy.class);
8183
}
8284

85+
@Override
86+
public Proxy retrieveProxy(String id) throws HamException {
87+
var request = hamBuilder.newRequest()
88+
.withPath("/api/proxies/"+id);
89+
return hamBuilder.callJson(request.build(), Proxy.class);
90+
}
91+
8392
@Override
8493
public void refresh() throws HamException {
8594
var request = hamBuilder.newRequest()

ham/api.ham/src/test/java/org/kendar/ham/DnsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static void beforeAll() throws HamTestException {
1717

1818
@Test
1919
public void testAddingDnsFix() throws HamException {
20-
final String WEIRD_NAME = "casper.core-workload.qa.aws.lmn";
20+
final String WEIRD_NAME = "cooper.core-workload.qa.aws.test";
2121
final String IP = "10.0.0.1";
2222
var dnsId = hamBuilder
2323
.dns()

0 commit comments

Comments
 (0)