Skip to content

Commit 32d1e50

Browse files
committed
add some documentation
1 parent b765a3e commit 32d1e50

File tree

3 files changed

+109
-31
lines changed

3 files changed

+109
-31
lines changed

mkdocs/docs/proxies.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Proxies
2+
Sometimes you need to tunnel through a proxy. Unirest provides several different mechanisms for this.
3+
4+
## Simple Proxy
5+
You can set a simple single proxy object. This will create a simple ```java.net.ProxySelector``` and ```java.net.Authenticator``` (if passing creds)
6+
```java
7+
// Configure with authentication:
8+
Unirest.config().proxy("proxy.com", 7777, "username", "password1!");
9+
10+
// or without
11+
Unirest.config().proxy("proxy.com", 7777);
12+
13+
// You can also pass a Unirest Proxy object
14+
Unirest.config().proxy(new Proxy("proxy.com", 7777));
15+
```
16+
17+
## Using System Settings For Proxies
18+
Java has some defined system properties for Proxies.
19+
```java
20+
System.setProperty("http.proxyHost", "localhost");
21+
System.setProperty("http.proxyPort", "7777");
22+
23+
Unirest.config().useSystemProperties(true);
24+
```
25+
26+
## Dealing with Multiple Proxies
27+
Sometimes, if the universe hates you. You might need to use different proxies for different hosts.
28+
This can be done by using a ```ProxySelector``` (and a ```Authenticator``` if auth is required)
29+
30+
```java
31+
Unirest.config()
32+
.proxy(new ProxySelector() {
33+
@Override
34+
public List<java.net.Proxy> select(URI uri) {
35+
if (uri.getHost().equals("homestarrunner.com")) {
36+
return List.of(new java.net.Proxy(HTTP, InetSocketAddress.createUnresolved("proxy-sad.com", 7777)));
37+
}
38+
39+
return List.of(new java.net.Proxy(HTTP, InetSocketAddress.createUnresolved("default.com", 7777)));
40+
}
41+
42+
@Override
43+
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
44+
45+
}
46+
})
47+
.authenticator(new Authenticator() {
48+
@Override
49+
public PasswordAuthentication requestPasswordAuthenticationInstance(String host, InetAddress addr,
50+
int port, String protocol,
51+
String prompt, String scheme,
52+
URL url, RequestorType reqType) {
53+
// Please don't hardcode passwords in your code :D
54+
if(host.equals("homestarrunner.com")) {
55+
return new PasswordAuthentication("strongbad", "password".toCharArray());
56+
}
57+
return new PasswordAuthentication("default", "password".toCharArray());
58+
}
59+
});
60+
```

mkdocs/docs/requests.md

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -218,21 +218,4 @@ Unirest.config()
218218

219219
Unirest.get("https://some.custom.secured.place.com")
220220
.asString();
221-
```
222-
223-
## Proxies
224-
Sometimes you need to tunnel through a proxy. Unirest can be configured to do this. Note that authenticated proxies cannot be configured on a per-request basis unless you want to build it into the URL itself.
225-
226-
```java
227-
// Configure with authentication:
228-
Unirest.config().proxy("proxy.com", 7777, "username", "password1!");
229-
230-
// or without
231-
Unirest.config().proxy("proxy.com", 7777);
232-
233-
// or pass it in the request. This will override any proxy done in the config
234-
// currently only unauthenticated proxies work
235-
Unirest.get(MockServer.GET)
236-
.proxy("proxy.com", 7777)
237-
.asString();
238-
```
221+
```

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

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
/**
22
* The MIT License
3-
*
3+
* <p>
44
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
5-
*
5+
* <p>
66
* Permission is hereby granted, free of charge, to any person obtaining
77
* a copy of this software and associated documentation files (the
88
* "Software"), to deal in the Software without restriction, including
99
* without limitation the rights to use, copy, modify, merge, publish,
1010
* distribute, sublicense, and/or sell copies of the Software, and to
1111
* permit persons to whom the Software is furnished to do so, subject to
1212
* the following conditions:
13-
*
13+
* <p>
1414
* The above copyright notice and this permission notice shall be
1515
* included in all copies or substantial portions of the Software.
16-
*
16+
* <p>
1717
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1818
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1919
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@@ -32,10 +32,7 @@
3232
import kong.unirest.core.Unirest;
3333

3434
import java.io.IOException;
35-
import java.net.InetSocketAddress;
36-
import java.net.ProxySelector;
37-
import java.net.SocketAddress;
38-
import java.net.URI;
35+
import java.net.*;
3936
import java.util.List;
4037

4138
import static java.net.Proxy.Type.HTTP;
@@ -84,7 +81,7 @@ void canUseNonAuthProxyWithEasyMethod() {
8481
void canUseSelector() {
8582
JankyProxy.runServer("localhost", 4567, 7777);
8683

87-
Unirest.config().proxy(new ProxySelector(){
84+
Unirest.config().proxy(new ProxySelector() {
8885
@Override
8986
public List<java.net.Proxy> select(URI uri) {
9087
var address = InetSocketAddress.createUnresolved("localhost", 7777);
@@ -106,7 +103,7 @@ public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
106103
}
107104

108105
@Test
109-
void canSetAuthenticatedProxy(){
106+
void canSetAuthenticatedProxy() {
110107
JankyProxy.runServer("localhost", 4567, 7777);
111108

112109
Unirest.config().proxy("localhost", 7777, "username", "password1!");
@@ -120,8 +117,9 @@ void canSetAuthenticatedProxy(){
120117
}
121118

122119
@Test
123-
@Disabled // there is some weird conflict between jetty and unirest here
124-
void canFlagTheClientsToUseSystemProperties(){
120+
@Disabled
121+
// there is some weird conflict between jetty and unirest here
122+
void canFlagTheClientsToUseSystemProperties() {
125123
JankyProxy.runServer("localhost", 4567, 7777);
126124

127125
System.setProperty("http.proxyHost", "localhost");
@@ -137,7 +135,44 @@ void canFlagTheClientsToUseSystemProperties(){
137135
assertTrue(JankyProxy.wasUsed());
138136
}
139137

140-
// @Test @Disabled // https://free-proxy-list.net/
138+
@Test
139+
void multipleOfEverything() {
140+
JankyProxy.runServer("localhost", 4567, 7777);
141+
142+
Unirest.config()
143+
.proxy(new ProxySelector() {
144+
@Override
145+
public List<java.net.Proxy> select(URI uri) {
146+
if (uri.getHost().equals("homestarrunner.com")) {
147+
return List.of(new java.net.Proxy(HTTP, InetSocketAddress.createUnresolved("proxy-sad.com", 7777)));
148+
}
149+
150+
return List.of(new java.net.Proxy(HTTP, InetSocketAddress.createUnresolved("default.com", 7777)));
151+
}
152+
153+
@Override
154+
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
155+
156+
}
157+
})
158+
159+
.authenticator(new Authenticator() {
160+
@Override
161+
public PasswordAuthentication requestPasswordAuthenticationInstance(String host, InetAddress addr,
162+
int port, String protocol,
163+
String prompt, String scheme,
164+
URL url, RequestorType reqType) {
165+
// Please don't hardcode passwords in your code :D
166+
if(host.equals("homestarrunner.com")) {
167+
return new PasswordAuthentication("strongbad", "password".toCharArray());
168+
}
169+
return new PasswordAuthentication("default", "password".toCharArray());
170+
}
171+
});
172+
173+
}
174+
175+
// @Test @Disabled // https://free-proxy-list.net/
141176
// void callSomethingRealThroughARealProxy() {
142177
// Unirest.config().proxy("18.222.230.116",8080);
143178
// //Unirest.config().proxy("34.73.62.46",3128, "myuser","pass1!");

0 commit comments

Comments
 (0)