Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable WireMock banner in the CLI output by default #42

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,23 @@
*/
package org.wiremock.integrations.testcontainers;

import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.containers.wait.strategy.WaitStrategy;
import org.testcontainers.images.builder.Transferable;
import org.testcontainers.shaded.com.google.common.io.Resources;
import org.testcontainers.utility.MountableFile;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
julian-michelmann marked this conversation as resolved.
Show resolved Hide resolved
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.containers.wait.strategy.WaitStrategy;
import org.testcontainers.images.builder.Transferable;
import org.testcontainers.shaded.com.google.common.io.Resources;
import org.testcontainers.utility.MountableFile;

/**
* Provisions WireMock standalone server as a container.
* Designed to follow the WireMock Docker image ({@code wiremock/wiremock}) structure and configuration,
Expand All @@ -50,19 +45,16 @@ public class WireMockContainer extends GenericContainer<WireMockContainer> {
private static final String FILES_DIR = "/home/wiremock/__files/";

private static final String EXTENSIONS_DIR = "/var/wiremock/extensions/";

private static final WaitStrategy DEFAULT_WAITER = Wait
.forHttp("/__admin/mappings")
.withMethod("GET")
.forStatusCode(200);

private static final int PORT = 8080;

private final StringBuilder wireMockArgs;

private final Map<String, Stub> mappingStubs = new HashMap<>();
private final Map<String, MountableFile> mappingFiles = new HashMap<>();
private final Map<String, Extension> extensions = new HashMap<>();
private boolean isBannerDisabled = true;

public WireMockContainer() {
this(DEFAULT_TAG);
Expand All @@ -85,6 +77,9 @@ public WireMockContainer(String image, String version) {
*/
public WireMockContainer withCliArg(String arg) {
//TODO: Switch to framework with proper CLI escaping
if (arg.contains("--verbose")) {
julian-michelmann marked this conversation as resolved.
Show resolved Hide resolved
isBannerDisabled = false;
}
wireMockArgs.append(' ').append(arg);
return this;
}
Expand Down Expand Up @@ -223,6 +218,10 @@ protected void configure() {
wireMockArgs.append(String.join(",", extensionClassNames));
}

if(isBannerDisabled) {
julian-michelmann marked this conversation as resolved.
Show resolved Hide resolved
this.withCliArg("--disable-banner");
}

// Add CLI arguments
withCommand(wireMockArgs.toString());
}
Expand All @@ -231,7 +230,7 @@ private static final class Stub {
final String name;
final String json;

public Stub (String name, String json) {
public Stub(String name, String json) {
this.name = name;
this.json = json;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,55 +1,83 @@
package org.wiremock.integrations.testcontainers;

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

import java.net.http.HttpResponse;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mockito;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpClient;

import java.net.http.HttpResponse;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@Testcontainers
public class WireMockContainerJUnit5Test {

@Container
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
.withMapping("hello", WireMockContainerTest.class, "hello-world.json")
.withMapping("hello-resource", WireMockContainerTest.class, "hello-world-resource.json")
.withFileFromResource("hello-world-resource-response.xml", WireMockContainerTest.class,
"hello-world-resource-response.xml");


@ParameterizedTest
@ValueSource(strings = {
"hello",
"/hello"
})
public void helloWorld(String path) throws Exception {
// given
String url = wiremockServer.getUrl(path);

// when
HttpResponse<String> response = TestHttpClient.newInstance().get(url);

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

@Test
public void helloWorldFromFile() throws Exception {
// given
String url = wiremockServer.getUrl("/hello-from-file");

// when
HttpResponse<String> response = TestHttpClient.newInstance().get(url);

// then
assertThat(response.body())
.as("Wrong response body")
.contains("Hello, world!");
}
@Container
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
.withMapping("hello", WireMockContainerTest.class, "hello-world.json")
.withMapping("hello-resource", WireMockContainerTest.class, "hello-world-resource.json")
.withFileFromResource("hello-world-resource-response.xml", WireMockContainerTest.class,
"hello-world-resource-response.xml");


@ParameterizedTest
@ValueSource(strings = {
"hello",
"/hello"
})
public void helloWorld(String path) throws Exception {
// given
String url = wiremockServer.getUrl(path);

// when
HttpResponse<String> response = TestHttpClient.newInstance().get(url);

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

@Test
public void helloWorldFromFile() throws Exception {
// given
String url = wiremockServer.getUrl("/hello-from-file");

// when
HttpResponse<String> response = TestHttpClient.newInstance().get(url);

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

@Test
public void defaultBannerDisabled() {
julian-michelmann marked this conversation as resolved.
Show resolved Hide resolved
WireMockContainer wireMockContainerSpy = Mockito.spy(new WireMockContainer("2.35.0"));

wireMockContainerSpy.configure();

verify(wireMockContainerSpy).withCliArg("--disable-banner");
}

@ParameterizedTest
@ValueSource(strings = {
"--verbose",
" --verbose ",
"--verbose --help"
})
public void showBannerWhenStartingVerbose(String verboseArg) {
WireMockContainer wireMockContainerSpy = Mockito.spy(new WireMockContainer("2.35.0"));
wireMockContainerSpy.withCliArg(verboseArg);

wireMockContainerSpy.configure();

verify(wireMockContainerSpy, times(0)).withCliArg("--disable-banner");
}
julian-michelmann marked this conversation as resolved.
Show resolved Hide resolved
}