forked from AsyncHttpClient/async-http-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpsProxyTest.java
214 lines (184 loc) · 9.08 KB
/
HttpsProxyTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* Copyright (c) 2010-2012 Sonatype, Inc. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package org.asynchttpclient.proxy;
import io.github.artsok.RepeatedIfExceptionsTest;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.asynchttpclient.AbstractBasicTest;
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.AsyncHttpClientConfig;
import org.asynchttpclient.RequestBuilder;
import org.asynchttpclient.Response;
import org.asynchttpclient.proxy.ProxyServer.Builder;
import org.asynchttpclient.request.body.generator.ByteArrayBodyGenerator;
import org.asynchttpclient.test.EchoHandler;
import org.asynchttpclient.util.HttpConstants;
import org.eclipse.jetty.proxy.ConnectHandler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import static org.asynchttpclient.Dsl.asyncHttpClient;
import static org.asynchttpclient.Dsl.config;
import static org.asynchttpclient.Dsl.get;
import static org.asynchttpclient.Dsl.post;
import static org.asynchttpclient.Dsl.proxyServer;
import static org.asynchttpclient.test.TestUtils.LARGE_IMAGE_BYTES;
import static org.asynchttpclient.test.TestUtils.addHttpConnector;
import static org.asynchttpclient.test.TestUtils.addHttpsConnector;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
/**
* Proxy usage tests.
*/
public class HttpsProxyTest extends AbstractBasicTest {
private Server server2;
@Override
public AbstractHandler configureHandler() throws Exception {
return new ProxyHandler();
}
@Override
@BeforeEach
public void setUpGlobal() throws Exception {
server = new Server();
ServerConnector connector = addHttpConnector(server);
server.setHandler(configureHandler());
server.start();
port1 = connector.getLocalPort();
server2 = new Server();
ServerConnector connector2 = addHttpsConnector(server2);
server2.setHandler(new EchoHandler());
server2.start();
port2 = connector2.getLocalPort();
logger.info("Local HTTP server started successfully");
}
@Override
@AfterEach
public void tearDownGlobal() throws Exception {
server.stop();
server2.stop();
}
@RepeatedIfExceptionsTest(repeats = 5)
public void testRequestProxy() throws Exception {
try (AsyncHttpClient client = asyncHttpClient(config().setFollowRedirect(true).setUseInsecureTrustManager(true))) {
RequestBuilder rb = get(getTargetUrl2()).setProxyServer(proxyServer("localhost", port1));
Response response = client.executeRequest(rb.build()).get();
assertEquals(200, response.getStatusCode());
}
}
@RepeatedIfExceptionsTest(repeats = 5)
public void testConfigProxy() throws Exception {
AsyncHttpClientConfig config = config()
.setFollowRedirect(true)
.setProxyServer(proxyServer("localhost", port1).build())
.setUseInsecureTrustManager(true)
.build();
try (AsyncHttpClient client = asyncHttpClient(config)) {
Response response = client.executeRequest(get(getTargetUrl2())).get();
assertEquals(200, response.getStatusCode());
}
}
@RepeatedIfExceptionsTest(repeats = 5)
public void testNoDirectRequestBodyWithProxy() throws Exception {
AsyncHttpClientConfig config = config()
.setFollowRedirect(true)
.setProxyServer(proxyServer("localhost", port1).build())
.setUseInsecureTrustManager(true)
.build();
try (AsyncHttpClient client = asyncHttpClient(config)) {
Response response = client.executeRequest(post(getTargetUrl2()).setBody(new ByteArrayBodyGenerator(LARGE_IMAGE_BYTES))).get();
assertEquals(200, response.getStatusCode());
}
}
@RepeatedIfExceptionsTest(repeats = 5)
public void testDecompressBodyWithProxy() throws Exception {
AsyncHttpClientConfig config = config()
.setFollowRedirect(true)
.setProxyServer(proxyServer("localhost", port1).build())
.setUseInsecureTrustManager(true)
.build();
try (AsyncHttpClient client = asyncHttpClient(config)) {
String body = "hello world";
Response response = client.executeRequest(post(getTargetUrl2())
.setHeader("X-COMPRESS", "true")
.setBody(body)).get();
assertEquals(200, response.getStatusCode());
assertEquals(body, response.getResponseBody());
}
}
@RepeatedIfExceptionsTest(repeats = 5)
public void testPooledConnectionsWithProxy() throws Exception {
try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config().setFollowRedirect(true).setUseInsecureTrustManager(true).setKeepAlive(true))) {
RequestBuilder rb = get(getTargetUrl2()).setProxyServer(proxyServer("localhost", port1));
Response response1 = asyncHttpClient.executeRequest(rb.build()).get();
assertEquals(200, response1.getStatusCode());
Response response2 = asyncHttpClient.executeRequest(rb.build()).get();
assertEquals(200, response2.getStatusCode());
}
}
@RepeatedIfExceptionsTest(repeats = 5)
public void testFailedConnectWithProxy() throws Exception {
try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config().setFollowRedirect(true).setUseInsecureTrustManager(true).setKeepAlive(true))) {
Builder proxyServer = proxyServer("localhost", port1);
proxyServer.setCustomHeaders(r -> new DefaultHttpHeaders().set(ProxyHandler.HEADER_FORBIDDEN, "1"));
RequestBuilder rb = get(getTargetUrl2()).setProxyServer(proxyServer);
Response response1 = asyncHttpClient.executeRequest(rb.build()).get();
assertEquals(403, response1.getStatusCode());
Response response2 = asyncHttpClient.executeRequest(rb.build()).get();
assertEquals(403, response2.getStatusCode());
Response response3 = asyncHttpClient.executeRequest(rb.build()).get();
assertEquals(403, response3.getStatusCode());
}
}
@RepeatedIfExceptionsTest(repeats = 5)
public void testClosedConnectionWithProxy() throws Exception {
try (AsyncHttpClient asyncHttpClient = asyncHttpClient(
config().setFollowRedirect(true).setUseInsecureTrustManager(true).setKeepAlive(true))) {
Builder proxyServer = proxyServer("localhost", port1);
proxyServer.setCustomHeaders(r -> new DefaultHttpHeaders().set(ProxyHandler.HEADER_FORBIDDEN, "2"));
RequestBuilder rb = get(getTargetUrl2()).setProxyServer(proxyServer);
assertThrowsExactly(ExecutionException.class, () -> asyncHttpClient.executeRequest(rb.build()).get());
assertThrowsExactly(ExecutionException.class, () -> asyncHttpClient.executeRequest(rb.build()).get());
assertThrowsExactly(ExecutionException.class, () -> asyncHttpClient.executeRequest(rb.build()).get());
}
}
public static class ProxyHandler extends ConnectHandler {
final static String HEADER_FORBIDDEN = "X-REJECT-REQUEST";
@Override
public void handle(String s, Request r, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
if (HttpConstants.Methods.CONNECT.equalsIgnoreCase(request.getMethod())) {
String headerValue = request.getHeader(HEADER_FORBIDDEN);
if (headerValue == null) {
headerValue = "";
}
switch (headerValue) {
case "1":
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
r.setHandled(true);
return;
case "2":
r.getHttpChannel().getConnection().close();
r.setHandled(true);
return;
}
}
super.handle(s, r, request, response);
}
}
}