File tree Expand file tree Collapse file tree 6 files changed +136
-1
lines changed
main/java/org/wiremock/spring
resources/response-templating/mappings Expand file tree Collapse file tree 6 files changed +136
-1
lines changed Original file line number Diff line number Diff line change 132132 * be {@link Autowired} by name.
133133 */
134134 boolean registerSpringBean () default false ;
135+
136+ /**
137+ * @see WireMockConfiguration#globalTemplating(boolean)
138+ */
139+ boolean globalTemplating () default false ;
135140}
Original file line number Diff line number Diff line change 1111import java .util .List ;
1212import java .util .Optional ;
1313import java .util .stream .Collectors ;
14+ import java .util .stream .Stream ;
1415import org .junit .platform .commons .util .ReflectionUtils ;
1516import org .junit .platform .commons .util .StringUtils ;
1617import org .slf4j .Logger ;
@@ -82,6 +83,8 @@ public WireMockServer createWireMockServer(
8283 serverOptions .extensions (options .extensions ());
8384 }
8485
86+ serverOptions .globalTemplating (options .globalTemplating ());
87+
8588 this .applyCustomizers (options , serverOptions );
8689
8790 this .logger .info (
@@ -251,7 +254,7 @@ private Optional<String> configureFilesUnderClasspath(
251254 private Optional <String > configureFilesUnderDirectory (
252255 final String [] filesUnderDirectory , final String suffix ) {
253256 final List <String > alternatives =
254- List .of (filesUnderDirectory ). stream ( )
257+ Stream .of (filesUnderDirectory )
255258 .map (it -> it + suffix )
256259 .filter (
257260 it -> {
Original file line number Diff line number Diff line change 1+ package usecases ;
2+
3+ import static org .assertj .core .api .Assertions .assertThat ;
4+
5+ import io .restassured .RestAssured ;
6+ import org .junit .jupiter .api .Test ;
7+ import org .springframework .beans .factory .annotation .Value ;
8+ import org .springframework .boot .test .context .SpringBootTest ;
9+ import org .wiremock .spring .ConfigureWireMock ;
10+ import org .wiremock .spring .EnableWireMock ;
11+
12+ @ SpringBootTest
13+ @ EnableWireMock ({
14+ @ ConfigureWireMock (filesUnderClasspath = "response-templating" , globalTemplating = true )
15+ })
16+ class ResponseTemplatingGlobalTest {
17+
18+ @ Value ("${wiremock.server.baseUrl}" )
19+ private String wireMockServerUrl ;
20+
21+ @ Test
22+ void testLocal () {
23+ RestAssured .baseURI = this .wireMockServerUrl ;
24+ final String actual =
25+ RestAssured .when ().get ("/local-templating" ).then ().extract ().asPrettyString ();
26+ assertThat (actual )
27+ .isEqualToIgnoringWhitespace (
28+ """
29+ {
30+ "name": "Resolved: local-templating"
31+ }
32+ """ );
33+ }
34+
35+ @ Test
36+ void testGlobal () {
37+ RestAssured .baseURI = this .wireMockServerUrl ;
38+ final String actual =
39+ RestAssured .when ().get ("/global-templating" ).then ().extract ().asPrettyString ();
40+ assertThat (actual )
41+ .isEqualToIgnoringWhitespace (
42+ """
43+ {
44+ "name": "Resolved: global-templating"
45+ }
46+ """ );
47+ }
48+ }
Original file line number Diff line number Diff line change 1+ package usecases ;
2+
3+ import static org .assertj .core .api .Assertions .assertThat ;
4+
5+ import io .restassured .RestAssured ;
6+ import org .junit .jupiter .api .Test ;
7+ import org .springframework .beans .factory .annotation .Value ;
8+ import org .springframework .boot .test .context .SpringBootTest ;
9+ import org .wiremock .spring .ConfigureWireMock ;
10+ import org .wiremock .spring .EnableWireMock ;
11+
12+ @ SpringBootTest
13+ @ EnableWireMock ({
14+ @ ConfigureWireMock (filesUnderClasspath = "response-templating" , globalTemplating = false )
15+ })
16+ class ResponseTemplatingLocalTest {
17+
18+ @ Value ("${wiremock.server.baseUrl}" )
19+ private String wireMockServerUrl ;
20+
21+ @ Test
22+ void testLocal () {
23+ RestAssured .baseURI = this .wireMockServerUrl ;
24+ final String actual =
25+ RestAssured .when ().get ("/local-templating" ).then ().extract ().asPrettyString ();
26+ assertThat (actual )
27+ .isEqualToIgnoringWhitespace ("""
28+ {
29+ "name": "Resolved: local-templating"
30+ }
31+ """ );
32+ }
33+
34+ @ Test
35+ void testGlobal () {
36+ RestAssured .baseURI = this .wireMockServerUrl ;
37+ final String actual =
38+ RestAssured .when ().get ("/global-templating" ).then ().extract ().asPrettyString ();
39+ assertThat (actual )
40+ .isEqualToIgnoringWhitespace ("""
41+ {
42+ "name": "Resolved: {{request.path.[0]}}"
43+ }
44+ """ );
45+ }
46+ }
Original file line number Diff line number Diff line change 1+ {
2+ "request" : {
3+ "method" : " GET" ,
4+ "url" : " /global-templating"
5+ },
6+ "response" : {
7+ "headers" : {
8+ "Content-Type" : " application/json"
9+ },
10+ "jsonBody" : {
11+ "name" : " Resolved: {{request.path.[0]}}"
12+ },
13+ "status" : 200
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ {
2+ "request" : {
3+ "method" : " GET" ,
4+ "url" : " /local-templating"
5+ },
6+ "response" : {
7+ "headers" : {
8+ "Content-Type" : " application/json"
9+ },
10+ "jsonBody" : {
11+ "name" : " Resolved: {{request.path.[0]}}"
12+ },
13+ "transformers" : [
14+ " response-template"
15+ ],
16+ "status" : 200
17+ }
18+ }
You can’t perform that action at this time.
0 commit comments