diff --git a/src/main/java/io/specto/hoverfly/junit/core/model/Response.java b/src/main/java/io/specto/hoverfly/junit/core/model/Response.java index eefee00d..866d6444 100644 --- a/src/main/java/io/specto/hoverfly/junit/core/model/Response.java +++ b/src/main/java/io/specto/hoverfly/junit/core/model/Response.java @@ -28,6 +28,7 @@ public class Response { private final Integer status; private final String body; + private final String bodyFile; private final boolean encodedBody; private final boolean templated; private final Map> headers; @@ -41,6 +42,7 @@ public class Response { public Response( @JsonProperty("status") Integer status, @JsonProperty("body") String body, + @JsonProperty("bodyFile") String bodyFile, @JsonProperty("encodedBody") boolean encodedBody, @JsonProperty("templated") boolean templated, @JsonProperty("headers") Map> headers, @@ -51,6 +53,7 @@ public Response( @JsonProperty("postServeAction") String postServeAction) { this.status = status; this.body = body; + this.bodyFile = bodyFile; this.encodedBody = encodedBody; this.templated = templated; this.headers = headers; @@ -69,11 +72,14 @@ public String getBody() { return body; } + public String getBodyFile() { + return bodyFile; + } + public boolean isEncodedBody() { return encodedBody; } - public boolean isTemplated() { return templated; } @@ -105,6 +111,7 @@ public String getPostServeAction() { static class Builder { private Integer status; private String body; + private String bodyFile; private boolean encodedBody; private boolean templated; private Map> headers; @@ -124,6 +131,11 @@ Builder body(String body) { return this; } + Builder bodyFile(String bodyFile) { + this.bodyFile = bodyFile; + return this; + } + Builder encodedBody(boolean encodedBody) { this.encodedBody = encodedBody; return this; @@ -165,7 +177,7 @@ Builder postServeAction(String postServeAction) { } Response build() { - return new Response(status, body, encodedBody, templated, headers, transitionsState, removesState, fixedDelay, logNormalDelay, postServeAction); + return new Response(status, body, bodyFile, encodedBody, templated, headers, transitionsState, removesState, fixedDelay, logNormalDelay, postServeAction); } } diff --git a/src/main/java/io/specto/hoverfly/junit/dsl/ResponseBuilder.java b/src/main/java/io/specto/hoverfly/junit/dsl/ResponseBuilder.java index 1b44d316..a5daf84c 100644 --- a/src/main/java/io/specto/hoverfly/junit/dsl/ResponseBuilder.java +++ b/src/main/java/io/specto/hoverfly/junit/dsl/ResponseBuilder.java @@ -32,6 +32,7 @@ public class ResponseBuilder { private final Map> headers = new HashMap<>(); private String body = ""; + private String bodyFile; private boolean encodedBody = false; private int status = 200; private boolean templated = true; @@ -67,6 +68,20 @@ public ResponseBuilder body(final String body) { return this; } + /** + * Sets the file which the response body returns. + * You may need to configure the path for Hoverfly to look up the file by using + * {@link io.specto.hoverfly.junit.core.HoverflyConfig#absoluteResponseBodyFilesPath(String)} or + * {@link io.specto.hoverfly.junit.core.HoverflyConfig#relativeResponseBodyFilesPath(String)} + * If both body and bodyFile are set, Hoverfly will log a warning and the bodyFile will be ignored. + * @param bodyFile the path of the file which contains the response body + * @return the {@link ResponseBuilder for further customizations} + */ + public ResponseBuilder bodyFile(final String bodyFile) { + this.bodyFile = bodyFile; + return this; + } + /** * Sets the status * @param status status of the response @@ -114,7 +129,7 @@ public ResponseBuilder andRemoveState(final String stateToRemove) { * @return the response */ Response build() { - return new Response(status, body, encodedBody, templated, headers, transitionsState, removesState, fixedDelay, logNormalDelay, null); + return new Response(status, body, bodyFile, encodedBody, templated, headers, transitionsState, removesState, fixedDelay, logNormalDelay, null); } public ResponseBuilder body(final HttpBodyConverter httpBodyConverter) { diff --git a/src/test/java/io/specto/hoverfly/junit/core/ResponseBodyFileTest.java b/src/test/java/io/specto/hoverfly/junit/core/ResponseBodyFileTest.java index 2f28506b..15563da5 100644 --- a/src/test/java/io/specto/hoverfly/junit/core/ResponseBodyFileTest.java +++ b/src/test/java/io/specto/hoverfly/junit/core/ResponseBodyFileTest.java @@ -1,6 +1,8 @@ package io.specto.hoverfly.junit.core; import static io.specto.hoverfly.junit.core.HoverflyMode.SIMULATE; +import static io.specto.hoverfly.junit.dsl.HoverflyDsl.service; +import static io.specto.hoverfly.junit.dsl.ResponseCreators.success; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -9,7 +11,9 @@ import java.net.URISyntaxException; import java.time.LocalDate; import org.junit.Test; +import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.http.RequestEntity; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; @@ -41,7 +45,21 @@ public void shouldResolveResponseBodyFilesInTestResourcesFolder() throws URISynt } @Test - public void shouldThrowExceptionIfRelativeFilesPathNotFound() throws URISyntaxException { + public void shouldWorksWithDsl() throws URISyntaxException { + + try (Hoverfly hoverfly = new Hoverfly(HoverflyConfig.localConfigs().relativeResponseBodyFilesPath("simulations"), SIMULATE)) { + hoverfly.start(); + hoverfly.simulate(SimulationSource.dsl( + service("www.my-test.com") + .get("/api/bookings/1") + .willReturn(success().header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).bodyFile("responses/booking-200.json")))); + + checkSimulationSuccessful(); + } + } + + @Test + public void shouldThrowExceptionIfRelativeFilesPathNotFound() { assertThatThrownBy(() -> new Hoverfly(HoverflyConfig.localConfigs().relativeResponseBodyFilesPath("blahblah"), SIMULATE)) .isInstanceOf(IllegalArgumentException.class)