13
13
package org .asynchttpclient .proxy ;
14
14
15
15
import org .asynchttpclient .*;
16
+ import org .asynchttpclient .proxy .ProxyServer .Builder ;
16
17
import org .asynchttpclient .request .body .generator .ByteArrayBodyGenerator ;
17
18
import org .asynchttpclient .test .EchoHandler ;
19
+ import org .asynchttpclient .util .HttpConstants ;
18
20
import org .eclipse .jetty .proxy .ConnectHandler ;
19
21
import org .eclipse .jetty .server .Server ;
20
22
import org .eclipse .jetty .server .ServerConnector ;
23
25
import org .testng .annotations .BeforeClass ;
24
26
import org .testng .annotations .Test ;
25
27
28
+ import io .netty .handler .codec .http .DefaultHttpHeaders ;
29
+
26
30
import static org .asynchttpclient .Dsl .*;
27
31
import static org .asynchttpclient .test .TestUtils .LARGE_IMAGE_BYTES ;
28
32
import static org .asynchttpclient .test .TestUtils .addHttpConnector ;
29
33
import static org .asynchttpclient .test .TestUtils .addHttpsConnector ;
30
34
import static org .testng .Assert .assertEquals ;
35
+ import static org .testng .Assert .assertThrows ;
36
+
37
+ import java .io .IOException ;
38
+ import java .util .concurrent .ExecutionException ;
39
+
40
+ import javax .servlet .ServletException ;
41
+ import javax .servlet .http .HttpServletRequest ;
42
+ import javax .servlet .http .HttpServletResponse ;
31
43
32
44
/**
33
45
* Proxy usage tests.
@@ -37,7 +49,7 @@ public class HttpsProxyTest extends AbstractBasicTest {
37
49
private Server server2 ;
38
50
39
51
public AbstractHandler configureHandler () throws Exception {
40
- return new ConnectHandler ();
52
+ return new ProxyHandler ();
41
53
}
42
54
43
55
@ BeforeClass (alwaysRun = true )
@@ -129,4 +141,62 @@ public void testPooledConnectionsWithProxy() throws Exception {
129
141
assertEquals (r2 .getStatusCode (), 200 );
130
142
}
131
143
}
144
+
145
+ @ Test
146
+ public void testFailedConnectWithProxy () throws Exception {
147
+ try (AsyncHttpClient asyncHttpClient = asyncHttpClient (config ().setFollowRedirect (true ).setUseInsecureTrustManager (true ).setKeepAlive (true ))) {
148
+ Builder proxyServer = proxyServer ("localhost" , port1 );
149
+ proxyServer .setCustomHeaders (r -> new DefaultHttpHeaders ().set (ProxyHandler .HEADER_FORBIDDEN , "1" ));
150
+ RequestBuilder rb = get (getTargetUrl2 ()).setProxyServer (proxyServer );
151
+
152
+ Response response1 = asyncHttpClient .executeRequest (rb .build ()).get ();
153
+ assertEquals (403 , response1 .getStatusCode ());
154
+
155
+ Response response2 = asyncHttpClient .executeRequest (rb .build ()).get ();
156
+ assertEquals (403 , response2 .getStatusCode ());
157
+
158
+ Response response3 = asyncHttpClient .executeRequest (rb .build ()).get ();
159
+ assertEquals (403 , response3 .getStatusCode ());
160
+ }
161
+ }
162
+
163
+ @ Test
164
+ public void testClosedConnectionWithProxy () throws Exception {
165
+ try (AsyncHttpClient asyncHttpClient = asyncHttpClient (
166
+ config ().setFollowRedirect (true ).setUseInsecureTrustManager (true ).setKeepAlive (true ))) {
167
+ Builder proxyServer = proxyServer ("localhost" , port1 );
168
+ proxyServer .setCustomHeaders (r -> new DefaultHttpHeaders ().set (ProxyHandler .HEADER_FORBIDDEN , "2" ));
169
+ RequestBuilder rb = get (getTargetUrl2 ()).setProxyServer (proxyServer );
170
+
171
+ assertThrows (ExecutionException .class , () -> asyncHttpClient .executeRequest (rb .build ()).get ());
172
+ assertThrows (ExecutionException .class , () -> asyncHttpClient .executeRequest (rb .build ()).get ());
173
+ assertThrows (ExecutionException .class , () -> asyncHttpClient .executeRequest (rb .build ()).get ());
174
+ }
175
+ }
176
+
177
+ public static class ProxyHandler extends ConnectHandler {
178
+ final static String HEADER_FORBIDDEN = "X-REJECT-REQUEST" ;
179
+
180
+ @ Override
181
+ public void handle (String s , org .eclipse .jetty .server .Request r , HttpServletRequest request ,
182
+ HttpServletResponse response ) throws ServletException , IOException {
183
+ if (HttpConstants .Methods .CONNECT .equalsIgnoreCase (request .getMethod ())) {
184
+ String headerValue = request .getHeader (HEADER_FORBIDDEN );
185
+ if (headerValue == null ) {
186
+ headerValue = "" ;
187
+ }
188
+ switch (headerValue ) {
189
+ case "1" :
190
+ response .setStatus (HttpServletResponse .SC_FORBIDDEN );
191
+ r .setHandled (true );
192
+ return ;
193
+ case "2" :
194
+ r .getHttpChannel ().getConnection ().close ();
195
+ r .setHandled (true );
196
+ return ;
197
+ }
198
+ }
199
+ super .handle (s , r , request , response );
200
+ }
201
+ }
132
202
}
0 commit comments