13
13
package org .asynchttpclient .proxy ;
14
14
15
15
import io .github .artsok .RepeatedIfExceptionsTest ;
16
+ import jakarta .servlet .ServletException ;
17
+ import jakarta .servlet .http .HttpServletRequest ;
18
+ import jakarta .servlet .http .HttpServletResponse ;
19
+
16
20
import org .asynchttpclient .AbstractBasicTest ;
17
21
import org .asynchttpclient .AsyncHttpClient ;
18
22
import org .asynchttpclient .AsyncHttpClientConfig ;
19
23
import org .asynchttpclient .RequestBuilder ;
20
24
import org .asynchttpclient .Response ;
25
+ import org .asynchttpclient .proxy .ProxyServer .Builder ;
21
26
import org .asynchttpclient .request .body .generator .ByteArrayBodyGenerator ;
22
27
import org .asynchttpclient .test .EchoHandler ;
28
+ import org .asynchttpclient .util .HttpConstants ;
23
29
import org .eclipse .jetty .proxy .ConnectHandler ;
30
+ import org .eclipse .jetty .server .Request ;
24
31
import org .eclipse .jetty .server .Server ;
25
32
import org .eclipse .jetty .server .ServerConnector ;
26
33
import org .eclipse .jetty .server .handler .AbstractHandler ;
27
34
import org .junit .jupiter .api .AfterEach ;
28
35
import org .junit .jupiter .api .BeforeEach ;
36
+ import org .junit .jupiter .api .Test ;
29
37
30
38
import static org .asynchttpclient .Dsl .asyncHttpClient ;
31
39
import static org .asynchttpclient .Dsl .config ;
37
45
import static org .asynchttpclient .test .TestUtils .addHttpsConnector ;
38
46
import static org .junit .jupiter .api .Assertions .assertEquals ;
39
47
48
+ import java .io .IOException ;
49
+
40
50
/**
41
51
* Proxy usage tests.
42
52
*/
@@ -46,7 +56,7 @@ public class HttpsProxyTest extends AbstractBasicTest {
46
56
47
57
@ Override
48
58
public AbstractHandler configureHandler () throws Exception {
49
- return new ConnectHandler ();
59
+ return new ProxyHandler ();
50
60
}
51
61
52
62
@ Override
@@ -142,4 +152,38 @@ public void testPooledConnectionsWithProxy() throws Exception {
142
152
assertEquals (200 , response2 .getStatusCode ());
143
153
}
144
154
}
155
+
156
+ @ RepeatedIfExceptionsTest (repeats = 5 )
157
+ public void testFailedConnectWithProxy () throws Exception {
158
+ try (AsyncHttpClient asyncHttpClient = asyncHttpClient (config ().setFollowRedirect (true ).setUseInsecureTrustManager (true ).setKeepAlive (true ))) {
159
+ Builder proxyServer = proxyServer ("localhost" , port1 );
160
+ proxyServer .setCustomHeaders (r -> r .getHeaders ().add (ProxyHandler .HEADER_FORBIDDEN , "1" ));
161
+ RequestBuilder rb = get (getTargetUrl2 ()).setProxyServer (proxyServer );
162
+
163
+ Response response1 = asyncHttpClient .executeRequest (rb .build ()).get ();
164
+ assertEquals (403 , response1 .getStatusCode ());
165
+
166
+ Response response2 = asyncHttpClient .executeRequest (rb .build ()).get ();
167
+ assertEquals (403 , response2 .getStatusCode ());
168
+
169
+ Response response3 = asyncHttpClient .executeRequest (rb .build ()).get ();
170
+ assertEquals (403 , response3 .getStatusCode ());
171
+ }
172
+ }
173
+
174
+ public static class ProxyHandler extends ConnectHandler {
175
+ final static String HEADER_FORBIDDEN = "X-REJECT-REQUEST" ;
176
+
177
+ @ Override
178
+ public void handle (String s , Request r , HttpServletRequest request , HttpServletResponse response ) throws IOException , ServletException {
179
+ if (HttpConstants .Methods .CONNECT .equalsIgnoreCase (request .getMethod ())) {
180
+ if (request .getHeader (HEADER_FORBIDDEN ) != null ) {
181
+ response .setStatus (HttpServletResponse .SC_FORBIDDEN );
182
+ r .setHandled (true );
183
+ return ;
184
+ }
185
+ }
186
+ super .handle (s , r , request , response );
187
+ }
188
+ }
145
189
}
0 commit comments