Skip to content

feat: global response templating configurable #86

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

Merged
merged 1 commit into from
Feb 7, 2025
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
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
}
}