|
| 1 | +package io.quarkus.restclient.runtime; |
| 2 | + |
| 3 | +import static org.mockito.ArgumentMatchers.any; |
| 4 | +import static org.mockito.Mockito.mock; |
| 5 | +import static org.mockito.Mockito.never; |
| 6 | +import static org.mockito.Mockito.times; |
| 7 | +import static org.mockito.Mockito.verify; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | + |
| 10 | +import java.lang.reflect.Field; |
| 11 | +import java.lang.reflect.Method; |
| 12 | +import java.security.SecureRandom; |
| 13 | +import java.util.Optional; |
| 14 | + |
| 15 | +import javax.net.ssl.SSLContext; |
| 16 | + |
| 17 | +import org.eclipse.microprofile.config.Config; |
| 18 | +import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import io.quarkus.restclient.NoopHostnameVerifier; |
| 22 | + |
| 23 | +public class QuarkusRestClientBuilderTest { |
| 24 | + |
| 25 | + private static final String TLS_TRUST_ALL = "quarkus.tls.trust-all"; |
| 26 | + |
| 27 | + @Test |
| 28 | + public void preservesCustomSslContextWhenTrustAllEnabled() throws Exception { |
| 29 | + QuarkusRestClientBuilder builder = new QuarkusRestClientBuilder(); |
| 30 | + |
| 31 | + // set a mocked config that enables trust-all |
| 32 | + Config mockConfig = mock(Config.class); |
| 33 | + when(mockConfig.getOptionalValue(TLS_TRUST_ALL, Boolean.class)).thenReturn(Optional.of(Boolean.TRUE)); |
| 34 | + setQuarkusRestClientBuilderField(builder, "config", mockConfig); |
| 35 | + |
| 36 | + // set a custom SSLContext on the builder |
| 37 | + SSLContext custom = SSLContext.getInstance("TLS"); |
| 38 | + custom.init(null, null, new SecureRandom()); |
| 39 | + setQuarkusRestClientBuilderField(builder, "sslContext", custom); |
| 40 | + |
| 41 | + ResteasyClientBuilder clientBuilder = mock(ResteasyClientBuilder.class); |
| 42 | + |
| 43 | + // invoke private configureTrustAll method |
| 44 | + Method m = QuarkusRestClientBuilder.class.getDeclaredMethod("configureTrustAll", ResteasyClientBuilder.class); |
| 45 | + m.setAccessible(true); |
| 46 | + m.invoke(builder, clientBuilder); |
| 47 | + |
| 48 | + // hostname verifier should be set to NoopHostnameVerifier |
| 49 | + verify(clientBuilder, times(1)).hostnameVerifier(any(NoopHostnameVerifier.class)); |
| 50 | + // but sslContext should NOT be overridden when the user provided one |
| 51 | + verify(clientBuilder, never()).sslContext(any(SSLContext.class)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void createsTrustAllSslContextWhenNoCustomProvided() throws Exception { |
| 56 | + QuarkusRestClientBuilder builder = new QuarkusRestClientBuilder(); |
| 57 | + |
| 58 | + // set a mocked config that enables trust-all |
| 59 | + Config mockConfig = mock(Config.class); |
| 60 | + when(mockConfig.getOptionalValue(TLS_TRUST_ALL, Boolean.class)).thenReturn(Optional.of(Boolean.TRUE)); |
| 61 | + setQuarkusRestClientBuilderField(builder, "config", mockConfig); |
| 62 | + |
| 63 | + // ensure sslContext field is null (no custom provided) |
| 64 | + setQuarkusRestClientBuilderField(builder, "sslContext", null); |
| 65 | + |
| 66 | + ResteasyClientBuilder clientBuilder = mock(ResteasyClientBuilder.class); |
| 67 | + |
| 68 | + // invoke private configureTrustAll method |
| 69 | + Method m = QuarkusRestClientBuilder.class.getDeclaredMethod("configureTrustAll", ResteasyClientBuilder.class); |
| 70 | + m.setAccessible(true); |
| 71 | + m.invoke(builder, clientBuilder); |
| 72 | + |
| 73 | + // hostname verifier should be set to NoopHostnameVerifier |
| 74 | + verify(clientBuilder, times(1)).hostnameVerifier(any(NoopHostnameVerifier.class)); |
| 75 | + // sslContext should be set to a newly created SSLContext |
| 76 | + verify(clientBuilder, times(1)).sslContext(any(SSLContext.class)); |
| 77 | + } |
| 78 | + |
| 79 | + private static void setQuarkusRestClientBuilderField(Object target, String name, Object value) throws Exception { |
| 80 | + Field f = QuarkusRestClientBuilder.class.getDeclaredField(name); |
| 81 | + f.setAccessible(true); |
| 82 | + f.set(target, value); |
| 83 | + } |
| 84 | +} |
0 commit comments