Skip to content

Commit c9f569a

Browse files
italomgItalo Garcia
andauthored
Support custom OkHttpClient on HoverflyClient (#260)
Co-authored-by: Italo Garcia <[email protected]>
1 parent 02b3fc0 commit c9f569a

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

src/main/java/io/specto/hoverfly/junit/api/HoverflyClient.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import io.specto.hoverfly.junit.core.model.Journal;
1313
import io.specto.hoverfly.junit.core.model.Request;
1414
import io.specto.hoverfly.junit.core.model.Simulation;
15+
import okhttp3.OkHttpClient;
1516

1617
/**
1718
* Http client for querying Hoverfly admin endpoints
@@ -129,6 +130,7 @@ class Builder {
129130
private String host = HoverflyConstants.LOCALHOST;
130131
private int port = HoverflyConstants.DEFAULT_ADMIN_PORT;
131132
private String authToken = null;
133+
private OkHttpClient client = null;
132134

133135
Builder() {
134136
}
@@ -157,8 +159,17 @@ public Builder withAuthToken() {
157159
return this;
158160
}
159161

162+
public Builder withHttpClient(OkHttpClient client) {
163+
this.client = client;
164+
return this;
165+
}
166+
160167
public HoverflyClient build() {
161-
return new OkHttpHoverflyClient(scheme, host, port, authToken);
168+
if (client == null) {
169+
return new OkHttpHoverflyClient(scheme, host, port, authToken);
170+
}
171+
172+
return new OkHttpHoverflyClient(scheme, host, port, client);
162173
}
163174
}
164175

src/main/java/io/specto/hoverfly/junit/api/OkHttpHoverflyClient.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ class OkHttpHoverflyClient implements HoverflyClient {
6060
.build();
6161
}
6262

63+
OkHttpHoverflyClient(String scheme, String host, int port, OkHttpClient client) {
64+
OBJECT_MAPPER.registerModule(new JavaTimeModule());
65+
this.client = client;
66+
this.baseUrl = new HttpUrl.Builder()
67+
.scheme(scheme)
68+
.host(host)
69+
.port(port)
70+
.build();
71+
}
72+
6373
@Override
6474
public void setSimulation(Simulation simulation) {
6575
try {

src/test/java/io/specto/hoverfly/junit/api/HoverflyClientTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import io.specto.hoverfly.junit.rule.HoverflyRule;
5+
import okhttp3.OkHttpClient;
56
import org.junit.ClassRule;
67
import org.junit.Rule;
78
import org.junit.Test;
@@ -55,6 +56,28 @@ public void shouldBeAbleToCreateHoverflyClientWithAuthToken() {
5556
assertThat(hoverflyClient.getHealth()).isTrue();
5657
}
5758

59+
@Test
60+
public void shouldBeAbleToCreateHoverflyClientWithHttpClient() {
61+
envVars.set("HOVERFLY_AUTH_TOKEN", "some-token");
62+
hoverflyRule.simulate(dsl(
63+
service("http://remote.host:12345")
64+
.get("/api/health")
65+
.willReturn(success())
66+
));
67+
68+
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
69+
OkHttpClient client = clientBuilder.build();
70+
71+
HoverflyClient hoverflyClient = HoverflyClient.custom()
72+
.host("remote.host")
73+
.port(12345)
74+
.withAuthToken()
75+
.withHttpClient(client)
76+
.build();
77+
78+
assertThat(hoverflyClient.getHealth()).isTrue();
79+
}
80+
5881
@Test
5982
public void shouldCreateDefaultClient() {
6083
hoverflyRule.simulate(dsl(

src/test/java/io/specto/hoverfly/junit/api/OkHttpHoverflyClientTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class OkHttpHoverflyClientTest {
4646
public void setUp() {
4747
startDefaultHoverfly();
4848
HoverflyConfiguration hoverflyConfig = hoverfly.getHoverflyConfig();
49-
client = new OkHttpHoverflyClient(hoverflyConfig.getScheme(), hoverflyConfig.getHost(), hoverflyConfig.getAdminPort(), null);
49+
client = new OkHttpHoverflyClient(hoverflyConfig.getScheme(), hoverflyConfig.getHost(), hoverflyConfig.getAdminPort(), "");
5050
}
5151

5252
@Test

0 commit comments

Comments
 (0)