Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class WireMockContainerJUnit5Test {
public void helloWorld() throws Exception {
final HttpClient client = HttpClient.newBuilder().build();
final HttpRequest request = HttpRequest.newBuilder()
.uri(wiremockServer.getRequestURI("hello"))
.uri(URI.create(wiremockServer.getUrl("/hello")))
.timeout(Duration.ofSeconds(10))
.header("Content-Type", "application/json")
.GET().build();
Expand All @@ -82,6 +82,7 @@ import org.wiremock.integrations.testcontainers.WireMockContainer;
import org.junit.Rule;
import org.junit.Test;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
Expand All @@ -97,7 +98,7 @@ public class WireMockContainerTest {
public void helloWorld() throws Exception {
final HttpClient client = HttpClient.newBuilder().build();
final HttpRequest request = HttpRequest.newBuilder()
.uri(wiremockServer.getRequestURI("hello"))
.uri(URI.create(wiremockServer.getUrl("/hello")))
.timeout(Duration.ofSeconds(10))
.header("Content-Type", "application/json")
.GET().build();
Expand Down Expand Up @@ -199,7 +200,7 @@ public class WireMockContainerExtensionJUnit5Test {
public void testJSONBodyTransformer() throws Exception {
final HttpClient client = HttpClient.newBuilder().build();
final HttpRequest request = HttpRequest.newBuilder()
.uri(wiremockServer.getRequestURI("json-body-transformer"))
.uri(URI.create(wiremockServer.getUrl("/json-body-transformer")))
.timeout(Duration.ofSeconds(10))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString("{\"name\":\"John Doe\"}")).build();
Expand All @@ -226,7 +227,7 @@ public class WireMockContainerExtensionTest {
public void testJSONBodyTransformer() throws Exception {
final HttpClient client = HttpClient.newBuilder().build();
final HttpRequest request = HttpRequest.newBuilder()
.uri(wiremockServer.getRequestURI("json-body-transformer"))
.uri(URI.create(wiremockServer.getUrl("/json-body-transformer")))
.timeout(Duration.ofSeconds(10))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString("{\"name\":\"John Doe\"}")).build();
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -184,15 +182,18 @@ public WireMockContainer withExtension(String id, String className) {
return withExtension(id, Collections.singleton(className), Collections.emptyList());
}

public String getEndpoint() {
return String.format("http://%s:%d", getHost(), getMappedPort(PORT));
public String getBaseUrl() {
return String.format("http://%s:%d", getHost(), getPort());
}

public URI getRequestURI(String relativePath) throws URISyntaxException {
return new URI(getEndpoint() + "/" + relativePath);
public String getUrl(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
return String.format("%s%s", getBaseUrl(), path);
}

public Integer getServerPort() {
public Integer getPort() {
return getMappedPort(PORT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.output.Slf4jLogConsumer;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
Expand Down Expand Up @@ -56,7 +57,7 @@ public void before() {
public void testJSONBodyTransformer() throws Exception {
final HttpClient client = HttpClient.newBuilder().build();
final HttpRequest request = HttpRequest.newBuilder()
.uri(wiremockServer.getRequestURI("json-body-transformer"))
.uri(URI.create(wiremockServer.getUrl("/json-body-transformer")))
.timeout(Duration.ofSeconds(10))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString("{\"name\":\"John Doe\"}")).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.output.Slf4jLogConsumer;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
Expand Down Expand Up @@ -58,7 +59,7 @@ public void before() {
public void testJSONBodyTransformer() throws Exception {
final HttpClient client = HttpClient.newBuilder().build();
final HttpRequest request = HttpRequest.newBuilder()
.uri(wiremockServer.getRequestURI("json-body-transformer"))
.uri(URI.create(wiremockServer.getUrl("/json-body-transformer")))
.timeout(Duration.ofSeconds(10))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString("{\"name\":\"John Doe\"}")).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

Expand All @@ -20,11 +23,16 @@ public class WireMockContainerJUnit5Test {
.withFileFromResource("hello-world-resource-response.xml", WireMockContainerTest.class,
"hello-world-resource-response.xml");

@Test
public void helloWorld() throws Exception {

@ParameterizedTest
@ValueSource(strings = {
"hello",
"/hello"
})
public void helloWorld(String path) throws Exception {
final HttpClient client = HttpClient.newBuilder().build();
final HttpRequest request = HttpRequest.newBuilder()
.uri(wiremockServer.getRequestURI("hello"))
.uri(URI.create(wiremockServer.getUrl(path)))
.timeout(Duration.ofSeconds(10))
.header("Content-Type", "application/json")
.GET().build();
Expand All @@ -43,7 +51,7 @@ public void helloWorldFromFile() throws Exception {
.build();

HttpRequest request = HttpRequest.newBuilder()
.uri(wiremockServer.getRequestURI("hello-from-file"))
.uri(URI.create(wiremockServer.getUrl("/hello-from-file")))
.timeout(Duration.ofSeconds(10))
.header("Content-Type", "application/json")
.GET()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.junit.Rule;
import org.junit.Test;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
Expand All @@ -37,7 +38,23 @@ public class WireMockContainerTest {
public void helloWorld() throws Exception {
final HttpClient client = HttpClient.newBuilder().build();
final HttpRequest request = HttpRequest.newBuilder()
.uri(wiremockServer.getRequestURI("hello"))
.uri(URI.create(wiremockServer.getUrl("/hello")))
.timeout(Duration.ofSeconds(10))
.header("Content-Type", "application/json")
.GET().build();

final HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

assertThat(response.body())
.as("Wrong response body")
.contains("Hello, world!");
}

@Test
public void helloWorldWithoutLeadingSlashInPath() throws Exception {
final HttpClient client = HttpClient.newBuilder().build();
final HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(wiremockServer.getUrl("hello")))
.timeout(Duration.ofSeconds(10))
.header("Content-Type", "application/json")
.GET().build();
Expand All @@ -53,7 +70,7 @@ public void helloWorld() throws Exception {
public void helloWorldFromFile() throws Exception {
final HttpClient client = HttpClient.newBuilder().build();
final HttpRequest request = HttpRequest.newBuilder()
.uri(wiremockServer.getRequestURI("hello-from-file"))
.uri(URI.create(wiremockServer.getUrl("/hello-from-file")))
.timeout(Duration.ofSeconds(10))
.header("Content-Type", "application/json")
.GET().build();
Expand Down