Skip to content

Commit

Permalink
Add bodyFile support for DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
tommysitu committed Jul 12, 2024
1 parent 1395cb8 commit 784de02
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
16 changes: 14 additions & 2 deletions src/main/java/io/specto/hoverfly/junit/core/model/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, List<String>> headers;
Expand All @@ -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<String, List<String>> headers,
Expand All @@ -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;
Expand All @@ -69,11 +72,14 @@ public String getBody() {
return body;
}

public String getBodyFile() {
return bodyFile;
}

public boolean isEncodedBody() {
return encodedBody;
}


public boolean isTemplated() {
return templated;
}
Expand Down Expand Up @@ -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<String, List<String>> headers;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
17 changes: 16 additions & 1 deletion src/main/java/io/specto/hoverfly/junit/dsl/ResponseBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class ResponseBuilder {

private final Map<String, List<String>> headers = new HashMap<>();
private String body = "";
private String bodyFile;
private boolean encodedBody = false;
private int status = 200;
private boolean templated = true;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 784de02

Please sign in to comment.