Skip to content

Commit 88a1d05

Browse files
committed
Docker.httpClient() + unit tests
1 parent 6667347 commit 88a1d05

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

src/main/java/com/amihaiemil/docker/Docker.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
*/
2626
package com.amihaiemil.docker;
2727

28+
import org.apache.http.client.HttpClient;
29+
2830
import java.io.IOException;
2931

3032
/**
@@ -79,4 +81,19 @@ public interface Docker {
7981
* @return Swarm.
8082
*/
8183
Swarm swarm();
84+
85+
/**
86+
* The underlying, immutable, Apache HttpClient.<br><br>
87+
*
88+
* Use this method to fetch the underlying HttpClient and perform your own
89+
* HTTP requests, in case the API is missing the desired method.<br><br>
90+
*
91+
* Usage in any other scenario is discouraged. Try to find the desired
92+
* method before using this. Also, if the method is missing, open an
93+
* Issue at https://www.github.com/amihaiemil/docker-java-api, maybe
94+
* it can be implemented and released quickly.
95+
*
96+
* @return The underlying HttpClient.
97+
*/
98+
HttpClient httpClient();
8299
}

src/main/java/com/amihaiemil/docker/RtDocker.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,9 @@ public final Swarm swarm() {
118118
this
119119
);
120120
}
121+
122+
@Override
123+
public HttpClient httpClient() {
124+
return this.client;
125+
}
121126
}

src/test/java/com/amihaiemil/docker/LocalDockerTestCase.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import com.amihaiemil.docker.mock.AssertRequest;
2929
import com.amihaiemil.docker.mock.Response;
30+
import org.apache.http.client.HttpClient;
3031
import org.hamcrest.MatcherAssert;
3132
import org.hamcrest.Matchers;
3233
import org.junit.Test;
@@ -126,4 +127,21 @@ public void returnsImages() {
126127
Matchers.notNullValue()
127128
);
128129
}
130+
131+
/**
132+
* LocalDocker can return its HttpClient.
133+
*/
134+
@Test
135+
public void returnsHttpClient() {
136+
MatcherAssert.assertThat(
137+
new LocalDocker(
138+
new File("/var/run/docker.sock")
139+
).httpClient(),
140+
Matchers.allOf(
141+
Matchers.notNullValue(),
142+
Matchers.instanceOf(HttpClient.class),
143+
Matchers.instanceOf(UnixHttpClient.class)
144+
)
145+
);
146+
}
129147
}

src/test/java/com/amihaiemil/docker/RemoteDockerTestCase.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import com.amihaiemil.docker.mock.AssertRequest;
2929
import com.amihaiemil.docker.mock.Response;
30+
3031
import java.net.URI;
3132
import org.apache.http.HttpStatus;
3233
import org.apache.http.client.HttpClient;
@@ -121,4 +122,23 @@ public void returnsImages() {
121122
Matchers.notNullValue()
122123
);
123124
}
125+
126+
/**
127+
* LocalDocker can return its HttpClient.
128+
*/
129+
@Test
130+
public void returnsHttpClient() {
131+
final HttpClient client = Mockito.mock(HttpClient.class);
132+
MatcherAssert.assertThat(
133+
new RemoteDocker(
134+
client,
135+
URI.create("http://localhost")
136+
).httpClient(),
137+
Matchers.allOf(
138+
Matchers.notNullValue(),
139+
Matchers.instanceOf(HttpClient.class),
140+
Matchers.sameInstance(client)
141+
)
142+
);
143+
}
124144
}

0 commit comments

Comments
 (0)