Skip to content

Commit b765a3e

Browse files
committed
issue #553 add support for Proxy selectors
1 parent db200e1 commit b765a3e

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

unirest-bdd-tests/src/test/java/BehaviorTests/ProxyTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@
3131
import org.junit.jupiter.api.Test;
3232
import kong.unirest.core.Unirest;
3333

34+
import java.io.IOException;
35+
import java.net.InetSocketAddress;
36+
import java.net.ProxySelector;
37+
import java.net.SocketAddress;
38+
import java.net.URI;
39+
import java.util.List;
40+
41+
import static java.net.Proxy.Type.HTTP;
3442
import static org.junit.jupiter.api.Assertions.assertTrue;
3543

3644
@Disabled // The Janky Proxy is pretty janky and isn't entirely stable in CI
@@ -72,6 +80,31 @@ void canUseNonAuthProxyWithEasyMethod() {
7280
assertTrue(JankyProxy.wasUsed());
7381
}
7482

83+
@Test
84+
void canUseSelector() {
85+
JankyProxy.runServer("localhost", 4567, 7777);
86+
87+
Unirest.config().proxy(new ProxySelector(){
88+
@Override
89+
public List<java.net.Proxy> select(URI uri) {
90+
var address = InetSocketAddress.createUnresolved("localhost", 7777);
91+
return List.of(new java.net.Proxy(HTTP, address));
92+
}
93+
94+
@Override
95+
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
96+
throw new RuntimeException(ioe);
97+
}
98+
});
99+
100+
Unirest.get(MockServer.GET)
101+
.asObject(RequestCapture.class)
102+
.getBody()
103+
.assertStatus(200);
104+
105+
assertTrue(JankyProxy.wasUsed());
106+
}
107+
75108
@Test
76109
void canSetAuthenticatedProxy(){
77110
JankyProxy.runServer("localhost", 4567, 7777);

unirest/src/main/java/kong/unirest/core/Config.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import javax.net.ssl.SSLContext;
3232
import java.io.InputStream;
3333
import java.net.Authenticator;
34+
import java.net.ProxySelector;
3435
import java.net.http.*;
3536
import java.net.http.HttpRequest;
3637
import java.net.http.HttpResponse;
@@ -76,6 +77,7 @@ public class Config {
7677
private HttpClient.Version version = HttpClient.Version.HTTP_2;
7778
private RetryStrategy retry;
7879
private Authenticator authenticator;
80+
private ProxySelector proxySelector;
7981

8082
public Config() {
8183
setDefaults();
@@ -104,6 +106,7 @@ private void setDefaults() {
104106
objectMapper = () -> CoreFactory.getCore().getObjectMapper();
105107
version = HttpClient.Version.HTTP_2;
106108
authenticator = null;
109+
proxySelector = null;
107110

108111
try {
109112
clientBuilder = JavaClient::new;
@@ -151,16 +154,32 @@ public Config executor(Executor executor){
151154

152155
/**
153156
* Set a proxy
157+
* This will set any ProxySelector object already set to null and take over all proxy work
154158
*
155159
* @param value Proxy settings object.
156160
* @return this config object
157161
*/
158162
public Config proxy(Proxy value) {
159163
validateClientsNotRunning();
164+
this.proxySelector = null;
160165
this.proxy = value;
161166
return this;
162167
}
163168

169+
/**
170+
* Set a proxy selector.
171+
* This will set any Proxy object already set to null and take over all proxy work
172+
*
173+
* @param value ProxySelector object.
174+
* @return this config object
175+
*/
176+
public Config proxy(ProxySelector value) {
177+
validateClientsNotRunning();
178+
this.proxy = null;
179+
this.proxySelector = value;
180+
return this;
181+
}
182+
164183
/**
165184
* Set a proxy
166185
*
@@ -933,4 +952,11 @@ public Config authenticator(Authenticator auth) {
933952
public Authenticator getAuthenticator(){
934953
return authenticator;
935954
}
955+
956+
/**
957+
* @return the ProxySelector
958+
*/
959+
public ProxySelector getProxySelector(){
960+
return proxySelector;
961+
}
936962
}

unirest/src/main/java/kong/unirest/core/java/JavaClientBuilder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ public HttpClient apply(Config config) {
5959
if(config.getProxy() != null){
6060
createProxy(builder, config.getProxy());
6161
}
62+
if(config.getProxySelector() != null){
63+
builder = builder.proxy(config.getProxySelector());
64+
}
6265
if(config.useSystemProperties()){
6366
builder.proxy(ProxySelector.getDefault());
6467
}

0 commit comments

Comments
 (0)