1717
1818import static com .github .tomakehurst .wiremock .client .WireMock .aResponse ;
1919import static com .github .tomakehurst .wiremock .client .WireMock .any ;
20+ import static com .github .tomakehurst .wiremock .client .WireMock .anyRequestedFor ;
2021import static com .github .tomakehurst .wiremock .client .WireMock .anyUrl ;
2122import static com .github .tomakehurst .wiremock .client .WireMock .matching ;
2223import static org .assertj .core .api .Assertions .assertThat ;
4243 */
4344public class ProxyPreemptiveAuthTest {
4445
45- private WireMockServer proxyServer ;
46+ private WireMockServer mockProxy ;
4647 private SdkHttpClient httpClient ;
4748
4849 @ BeforeEach
4950 public void setup () {
50- proxyServer = new WireMockServer (WireMockConfiguration .options ().dynamicPort ());
51- proxyServer .start ();
51+ mockProxy = new WireMockServer (WireMockConfiguration .options ().dynamicPort ());
52+ mockProxy .start ();
5253 }
5354
5455 @ AfterEach
5556 public void teardown () {
5657 if (httpClient != null ) {
5758 httpClient .close ();
5859 }
59- if (proxyServer != null ) {
60- proxyServer .stop ();
60+ if (mockProxy != null ) {
61+ mockProxy .stop ();
6162 }
6263 }
6364
6465 @ Test
65- public void testPreemptiveAuthenticationSendsProxyAuthorizationHeader () throws Exception {
66- proxyServer .stubFor (any (anyUrl ())
66+ public void addPreemptiveAuthenticationProxy_whenPreemptiveAuthEnabled_shouldSendProxyAuthorizationHeader () throws Exception {
67+ mockProxy .stubFor (any (anyUrl ())
6768 .withHeader ("Proxy-Authorization" , matching ("Basic .+" ))
6869 .willReturn (aResponse ()
6970 .withStatus (200 )
@@ -72,7 +73,7 @@ public void testPreemptiveAuthenticationSendsProxyAuthorizationHeader() throws E
7273 // Create HTTP client with preemptive proxy authentication enabled
7374 httpClient = ApacheHttpClient .builder ()
7475 .proxyConfiguration (ProxyConfiguration .builder ()
75- .endpoint (URI .create ("http://localhost:" + proxyServer .port ()))
76+ .endpoint (URI .create ("http://localhost:" + mockProxy .port ()))
7677 .username ("testuser" )
7778 .password ("testpass" )
7879 .preemptiveBasicAuthenticationEnabled (true )
@@ -92,9 +93,56 @@ public void testPreemptiveAuthenticationSendsProxyAuthorizationHeader() throws E
9293 // Execute the request - should succeed with preemptive auth header
9394 HttpExecuteResponse response = httpClient .prepareRequest (executeRequest ).call ();
9495 assertThat (response .httpResponse ().statusCode ()).isEqualTo (200 );
95-
96- // Verify that the proxy received the request with Proxy-Authorization header
97- proxyServer .verify (WireMock .getRequestedFor (anyUrl ())
96+
97+ mockProxy . verify ( 1 , anyRequestedFor ( anyUrl ()));
98+ mockProxy .verify (WireMock .getRequestedFor (anyUrl ())
9899 .withHeader ("Proxy-Authorization" , matching ("Basic .+" )));
99100 }
101+
102+ @ Test
103+ public void addPreemptiveAuthenticationProxy_whenPreemptiveAuthDisabled_shouldUseChallengeResponseAuth () throws Exception {
104+ // First request without auth header should get 407
105+ mockProxy .stubFor (any (anyUrl ())
106+ .willReturn (aResponse ()
107+ .withStatus (407 )
108+ .withHeader ("Proxy-Authenticate" , "Basic realm=\" proxy\" " )));
109+
110+ // Second request with auth header should succeed
111+ mockProxy .stubFor (any (anyUrl ())
112+ .withHeader ("Proxy-Authorization" , matching ("Basic .+" ))
113+ .willReturn (aResponse ()
114+ .withStatus (200 )
115+ .withBody ("Success" )));
116+
117+ // Create HTTP client with preemptive proxy authentication disabled
118+ httpClient = ApacheHttpClient .builder ()
119+ .proxyConfiguration (ProxyConfiguration .builder ()
120+ .endpoint (URI .create ("http://localhost:" + mockProxy .port ()))
121+ .username ("testuser" )
122+ .password ("testpass" )
123+ .preemptiveBasicAuthenticationEnabled (false )
124+ .build ())
125+ .build ();
126+
127+ // Create a request
128+ SdkHttpRequest request = SdkHttpRequest .builder ()
129+ .method (SdkHttpMethod .GET )
130+ .uri (URI .create ("http://example.com/test" ))
131+ .build ();
132+
133+ HttpExecuteRequest executeRequest = HttpExecuteRequest .builder ()
134+ .request (request )
135+ .build ();
136+
137+ // Execute the request - should succeed after challenge-response
138+ HttpExecuteResponse response = httpClient .prepareRequest (executeRequest ).call ();
139+ assertThat (response .httpResponse ().statusCode ()).isEqualTo (200 );
140+
141+ // Verify challenge-response flow - 2 requests total
142+ mockProxy .verify (2 , anyRequestedFor (anyUrl ()));
143+ // First request without auth header
144+ mockProxy .verify (1 , anyRequestedFor (anyUrl ()).withoutHeader ("Proxy-Authorization" ));
145+ // Second request with auth header
146+ mockProxy .verify (1 , anyRequestedFor (anyUrl ()).withHeader ("Proxy-Authorization" , matching ("Basic .+" )));
147+ }
100148}
0 commit comments