Skip to content

Commit 8e7d5b2

Browse files
committed
Some refactors and fix unit tests
1 parent 39409a8 commit 8e7d5b2

11 files changed

Lines changed: 49 additions & 37 deletions

File tree

junit5/src/main/java/io/specto/hoverfly/junit5/HoverflyExtensionUtils.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.specto.hoverfly.junit5.api.HoverflyConfig;
88
import io.specto.hoverfly.junit5.api.HoverflySimulate;
99
import io.specto.hoverfly.junit5.api.UnsetSimulationPreprocessor;
10+
import org.apache.commons.lang3.StringUtils;
1011
import org.junit.platform.commons.util.ReflectionUtils;
1112

1213
import java.nio.file.Path;
@@ -29,11 +30,16 @@ static io.specto.hoverfly.junit.core.HoverflyConfig getHoverflyConfigs(HoverflyC
2930
configs = remoteConfigs().host(config.remoteHost());
3031
} else {
3132
configs = localConfigs()
32-
.caCert(config.caCertPath(), config.caCertKeyPath())
33-
.clientAuth(config.clientCertPath(), config.clientKeyPath(), config.clientAuthDestination())
34-
.clientAuthCaCertPath(config.clientCaCertPath())
3533
.upstreamProxy(config.upstreamProxy())
3634
.logLevel(config.logLevel());
35+
if (StringUtils.isNotBlank(config.caCertPath()) || StringUtils.isNotBlank(config.caKeyPath())) {
36+
((LocalHoverflyConfig) configs).overrideDefaultCaCert(config.caCertPath(), config.caKeyPath());
37+
}
38+
if (StringUtils.isNotBlank(config.clientCertPath()) || StringUtils.isNotBlank(config.clientKeyPath())) {
39+
((LocalHoverflyConfig) configs)
40+
.enableClientAuth(config.clientCertPath(), config.clientKeyPath(), config.clientAuthDestination())
41+
.clientAuthCaCertPath(config.clientCaCertPath());
42+
}
3743
if (config.plainHttpTunneling()) {
3844
((LocalHoverflyConfig) configs).plainHttpTunneling();
3945
}

junit5/src/main/java/io/specto/hoverfly/junit5/api/HoverflyConfig.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@
6464
String sslKeyPath() default "";
6565

6666
/**
67-
* Custom CA certificate for Hoverfly {@link LocalHoverflyConfig#caCert(String, String)}
67+
* Custom CA certificate for Hoverfly {@link LocalHoverflyConfig#overrideDefaultCaCert(String, String)}
6868
*/
6969
String caCertPath() default "";
7070

7171
/**
72-
* Key for Hoverfly custom CA cert {@link LocalHoverflyConfig#caCert(String, String)}
72+
* Key for Hoverfly custom CA cert {@link LocalHoverflyConfig#overrideDefaultCaCert(String, String)}
7373
*/
74-
String caCertKeyPath() default "";
74+
String caKeyPath() default "";
7575

7676
/**
7777
* External Hoverfly instance hostname {@link RemoteHoverflyConfig#host(String)}

junit5/src/test/java/io/specto/hoverfly/junit5/HoverflyConfigTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@ void shouldUseDefaultValues(Hoverfly hoverfly) {
3636
assertThat(configs.getCaptureHeaders()).isEmpty();
3737
assertThat(configs.getDestination()).isEmpty();
3838
assertThat(configs.getUpstreamProxy()).isEmpty();
39-
assertThat(configs.getSslCertificatePath()).isEmpty();
40-
assertThat(configs.getSslKeyPath()).isEmpty();
39+
assertThat(configs.getSslCertificatePath()).isNull();
40+
assertThat(configs.getSslKeyPath()).isNull();
4141
assertThat(configs.getHost()).isEqualTo("localhost");
4242
assertThat(configs.getScheme()).isEqualTo("http");
4343
assertThat(configs.isStatefulCapture()).isFalse();
4444
assertThat(configs.getSimulationPreprocessor()).isEmpty();
4545
assertThat(configs.getCommands()).isEmpty();
4646
assertThat(configs.getLogLevel()).isEqualTo(Optional.of(LogLevel.INFO));
47+
assertThat(configs.getClientCertPath()).isNull();
48+
assertThat(configs.getClientKeyPath()).isNull();
49+
assertThat(configs.getClientAuthDestination()).isNull();
50+
assertThat(configs.getClientCaCertPath()).isNull();
4751
}
4852
}
4953

src/main/java/io/specto/hoverfly/junit/core/Hoverfly.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -195,24 +195,23 @@ private void startHoverflyProcess() {
195195
commands.add("ca.key");
196196
}
197197

198-
if (StringUtils.isNotBlank(hoverflyConfig.getClientCertPath())) {
198+
if (hoverflyConfig.isClientAuthEnabled()) {
199199
tempFileManager.copyClassPathResource(hoverflyConfig.getClientCertPath(), "client-auth.crt");
200+
tempFileManager.copyClassPathResource(hoverflyConfig.getClientKeyPath(), "client-auth.key");
200201
commands.add("-client-authentication-client-cert");
201202
commands.add("client-auth.crt");
202-
}
203-
if (StringUtils.isNotBlank(hoverflyConfig.getClientKeyPath())) {
204-
tempFileManager.copyClassPathResource(hoverflyConfig.getClientKeyPath(), "client-auth.key");
203+
205204
commands.add("-client-authentication-client-key");
206205
commands.add("client-auth.key");
207-
}
208-
if (StringUtils.isNotBlank(hoverflyConfig.getClientAuthDestination())) {
206+
209207
commands.add("-client-authentication-destination");
210208
commands.add(hoverflyConfig.getClientAuthDestination());
211-
}
212-
if (StringUtils.isNotBlank(hoverflyConfig.getClientCaCertPath())) {
213-
tempFileManager.copyClassPathResource(hoverflyConfig.getClientCaCertPath(), "client-ca.crt");
214-
commands.add("-client-authentication-ca-cert");
215-
commands.add("client-ca.crt");
209+
210+
if (StringUtils.isNotBlank(hoverflyConfig.getClientCaCertPath())) {
211+
tempFileManager.copyClassPathResource(hoverflyConfig.getClientCaCertPath(), "client-ca.crt");
212+
commands.add("-client-authentication-ca-cert");
213+
commands.add("client-ca.crt");
214+
}
216215
}
217216

218217
if (hoverflyConfig.isPlainHttpTunneling()) {

src/main/java/io/specto/hoverfly/junit/core/config/HoverflyConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,4 +331,8 @@ public String getClientCaCertPath() {
331331
public void setClientCaCertPath(String clientCaCertPath) {
332332
this.clientCaCertPath = clientCaCertPath;
333333
}
334+
335+
public boolean isClientAuthEnabled() {
336+
return isNotBlank(clientCertPath) && isNotBlank(clientKeyPath) && isNotBlank(clientAuthDestination);
337+
}
334338
}

src/main/java/io/specto/hoverfly/junit/core/config/LocalHoverflyConfig.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
public class LocalHoverflyConfig extends HoverflyConfig {
2828

2929
private String caCertPath;
30-
private String caCertKeyPath;
30+
private String caKeyPath;
3131
private boolean tlsVerificationDisabled;
3232
private boolean plainHttpTunneling;
3333
private LocalMiddleware localMiddleware;
@@ -61,7 +61,7 @@ public LocalHoverflyConfig sslCertificatePath(String sslCertificatePath) {
6161
*/
6262
@Deprecated
6363
public LocalHoverflyConfig sslKeyPath(String sslKeyPath) {
64-
this.caCertKeyPath = sslKeyPath;
64+
this.caKeyPath = sslKeyPath;
6565
return this;
6666
}
6767

@@ -71,9 +71,9 @@ public LocalHoverflyConfig sslKeyPath(String sslKeyPath) {
7171
* @param keyPath key file in classpath. Must be any PEM encoded key, with .key or .pem extensions
7272
* @return the {@link LocalHoverflyConfig} for further customizations
7373
*/
74-
public LocalHoverflyConfig caCert(String certPath, String keyPath) {
74+
public LocalHoverflyConfig overrideDefaultCaCert(String certPath, String keyPath) {
7575
this.caCertPath = certPath;
76-
this.caCertKeyPath = keyPath;
76+
this.caKeyPath = keyPath;
7777
return this;
7878
}
7979

@@ -174,7 +174,7 @@ public HoverflyConfig binaryLocation(String binaryLocation) {
174174
* @param destinations the destination filter to what target urls to enable mutual TLS authentication. Enable for all remote hosts if not provided.
175175
* @return the {@link LocalHoverflyConfig} for further customizations
176176
*/
177-
public LocalHoverflyConfig clientAuth(String clientCertPath, String clientKeyPath, String... destinations) {
177+
public LocalHoverflyConfig enableClientAuth(String clientCertPath, String clientKeyPath, String... destinations) {
178178
this.clientCertPath = clientCertPath;
179179
this.clientKeyPath = clientKeyPath;
180180
if (destinations != null) {
@@ -202,7 +202,7 @@ public HoverflyConfiguration build() {
202202
HoverflyConfiguration configs = new HoverflyConfiguration(proxyPort, adminPort, proxyLocalHost, destination,
203203
proxyCaCert, captureHeaders, webServer, hoverflyLogger, logLevel, statefulCapture, incrementalCapture, simulationPreprocessor);
204204
configs.setSslCertificatePath(caCertPath);
205-
configs.setSslKeyPath(caCertKeyPath);
205+
configs.setSslKeyPath(caKeyPath);
206206
configs.setTlsVerificationDisabled(tlsVerificationDisabled);
207207
configs.setPlainHttpTunneling(plainHttpTunneling);
208208
configs.setLocalMiddleware(localMiddleware);

src/test/java/io/specto/hoverfly/junit/core/HoverflyConfigTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public void shouldSetDestinations() {
209209
@Test
210210
public void shouldSetClientAuth() {
211211
HoverflyConfiguration configs = localConfigs()
212-
.clientAuth("ssl/cert.pem", "ssl/key.pem")
212+
.enableClientAuth("ssl/cert.pem", "ssl/key.pem")
213213
.build();
214214

215215
assertThat(configs.getClientCertPath()).isEqualTo("ssl/cert.pem");
@@ -220,7 +220,7 @@ public void shouldSetClientAuth() {
220220
@Test
221221
public void shouldSetClientAuthWithDestinationFilter() {
222222
HoverflyConfiguration configs = localConfigs()
223-
.clientAuth("ssl/cert.pem", "ssl/key.pem", "foo.com", "bar.com")
223+
.enableClientAuth("ssl/cert.pem", "ssl/key.pem", "foo.com", "bar.com")
224224
.build();
225225

226226
assertThat(configs.getClientCertPath()).isEqualTo("ssl/cert.pem");

src/test/java/io/specto/hoverfly/junit/core/HoverflyTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import io.specto.hoverfly.junit.core.model.RequestFieldMatcher;
1919
import io.specto.hoverfly.junit.core.model.RequestResponsePair;
2020
import io.specto.hoverfly.junit.core.model.Simulation;
21-
import java.util.Optional;
2221
import org.apache.http.HttpResponse;
2322
import org.apache.http.client.HttpClient;
2423
import org.apache.http.client.methods.HttpGet;
@@ -348,7 +347,7 @@ public void shouldSetTrustStoreWhenStartingHoverfly() {
348347
@Test
349348
public void shouldConfigSslContextWithCustomCaCert() {
350349
// Given
351-
hoverfly = new Hoverfly(localConfigs().caCert("ssl/ca.crt", "ssl/ca.key"), SIMULATE);
350+
hoverfly = new Hoverfly(localConfigs().overrideDefaultCaCert("ssl/ca.crt", "ssl/ca.key"), SIMULATE);
352351
SslConfigurer sslConfigurer = mock(SslConfigurer.class);
353352
Whitebox.setInternalState(hoverfly, "sslConfigurer", sslConfigurer);
354353

@@ -411,7 +410,7 @@ public void shouldResetSimulationJournalAndStateWhenCallingReset() {
411410
@Test
412411
public void shouldCopySslCertAndKeyToTempFolderIfPresent () {
413412
// Given
414-
hoverfly = new Hoverfly(localConfigs().caCert("ssl/ca.crt", "ssl/ca.key"), SIMULATE);
413+
hoverfly = new Hoverfly(localConfigs().overrideDefaultCaCert("ssl/ca.crt", "ssl/ca.key"), SIMULATE);
415414
TempFileManager tempFileManager = spy(TempFileManager.class);
416415
Whitebox.setInternalState(hoverfly, "tempFileManager", tempFileManager);
417416

@@ -427,7 +426,7 @@ public void shouldCopySslCertAndKeyToTempFolderIfPresent () {
427426
public void shouldCopyClientCertAndKeyToTempFolderIfPresent () {
428427
// Given
429428
hoverfly = new Hoverfly(localConfigs()
430-
.clientAuth("ssl/ca.crt", "ssl/ca.key")
429+
.enableClientAuth("ssl/ca.crt", "ssl/ca.key")
431430
.clientAuthCaCertPath("ssl/client-ca.crt")
432431
, SIMULATE);
433432
TempFileManager tempFileManager = spy(TempFileManager.class);

src/test/java/io/specto/hoverfly/junit/core/config/HoverflyConfigValidatorTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ public void shouldAssignPortForLocalHoverflyInstanceIfNotConfigured() {
4242
@Test
4343
public void shouldThrowExceptionIfOnlySslKeyIsConfigured() {
4444

45-
assertThatThrownBy(() -> localConfigs().caCert("", "ssl/ca.key").build())
45+
assertThatThrownBy(() -> localConfigs().overrideDefaultCaCert("", "ssl/ca.key").build())
4646
.isInstanceOf(IllegalArgumentException.class)
4747
.hasMessageContaining("Both ca cert and key files are required to override the default Hoverfly ca cert.");
4848
}
4949

5050
@Test
5151
public void shouldThrowExceptionIfOnlySslCertIsConfigured() {
5252

53-
assertThatThrownBy(() -> localConfigs().caCert("ssl/ca.crt", "").build())
53+
assertThatThrownBy(() -> localConfigs().overrideDefaultCaCert("ssl/ca.crt", "").build())
5454
.isInstanceOf(IllegalArgumentException.class)
5555
.hasMessageContaining("Both ca cert and key files are required to override the default Hoverfly ca cert.");
5656
}
@@ -59,15 +59,15 @@ public void shouldThrowExceptionIfOnlySslCertIsConfigured() {
5959
@Test
6060
public void shouldThrowExceptionIfOnlyClientKeyIsConfigured() {
6161

62-
assertThatThrownBy(() -> localConfigs().clientAuth("", "ssl/ca.key").build())
62+
assertThatThrownBy(() -> localConfigs().enableClientAuth("", "ssl/ca.key").build())
6363
.isInstanceOf(IllegalArgumentException.class)
6464
.hasMessageContaining("Both client cert and key files are required to enable mutual TLS authentication.");
6565
}
6666

6767
@Test
6868
public void shouldThrowExceptionIfOnlyClientCertIsConfigured() {
6969

70-
assertThatThrownBy(() -> localConfigs().clientAuth("ssl/ca.crt", "").build())
70+
assertThatThrownBy(() -> localConfigs().enableClientAuth("ssl/ca.crt", "").build())
7171
.isInstanceOf(IllegalArgumentException.class)
7272
.hasMessageContaining("Both client cert and key files are required to enable mutual TLS authentication.");
7373
}

src/test/java/io/specto/hoverfly/ruletest/HoverflyRuleMutualTlsAuthenticationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class HoverflyRuleMutualTlsAuthenticationTest {
1919
@ClassRule
2020
public static HoverflyRule hoverflyRule = HoverflyRule.inCaptureOrSimulationMode(
2121
"mutual-tls.json",
22-
localConfigs().clientAuth("ssl/client-auth.pem", "ssl/client-auth.key"));
22+
localConfigs().enableClientAuth("ssl/client-auth.pem", "ssl/client-auth.key"));
2323

2424
@Test
2525
public void shouldWorkWithTestServerRequiredClientCertificate() {

0 commit comments

Comments
 (0)