Skip to content

Commit

Permalink
Fixed wiremock#1197 - response templates not rendered when using json…
Browse files Browse the repository at this point in the history
…Body
  • Loading branch information
tomakehurst committed Oct 3, 2019
1 parent f278364 commit 4c052a3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public ResponseDefinitionBuilder withBody(byte[] body) {
return this;
}

public ResponseDefinitionBuilder withJsonBody(JsonNode jsonBody) {
this.jsonBody = jsonBody;
return this;
}

public ResponseDefinitionBuilder withFixedDelay(Integer milliseconds) {
this.fixedDelayMilliseconds = milliseconds;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.github.tomakehurst.wiremock.extension.responsetemplating;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Helper;
import com.github.jknack.handlebars.helper.AssignHelper;
Expand All @@ -24,6 +25,7 @@
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
import com.github.tomakehurst.wiremock.common.Exceptions;
import com.github.tomakehurst.wiremock.common.FileSource;
import com.github.tomakehurst.wiremock.common.Json;
import com.github.tomakehurst.wiremock.common.TextFile;
import com.github.tomakehurst.wiremock.extension.Parameters;
import com.github.tomakehurst.wiremock.extension.ResponseDefinitionTransformer;
Expand Down Expand Up @@ -141,8 +143,9 @@ public ResponseDefinition transform(Request request, final ResponseDefinition re
.build();

if (responseDefinition.specifiesTextBodyContent()) {
boolean isJsonBody = responseDefinition.getJsonBody() != null;
HandlebarsOptimizedTemplate bodyTemplate = getTemplate(TemplateCacheKey.forInlineBody(responseDefinition), responseDefinition.getTextBody());
applyTemplatedResponseBody(newResponseDefBuilder, model, bodyTemplate);
applyTemplatedResponseBody(newResponseDefBuilder, model, bodyTemplate, isJsonBody);
} else if (responseDefinition.specifiesBodyFile()) {
HandlebarsOptimizedTemplate filePathTemplate = new HandlebarsOptimizedTemplate(handlebars, responseDefinition.getBodyFileName());
String compiledFilePath = uncheckedApplyTemplate(filePathTemplate, model);
Expand All @@ -154,7 +157,7 @@ public ResponseDefinition transform(Request request, final ResponseDefinition re
TextFile file = files.getTextFileNamed(compiledFilePath);
HandlebarsOptimizedTemplate bodyTemplate = getTemplate(
TemplateCacheKey.forFileBody(responseDefinition, compiledFilePath), file.readContentsAsString());
applyTemplatedResponseBody(newResponseDefBuilder, model, bodyTemplate);
applyTemplatedResponseBody(newResponseDefBuilder, model, bodyTemplate, false);
}
}

Expand Down Expand Up @@ -191,9 +194,14 @@ protected Map<String, Object> addExtraModelElements(Request request, ResponseDef
return Collections.emptyMap();
}

private void applyTemplatedResponseBody(ResponseDefinitionBuilder newResponseDefBuilder, ImmutableMap<String, Object> model, HandlebarsOptimizedTemplate bodyTemplate) {
private void applyTemplatedResponseBody(ResponseDefinitionBuilder newResponseDefBuilder, ImmutableMap<String, Object> model, HandlebarsOptimizedTemplate bodyTemplate, boolean isJsonBody) {
String newBody = uncheckedApplyTemplate(bodyTemplate, model);
newResponseDefBuilder.withBody(newBody);
if (isJsonBody) {
newResponseDefBuilder.withJsonBody(Json.read(newBody, JsonNode.class));
} else {
newResponseDefBuilder.withBody(newBody);
}

}

private String uncheckedApplyTemplate(HandlebarsOptimizedTemplate template, Object context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.testsupport.WireMatchers;
import com.github.tomakehurst.wiremock.testsupport.WireMockResponse;
import com.github.tomakehurst.wiremock.testsupport.WireMockTestClient;
import org.junit.Before;
Expand Down Expand Up @@ -207,6 +208,32 @@ public void supportsJsonBodiesWithTemplating() {
assertThat(
response.content(),
response.statusCode(), is(200));

assertThat(response.content(), WireMatchers.equalToJson("{ \"modified\": \"3\" }"));
}

@Test
public void jsonBodyTemplatesCanSpecifyRequestAttributes() {
String stubJson = "{\n" +
" \"request\": {\n" +
" \"method\": \"GET\",\n" +
" \"urlPath\": \"/jsonBody/template\"\n" +
" },\n" +
" \"response\": {\n" +
" \"jsonBody\": {\n" +
" \"Key\": \"Hello world {{request.query.qp}}!\"\n" +
" },\n" +
" \"status\": 200,\n" +
" \"transformers\": [\n" +
" \"response-template\"\n" +
" ]\n" +
" }\n" +
"}";
client.postJson("/__admin/mappings", stubJson);

WireMockResponse response = client.get("/jsonBody/template?qp=2");

assertThat(response.content(), is("{\"Key\":\"Hello world 2!\"}"));
}

}
Expand Down

0 comments on commit 4c052a3

Please sign in to comment.