-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(retrofit2): Improvise OkHttpClientProvider to accept interceptors (
#1220) * feat(web): add method to get OkHttpClient with given list of interceptors added * feat(web): add a method each to ServiceClientFactory and ServiceClientProvider to support list of interceptors. * test(web): add OkHttpClientProviderTest class
- Loading branch information
1 parent
2fef420
commit b4097e5
Showing
7 changed files
with
171 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
kork-web/src/test/java/com/netflix/spinnaker/config/okhttp3/OkHttpClientProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2025 OpsMx, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.config.okhttp3; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.netflix.spinnaker.config.ServiceEndpoint; | ||
import com.netflix.spinnaker.kork.exceptions.SystemException; | ||
import com.netflix.spinnaker.okhttp.OkHttpClientConfigurationProperties; | ||
import java.util.List; | ||
import okhttp3.Interceptor; | ||
import okhttp3.OkHttpClient; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@SpringBootTest( | ||
webEnvironment = SpringBootTest.WebEnvironment.NONE, | ||
classes = {OkHttpClient.class, OkHttpClientConfigurationProperties.class}) | ||
public class OkHttpClientProviderTest { | ||
|
||
@Mock private DefaultOkHttpClientBuilderProvider defaultProvider; | ||
|
||
@Mock private ServiceEndpoint service; | ||
|
||
private OkHttpClient.Builder builder; | ||
|
||
@Mock private Interceptor interceptor; | ||
|
||
@Mock private Interceptor interceptor2; | ||
|
||
private OkHttpClientProvider clientProvider; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
builder = new OkHttpClient.Builder(); | ||
clientProvider = new OkHttpClientProvider(List.of(defaultProvider)); | ||
} | ||
|
||
@Test | ||
void getClient_withInterceptors_shouldAddInterceptors() { | ||
when(defaultProvider.supports(service)).thenReturn(true); | ||
when(defaultProvider.get(service)).thenReturn(builder); | ||
|
||
OkHttpClient result = clientProvider.getClient(service, List.of(interceptor, interceptor2)); | ||
|
||
assertEquals(result.interceptors().size(), 2); | ||
assertEquals(result.interceptors().get(0), interceptor); | ||
assertEquals(result.interceptors().get(1), interceptor2); | ||
} | ||
|
||
@Test | ||
void getClient_noProviderFound_shouldThrowException() { | ||
when(defaultProvider.supports(service)).thenReturn(false); | ||
when(service.getBaseUrl()).thenReturn("http://example.com"); | ||
|
||
SystemException exception = | ||
assertThrows( | ||
SystemException.class, | ||
() -> clientProvider.getClient(service), | ||
"Expected an exception if no provider supports the service"); | ||
assertEquals( | ||
"No client provider found for url (http://example.com)", | ||
exception.getMessage(), | ||
"The exception message should indicate no provider was found."); | ||
} | ||
} |