Skip to content

Commit 25614d6

Browse files
committed
Adding test for not-preemptive auth codepath
1 parent b66d7d9 commit 25614d6

File tree

2 files changed

+60
-13
lines changed

2 files changed

+60
-13
lines changed

http-clients/apache-client/src/main/java/software/amazon/awssdk/http/apache/internal/utils/ApacheUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.apache.http.auth.AUTH;
2323
import org.apache.http.auth.AuthScope;
2424
import org.apache.http.auth.Credentials;
25-
import org.apache.http.auth.MalformedChallengeException;
2625
import org.apache.http.auth.NTCredentials;
2726
import org.apache.http.client.AuthCache;
2827
import org.apache.http.client.CredentialsProvider;
@@ -157,7 +156,7 @@ private static void addPreemptiveAuthenticationProxy(HttpClientContext clientCon
157156
authCache.put(targetHost, basicAuth);
158157
clientContext.setAuthCache(authCache);
159158
} catch (Exception e) {
160-
logger.warn(() -> "Failed to process synthetic challenge for preemptive proxy authentication: " + e.getMessage());
159+
logger.debug(() -> "Failed to process synthetic challenge for preemptive proxy authentication: " + e.getMessage());
161160
}
162161

163162
clientContext.setCredentialsProvider(credsProvider);

http-clients/apache-client/src/test/java/software/amazon/awssdk/http/apache/ProxyPreemptiveAuthTest.java

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
1919
import static com.github.tomakehurst.wiremock.client.WireMock.any;
20+
import static com.github.tomakehurst.wiremock.client.WireMock.anyRequestedFor;
2021
import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
2122
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
2223
import static org.assertj.core.api.Assertions.assertThat;
@@ -42,28 +43,28 @@
4243
*/
4344
public 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

Comments
 (0)