Skip to content

Commit

Permalink
feat: global response templating configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Feb 7, 2025
1 parent 1b42d76 commit 21b06a8
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/main/java/org/wiremock/spring/ConfigureWireMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,9 @@
* be {@link Autowired} by name.
*/
boolean registerSpringBean() default false;

/**
* @see WireMockConfiguration#globalTemplating(boolean)
*/
boolean globalTemplating() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.platform.commons.util.ReflectionUtils;
import org.junit.platform.commons.util.StringUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -82,6 +83,8 @@ public WireMockServer createWireMockServer(
serverOptions.extensions(options.extensions());
}

serverOptions.globalTemplating(options.globalTemplating());

this.applyCustomizers(options, serverOptions);

this.logger.info(
Expand Down Expand Up @@ -251,7 +254,7 @@ private Optional<String> configureFilesUnderClasspath(
private Optional<String> configureFilesUnderDirectory(
final String[] filesUnderDirectory, final String suffix) {
final List<String> alternatives =
List.of(filesUnderDirectory).stream()
Stream.of(filesUnderDirectory)
.map(it -> it + suffix)
.filter(
it -> {
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/usecases/ResponseTemplatingGlobalTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package usecases;

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

import io.restassured.RestAssured;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.wiremock.spring.ConfigureWireMock;
import org.wiremock.spring.EnableWireMock;

@SpringBootTest
@EnableWireMock({
@ConfigureWireMock(filesUnderClasspath = "response-templating", globalTemplating = true)
})
class ResponseTemplatingGlobalTest {

@Value("${wiremock.server.baseUrl}")
private String wireMockServerUrl;

@Test
void testLocal() {
RestAssured.baseURI = this.wireMockServerUrl;
final String actual =
RestAssured.when().get("/local-templating").then().extract().asPrettyString();
assertThat(actual)
.isEqualToIgnoringWhitespace(
"""
{
"name": "Resolved: local-templating"
}
""");
}

@Test
void testGlobal() {
RestAssured.baseURI = this.wireMockServerUrl;
final String actual =
RestAssured.when().get("/global-templating").then().extract().asPrettyString();
assertThat(actual)
.isEqualToIgnoringWhitespace(
"""
{
"name": "Resolved: global-templating"
}
""");
}
}
46 changes: 46 additions & 0 deletions src/test/java/usecases/ResponseTemplatingLocalTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package usecases;

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

import io.restassured.RestAssured;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.wiremock.spring.ConfigureWireMock;
import org.wiremock.spring.EnableWireMock;

@SpringBootTest
@EnableWireMock({
@ConfigureWireMock(filesUnderClasspath = "response-templating", globalTemplating = false)
})
class ResponseTemplatingLocalTest {

@Value("${wiremock.server.baseUrl}")
private String wireMockServerUrl;

@Test
void testLocal() {
RestAssured.baseURI = this.wireMockServerUrl;
final String actual =
RestAssured.when().get("/local-templating").then().extract().asPrettyString();
assertThat(actual)
.isEqualToIgnoringWhitespace("""
{
"name": "Resolved: local-templating"
}
""");
}

@Test
void testGlobal() {
RestAssured.baseURI = this.wireMockServerUrl;
final String actual =
RestAssured.when().get("/global-templating").then().extract().asPrettyString();
assertThat(actual)
.isEqualToIgnoringWhitespace("""
{
"name": "Resolved: {{request.path.[0]}}"
}
""");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"request": {
"method": "GET",
"url": "/global-templating"
},
"response": {
"headers": {
"Content-Type": "application/json"
},
"jsonBody": {
"name": "Resolved: {{request.path.[0]}}"
},
"status": 200
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"request": {
"method": "GET",
"url": "/local-templating"
},
"response": {
"headers": {
"Content-Type": "application/json"
},
"jsonBody": {
"name": "Resolved: {{request.path.[0]}}"
},
"transformers": [
"response-template"
],
"status": 200
}
}

0 comments on commit 21b06a8

Please sign in to comment.