Support for jsonBody and base64Body Fixes gh-1644 (#1645)

This commit is contained in:
akotynski
2021-04-22 10:04:23 +02:00
committed by GitHub
parent e4b7f65561
commit 006768c1e5
4 changed files with 53 additions and 6 deletions

View File

@@ -323,9 +323,9 @@ public final class WireMockRestServiceServer {
.contentType(contentType(response)).headers(responseHeaders(response));
}
private String body(ResponseDefinition response) {
if (response.getBody() != null) {
return response.getBody();
private byte[] body(ResponseDefinition response) {
if (response.getByteBody() != null) {
return response.getByteBody();
}
String file = response.getBodyFileName();
if (file != null) {
@@ -340,8 +340,7 @@ public final class WireMockRestServiceServer {
try {
Resource resource = files.createRelative(file);
if (resource.exists()) {
return StreamUtils.copyToString(resource.getInputStream(),
Charset.forName("UTF-8"));
return StreamUtils.copyToByteArray(resource.getInputStream());
}
}
catch (IOException e) {
@@ -355,7 +354,7 @@ public final class WireMockRestServiceServer {
}
}
}
return "";
return new byte[0];
}
private void requestHeaders(ResponseActions expect, RequestPattern request) {

View File

@@ -75,6 +75,30 @@ public class WiremockMockServerApplicationTests {
server.verify();
}
@Test
public void simpleGetWithJsonBody() throws Exception {
MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate)
.baseUrl("https://example.org")
.stubs("classpath:/mappings/resource-with-json-body.json").build();
String response = this.restTemplate.getForObject("https://example.org/resource", String.class);
assertThat(response).isEqualToIgnoringWhitespace("{\"message\":\"Hello World\"}");
server.verify();
}
@Test
public void simpleGetWithBase64Body() throws Exception {
MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate)
.baseUrl("https://example.org")
.stubs("classpath:/mappings/resource-with-base64-body.json").build();
String response = this.restTemplate.getForObject("https://example.org/resource", String.class);
assertThat(response).isEqualToIgnoringWhitespace("Hello World");
server.verify();
}
@Test
public void simpleGetWithBodyFileCustomLocation() throws Exception {
MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //

View File

@@ -0,0 +1,11 @@
{
"priority": 100,
"request": {
"urlPath": "/resource",
"method": "GET"
},
"response": {
"status": 200,
"base64Body": "SGVsbG8gV29ybGQ="
}
}

View File

@@ -0,0 +1,13 @@
{
"priority": 100,
"request": {
"urlPath": "/resource",
"method": "GET"
},
"response": {
"status": 200,
"jsonBody": {
"message": "Hello World"
}
}
}