Support for jsonBody and base64Body Fixes gh-1644 (#1645)
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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) //
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"priority": 100,
|
||||
"request": {
|
||||
"urlPath": "/resource",
|
||||
"method": "GET"
|
||||
},
|
||||
"response": {
|
||||
"status": 200,
|
||||
"base64Body": "SGVsbG8gV29ybGQ="
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"priority": 100,
|
||||
"request": {
|
||||
"urlPath": "/resource",
|
||||
"method": "GET"
|
||||
},
|
||||
"response": {
|
||||
"status": 200,
|
||||
"jsonBody": {
|
||||
"message": "Hello World"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user