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

feat: enable global response templating for WireMock 3 #52

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
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,12 @@ public interface WireMockServerConfig {
*/
@WithDefault("src/test/resources")
String filesMapping();

/**
* If global response templating should be enabled for WireMock
*
* @see <a href="https://wiremock.org/3.x/docs/response-templating/">https://wiremock.org/3.x/docs/response-templating/</a>
*/
@WithDefault("false")
boolean globalResponseTemplating();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.jboss.logging.Logger;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;

import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.CuratedApplicationShutdownBuildItem;
Expand Down Expand Up @@ -54,8 +55,11 @@ DevServicesResultBuildItem setup(LaunchModeBuildItem launchMode, LiveReloadBuild
private static RunningDevService startWireMockDevService(WireMockServerConfig config) {

LOGGER.debugf("Starting WireMock server with port [%s] and path [%s]", config.port(), config.filesMapping());
final WireMockServer server = new WireMockServer(
options().port(config.port()).usingFilesUnderDirectory(config.filesMapping()));
final WireMockConfiguration configuration = options()
.port(config.port())
.usingFilesUnderDirectory(config.filesMapping())
.globalTemplating(config.globalResponseTemplating());
final WireMockServer server = new WireMockServer(configuration);
server.start();

return new RunningDevService(config.serviceName(), null, server::shutdown, getPropertyKey(PORT),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ void testWireMockMappingsFolder() {
.body(is("Everything was just fine!"));
}

@Test
void testTemplatingDisabled() {
final int port = ConfigProvider.getConfig().getValue(PREFIX + "." + PORT, Integer.class);
RestAssured.when().get(String.format("http://localhost:%d/template", port)).then().statusCode(Response.SC_OK)
.body(is("Everything was just fine from {{ request.port }}!"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.quarkiverse.wiremock.devservice;

import static io.quarkiverse.wiremock.devservice.WireMockDevServiceConfig.PORT;
import static io.quarkiverse.wiremock.devservice.WireMockDevServiceConfig.PREFIX;
import static org.hamcrest.Matchers.is;

import org.eclipse.jetty.server.Response;
import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

class WireMockTemplatingTest {

private static final String APP_PROPERTIES = "application-templating.properties";

@RegisterExtension
static final QuarkusUnitTest UNIT_TEST = new QuarkusUnitTest().withConfigurationResource(APP_PROPERTIES)
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class));

@Test
void testTemplatingEnabled() {
final int port = ConfigProvider.getConfig().getValue(PREFIX + "." + PORT, Integer.class);
RestAssured.when().get(String.format("http://localhost:%d/template", port)).then().statusCode(Response.SC_OK)
.body(is(String.format("Everything was just fine from %d!", port)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
%test.quarkus.wiremock.devservices.global-response-templating=true
10 changes: 10 additions & 0 deletions deployment/src/test/resources/mappings/template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"request": {
"method": "GET",
"url": "/template"
},
"response": {
"status": 200,
"body": "Everything was just fine from {{ request.port }}!"
}
}
18 changes: 17 additions & 1 deletion docs/modules/ROOT/pages/includes/quarkus-wiremock.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,21 @@ endif::add-copy-button-to-env-var[]
--|string
|`src/test/resources`

a|icon:lock[title=Fixed at build time] [[quarkus-wiremock_quarkus.wiremock.devservices.global-response-templating]]`link:#quarkus-wiremock_quarkus.wiremock.devservices.global-response-templating[quarkus.wiremock.devservices.global-response-templating]`

|===
[.description]
--
Response templating is enabled by default in WireMock 3,
with this setting response templating can be enabled globally,
see https://wiremock.org/3.x/docs/response-templating/[https://wiremock.org/3.x/docs/response-templating/]

ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_WIREMOCK_DEVSERVICES_GLOBAL_RESPONSE_TEMPLATING+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_WIREMOCK_DEVSERVICES_GLOBAL_RESPONSE_TEMPLATING+++`
endif::add-copy-button-to-env-var[]
--|boolean
|`false`

|===
3 changes: 2 additions & 1 deletion docs/modules/ROOT/pages/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Example (default) configuration for testing and local development:
%dev,test.quarkus.wiremock.devservices.files-mapping=src/test/resources
%dev,test.quarkus.wiremock.devservices.port=8089
%dev,test.quarkus.wiremock.devservices.service-name=wiremock-server
%dev,test.quarkus.wiremock.devservices.global-response-templating=false
----

== Testing
Expand Down Expand Up @@ -83,4 +84,4 @@ public class WireMockDevServiceResourceIT extends WireMockDevServiceResourceTest
[[extension-configuration-reference]]
== Extension Configuration Reference

include::includes/quarkus-wiremock.adoc[leveloffset=+1, opts=optional]
include::includes/quarkus-wiremock.adoc[leveloffset=+1, opts=optional]