Skip to content

Commit aec2414

Browse files
committed
Testing changes
1 parent a4970d2 commit aec2414

2 files changed

Lines changed: 18 additions & 33 deletions

File tree

components/http/okHttp/src/main/java/com/microsoft/kiota/http/middleware/RedirectHandler.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,37 @@
2929
*/
3030
public class RedirectHandler implements Interceptor {
3131
@Nonnull private final RedirectHandlerOption mRedirectOption;
32+
@Nullable private final java.net.ProxySelector mProxySelector;
3233

3334
/**
3435
* Initialize using default redirect options, default IShouldRedirect and max redirect value
3536
*/
3637
public RedirectHandler() {
37-
this(null);
38+
this(null, null);
3839
}
3940

4041
/**
4142
* Initialize using custom redirect options.
4243
* @param redirectOption pass instance of redirect options to be used
4344
*/
4445
public RedirectHandler(@Nullable final RedirectHandlerOption redirectOption) {
46+
this(redirectOption, null);
47+
}
48+
49+
/**
50+
* Initialize using custom redirect options and proxy selector.
51+
* @param redirectOption pass instance of redirect options to be used
52+
* @param proxySelector The ProxySelector to use for determining proxy configuration, or null to use the system default
53+
*/
54+
public RedirectHandler(
55+
@Nullable final RedirectHandlerOption redirectOption,
56+
@Nullable final java.net.ProxySelector proxySelector) {
4557
if (redirectOption == null) {
4658
this.mRedirectOption = new RedirectHandlerOption();
4759
} else {
4860
this.mRedirectOption = redirectOption;
4961
}
62+
this.mProxySelector = proxySelector != null ? proxySelector : java.net.ProxySelector.getDefault();
5063
}
5164

5265
boolean isRedirected(
@@ -111,8 +124,7 @@ Request getRedirect(
111124

112125
// Scrub sensitive headers before following the redirect
113126
java.util.function.Function<HttpUrl, java.net.Proxy> proxyResolver =
114-
RedirectHandlerOption.getProxyResolver(
115-
chain.call().client().proxySelector());
127+
RedirectHandlerOption.getProxyResolver(mProxySelector);
116128
redirectOption.scrubSensitiveHeaders().scrubHeaders(
117129
requestBuilder, requestUrl, locationUrl, proxyResolver);
118130

components/http/okHttp/src/test/java/com/microsoft/kiota/http/middleware/RedirectHandlerTests.java

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,6 @@ void crossHostRedirectStripsAuthHeaders() throws Exception {
8484

8585
// Mock chain to get client without proxy
8686
Interceptor.Chain chain = mock(Interceptor.Chain.class);
87-
Call call = mock(Call.class);
88-
OkHttpClient client = new OkHttpClient.Builder().build();
89-
when(chain.call()).thenReturn(call);
90-
when(call.client()).thenReturn(client);
9187

9288
RedirectHandlerOption option = new RedirectHandlerOption();
9389
Request result = new RedirectHandler().getRedirect(original, redirect, option, chain);
@@ -143,17 +139,12 @@ void sameHostRedirectKeepsAllHeaders() throws Exception {
143139

144140
// Mock chain with proxy
145141
Interceptor.Chain chain = mock(Interceptor.Chain.class);
146-
Call call = mock(Call.class);
147142
ProxySelector proxySelector = mock(ProxySelector.class);
148143
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 8080));
149144
when(proxySelector.select(any(URI.class))).thenReturn(Collections.singletonList(proxy));
150145

151-
OkHttpClient client = new OkHttpClient.Builder().proxySelector(proxySelector).build();
152-
when(chain.call()).thenReturn(call);
153-
when(call.client()).thenReturn(client);
154-
155146
RedirectHandlerOption option = new RedirectHandlerOption();
156-
Request result = new RedirectHandler().getRedirect(original, redirect, option, chain);
147+
Request result = new RedirectHandler(option, proxySelector).getRedirect(original, redirect, option, chain);
157148

158149
assertNotNull(result);
159150
assertEquals("trusted.example.com", result.url().host());
@@ -183,17 +174,12 @@ void crossHostRedirectWithProxyKeepsProxyAuth() throws Exception {
183174

184175
// Mock chain with active proxy
185176
Interceptor.Chain chain = mock(Interceptor.Chain.class);
186-
Call call = mock(Call.class);
187177
ProxySelector proxySelector = mock(ProxySelector.class);
188178
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 8080));
189179
when(proxySelector.select(any(URI.class))).thenReturn(Collections.singletonList(proxy));
190180

191-
OkHttpClient client = new OkHttpClient.Builder().proxySelector(proxySelector).build();
192-
when(chain.call()).thenReturn(call);
193-
when(call.client()).thenReturn(client);
194-
195181
RedirectHandlerOption option = new RedirectHandlerOption();
196-
Request result = new RedirectHandler().getRedirect(original, redirect, option, chain);
182+
Request result = new RedirectHandler(option, proxySelector).getRedirect(original, redirect, option, chain);
197183

198184
assertNotNull(result);
199185
assertEquals("other.example.com", result.url().host());
@@ -224,17 +210,12 @@ void crossHostRedirectWithDirectProxyStripsProxyAuth() throws Exception {
224210

225211
// Mock chain with DIRECT proxy (no proxy)
226212
Interceptor.Chain chain = mock(Interceptor.Chain.class);
227-
Call call = mock(Call.class);
228213
ProxySelector proxySelector = mock(ProxySelector.class);
229214
when(proxySelector.select(any(URI.class)))
230215
.thenReturn(Collections.singletonList(Proxy.NO_PROXY));
231216

232-
OkHttpClient client = new OkHttpClient.Builder().proxySelector(proxySelector).build();
233-
when(chain.call()).thenReturn(call);
234-
when(call.client()).thenReturn(client);
235-
236217
RedirectHandlerOption option = new RedirectHandlerOption();
237-
Request result = new RedirectHandler().getRedirect(original, redirect, option, chain);
218+
Request result = new RedirectHandler(option, proxySelector).getRedirect(original, redirect, option, chain);
238219

239220
assertNotNull(result);
240221
assertEquals("other.example.com", result.url().host());
@@ -268,10 +249,6 @@ void schemeChangeStripsAuthHeaders() throws Exception {
268249

269250
// Mock chain without proxy
270251
Interceptor.Chain chain = mock(Interceptor.Chain.class);
271-
Call call = mock(Call.class);
272-
OkHttpClient client = new OkHttpClient.Builder().build();
273-
when(chain.call()).thenReturn(call);
274-
when(call.client()).thenReturn(client);
275252

276253
RedirectHandlerOption option = new RedirectHandlerOption();
277254
Request result = new RedirectHandler().getRedirect(original, redirect, option, chain);
@@ -310,10 +287,6 @@ void customScrubberIsUsed() throws Exception {
310287

311288
// Mock chain
312289
Interceptor.Chain chain = mock(Interceptor.Chain.class);
313-
Call call = mock(Call.class);
314-
OkHttpClient client = new OkHttpClient.Builder().build();
315-
when(chain.call()).thenReturn(call);
316-
when(call.client()).thenReturn(client);
317290

318291
RedirectHandlerOption option = new RedirectHandlerOption(5, null, customScrubber);
319292
Request result = new RedirectHandler().getRedirect(original, redirect, option, chain);

0 commit comments

Comments
 (0)