17
17
18
18
import static com .github .tomakehurst .wiremock .client .WireMock .aResponse ;
19
19
import static com .github .tomakehurst .wiremock .client .WireMock .any ;
20
+ import static com .github .tomakehurst .wiremock .client .WireMock .anyRequestedFor ;
20
21
import static com .github .tomakehurst .wiremock .client .WireMock .anyUrl ;
21
22
import static com .github .tomakehurst .wiremock .client .WireMock .matching ;
22
23
import static org .assertj .core .api .Assertions .assertThat ;
42
43
*/
43
44
public class ProxyPreemptiveAuthTest {
44
45
45
- private WireMockServer proxyServer ;
46
+ private WireMockServer mockProxy ;
46
47
private SdkHttpClient httpClient ;
47
48
48
49
@ BeforeEach
49
50
public void setup () {
50
- proxyServer = new WireMockServer (WireMockConfiguration .options ().dynamicPort ());
51
- proxyServer .start ();
51
+ mockProxy = new WireMockServer (WireMockConfiguration .options ().dynamicPort ());
52
+ mockProxy .start ();
52
53
}
53
54
54
55
@ AfterEach
55
56
public void teardown () {
56
57
if (httpClient != null ) {
57
58
httpClient .close ();
58
59
}
59
- if (proxyServer != null ) {
60
- proxyServer .stop ();
60
+ if (mockProxy != null ) {
61
+ mockProxy .stop ();
61
62
}
62
63
}
63
64
64
65
@ 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 ())
67
68
.withHeader ("Proxy-Authorization" , matching ("Basic .+" ))
68
69
.willReturn (aResponse ()
69
70
.withStatus (200 )
@@ -72,7 +73,7 @@ public void testPreemptiveAuthenticationSendsProxyAuthorizationHeader() throws E
72
73
// Create HTTP client with preemptive proxy authentication enabled
73
74
httpClient = ApacheHttpClient .builder ()
74
75
.proxyConfiguration (ProxyConfiguration .builder ()
75
- .endpoint (URI .create ("http://localhost:" + proxyServer .port ()))
76
+ .endpoint (URI .create ("http://localhost:" + mockProxy .port ()))
76
77
.username ("testuser" )
77
78
.password ("testpass" )
78
79
.preemptiveBasicAuthenticationEnabled (true )
@@ -92,9 +93,56 @@ public void testPreemptiveAuthenticationSendsProxyAuthorizationHeader() throws E
92
93
// Execute the request - should succeed with preemptive auth header
93
94
HttpExecuteResponse response = httpClient .prepareRequest (executeRequest ).call ();
94
95
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 ())
98
99
.withHeader ("Proxy-Authorization" , matching ("Basic .+" )));
99
100
}
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
+ }
100
148
}
0 commit comments