Skip to content

Commit

Permalink
feat: enable global response templating (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
wjglerum authored Oct 18, 2023
1 parent d624f56 commit c6039cc
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 4 deletions.
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]

0 comments on commit c6039cc

Please sign in to comment.