Skip to content

Commit

Permalink
Docker.httpClient() + unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Nov 3, 2018
1 parent 6667347 commit 88a1d05
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/java/com/amihaiemil/docker/Docker.java
Expand Up @@ -25,6 +25,8 @@
*/
package com.amihaiemil.docker;

import org.apache.http.client.HttpClient;

import java.io.IOException;

/**
Expand Down Expand Up @@ -79,4 +81,19 @@ public interface Docker {
* @return Swarm.
*/
Swarm swarm();

/**
* The underlying, immutable, Apache HttpClient.<br><br>
*
* Use this method to fetch the underlying HttpClient and perform your own
* HTTP requests, in case the API is missing the desired method.<br><br>
*
* Usage in any other scenario is discouraged. Try to find the desired
* method before using this. Also, if the method is missing, open an
* Issue at https://www.github.com/amihaiemil/docker-java-api, maybe
* it can be implemented and released quickly.
*
* @return The underlying HttpClient.
*/
HttpClient httpClient();
}
5 changes: 5 additions & 0 deletions src/main/java/com/amihaiemil/docker/RtDocker.java
Expand Up @@ -118,4 +118,9 @@ public final Swarm swarm() {
this
);
}

@Override
public HttpClient httpClient() {
return this.client;
}
}
18 changes: 18 additions & 0 deletions src/test/java/com/amihaiemil/docker/LocalDockerTestCase.java
Expand Up @@ -27,6 +27,7 @@

import com.amihaiemil.docker.mock.AssertRequest;
import com.amihaiemil.docker.mock.Response;
import org.apache.http.client.HttpClient;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
Expand Down Expand Up @@ -126,4 +127,21 @@ public void returnsImages() {
Matchers.notNullValue()
);
}

/**
* LocalDocker can return its HttpClient.
*/
@Test
public void returnsHttpClient() {
MatcherAssert.assertThat(
new LocalDocker(
new File("/var/run/docker.sock")
).httpClient(),
Matchers.allOf(
Matchers.notNullValue(),
Matchers.instanceOf(HttpClient.class),
Matchers.instanceOf(UnixHttpClient.class)
)
);
}
}
20 changes: 20 additions & 0 deletions src/test/java/com/amihaiemil/docker/RemoteDockerTestCase.java
Expand Up @@ -27,6 +27,7 @@

import com.amihaiemil.docker.mock.AssertRequest;
import com.amihaiemil.docker.mock.Response;

import java.net.URI;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
Expand Down Expand Up @@ -121,4 +122,23 @@ public void returnsImages() {
Matchers.notNullValue()
);
}

/**
* LocalDocker can return its HttpClient.
*/
@Test
public void returnsHttpClient() {
final HttpClient client = Mockito.mock(HttpClient.class);
MatcherAssert.assertThat(
new RemoteDocker(
client,
URI.create("http://localhost")
).httpClient(),
Matchers.allOf(
Matchers.notNullValue(),
Matchers.instanceOf(HttpClient.class),
Matchers.sameInstance(client)
)
);
}
}

0 comments on commit 88a1d05

Please sign in to comment.