Skip to content

Commit a5ca762

Browse files
committed
Add fixed delay per request
1 parent 54d6329 commit a5ca762

File tree

8 files changed

+51
-19
lines changed

8 files changed

+51
-19
lines changed

.java-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
11.0

docs/pages/corefunctionality/dsl.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Per-request delay can be set as follows:
5555
SimulationSource.dsl(
5656
service("www.not-so-slow-service.com")
5757
.get("/api/bookings")
58-
.willReturn(success().withDelay(1, TimeUnit.SECONDS))
58+
.willReturn(success().withFixedDelay(1, TimeUnit.SECONDS))
5959
)
6060
)
6161
@@ -83,4 +83,4 @@ There is an implementation which lets you write inline JSON body efficiently wit
8383
.. code-block:: java
8484
8585
.body(jsonWithSingleQuotes("{'bookingId':'1'}"))
86-
.body(jsonWithSingleQuotes("{'merchantName':'Jame\\'s'}")) // escape single quote in your data if necessary
86+
.body(jsonWithSingleQuotes("{'merchantName':'Jame\\'s'}")) // escape single quote in your data if necessary

src/main/java/io/specto/hoverfly/junit/core/model/Response.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,30 @@ public class Response {
3333
private final Map<String, List<String>> headers;
3434
private final Map<String, String> transitionsState;
3535
private final List<String> removesState;
36+
private final Integer fixedDelay;
37+
3638

3739
@JsonCreator
38-
public Response(@JsonProperty("status") Integer status,
39-
@JsonProperty("body") String body,
40-
@JsonProperty("encodedBody") boolean encodedBody,
41-
@JsonProperty("templated") boolean templated,
42-
@JsonProperty("headers") Map<String, List<String>> headers,
43-
@JsonProperty("transitionsState") Map<String, String> transitionsState,
44-
@JsonProperty("removesState") List<String> removesState) {
40+
public Response(
41+
@JsonProperty("status") Integer status,
42+
@JsonProperty("body") String body,
43+
@JsonProperty("encodedBody") boolean encodedBody,
44+
@JsonProperty("templated") boolean templated,
45+
@JsonProperty("headers") Map<String, List<String>> headers,
46+
@JsonProperty("transitionsState") Map<String, String> transitionsState,
47+
@JsonProperty("removesState") List<String> removesState,
48+
@JsonProperty("fixedDelay") Integer fixedDelay) {
4549
this.status = status;
4650
this.body = body;
4751
this.encodedBody = encodedBody;
4852
this.templated = templated;
4953
this.headers = headers;
5054
this.transitionsState = transitionsState;
5155
this.removesState = removesState;
56+
this.fixedDelay = fixedDelay;
5257
}
5358

54-
public int getStatus() {
59+
public Integer getStatus() {
5560
return status;
5661
}
5762

@@ -80,6 +85,10 @@ public List<String> getRemovesState() {
8085
return removesState;
8186
}
8287

88+
public Integer getFixedDelay() {
89+
return fixedDelay;
90+
}
91+
8392
static class Builder {
8493
private Integer status;
8594
private String body;
@@ -88,6 +97,7 @@ static class Builder {
8897
private Map<String, List<String>> headers;
8998
private Map<String, String> transitionsState;
9099
private List<String> removesState;
100+
private Integer fixedDelay;
91101

92102
Builder status(int status) {
93103
this.status = status;
@@ -124,8 +134,13 @@ Builder removesState(List<String> removesState) {
124134
return this;
125135
}
126136

137+
Builder fixedDelay(int fixedDelay) {
138+
this.fixedDelay = fixedDelay;
139+
return this;
140+
}
141+
127142
Response build() {
128-
return new Response(status, body, encodedBody, templated, headers, transitionsState, removesState);
143+
return new Response(status, body, encodedBody, templated, headers, transitionsState, removesState, fixedDelay);
129144
}
130145
}
131146

@@ -143,4 +158,4 @@ public int hashCode() {
143158
public String toString() {
144159
return ToStringBuilder.reflectionToString(this);
145160
}
146-
}
161+
}

src/main/java/io/specto/hoverfly/junit/dsl/ResponseBuilder.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public class ResponseBuilder {
3636
private final Map<String, String> transitionsState = new HashMap<>();
3737
private final List<String> removesState = new ArrayList<>();
3838

39+
private int fixedDelay;
40+
private TimeUnit fixedDelayTimeUnit;
41+
42+
// Deprecated: For global delay settings
3943
private int delay;
4044
private TimeUnit delayTimeUnit;
4145

@@ -108,7 +112,8 @@ public ResponseBuilder andRemoveState(final String stateToRemove) {
108112
* @return the response
109113
*/
110114
Response build() {
111-
return new Response(status, body, false, templated, headers, transitionsState, removesState);
115+
int fixedDelayInMillis = fixedDelayTimeUnit == null ? 0 : (int) fixedDelayTimeUnit.toMillis(fixedDelay);
116+
return new Response(status, body, false, templated, headers, transitionsState, removesState, fixedDelayInMillis);
112117
}
113118

114119
public ResponseBuilder body(final HttpBodyConverter httpBodyConverter) {
@@ -123,12 +128,19 @@ public ResponseBuilder disableTemplating() {
123128
return this;
124129
}
125130

131+
public ResponseBuilder withFixedDelay(int delay, TimeUnit delayTimeUnit) {
132+
fixedDelay = delay;
133+
fixedDelayTimeUnit = delayTimeUnit;
134+
return this;
135+
}
136+
126137
/**
127138
* Sets delay parameters.
128139
* @param delay amount of delay
129140
* @param delayTimeUnit time unit of delay (e.g. SECONDS)
130141
* @return the {@link ResponseBuilder for further customizations}
131142
*/
143+
@Deprecated
132144
public ResponseBuilder withDelay(int delay, TimeUnit delayTimeUnit) {
133145
this.delay = delay;
134146
this.delayTimeUnit = delayTimeUnit;

src/test/java/io/specto/hoverfly/junit/core/model/SimulationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ private Simulation getLatestSimulation() {
8383
.requiresState(ImmutableMap.of("requiresStateKey", "requiresStateValue"));
8484
Response.Builder responseBuilder = getTestResponseBuilder()
8585
.transitionsState(ImmutableMap.of("transitionsStateKey", "transitionsStateValue"))
86-
.removesState(ImmutableList.of("removesStateKey"));
86+
.removesState(ImmutableList.of("removesStateKey"))
87+
.fixedDelay(3000);
8788
HoverflyData data = getTestHoverflyData(requestBuilder, responseBuilder);
8889
HoverflyMetaData meta = new HoverflyMetaData();
8990
return new Simulation(data, meta);

src/test/java/io/specto/hoverfly/ruletest/HoverflyDslWithDelayTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import static io.specto.hoverfly.junit.dsl.ResponseCreators.success;
1616
import static io.specto.hoverfly.junit.dsl.matchers.HoverflyMatchers.contains;
1717
import static io.specto.hoverfly.junit.dsl.matchers.HoverflyMatchers.endsWith;
18+
import static io.specto.hoverfly.junit.dsl.matchers.HoverflyMatchers.equalsTo;
1819
import static org.assertj.core.api.Assertions.assertThat;
1920

2021
public class HoverflyDslWithDelayTest {
@@ -39,10 +40,10 @@ public class HoverflyDslWithDelayTest {
3940

4041
.andDelay(3, TimeUnit.SECONDS).forMethod("POST"),
4142

42-
// Delay based on URL
43+
// Fixed delay for a particular request
4344
service("www.not-so-slow-service.com")
4445
.get("/api/bookings")
45-
.willReturn(success().withDelay(1, TimeUnit.SECONDS))
46+
.willReturn(success().withFixedDelay(1, TimeUnit.SECONDS))
4647

4748
)).printSimulationData();
4849

@@ -91,7 +92,7 @@ public void shouldBeAbleToDelayRequestByHttpMethod() {
9192
}
9293

9394
@Test
94-
public void shouldBeAbleToDelayRequest() {
95+
public void shouldBeAbleToAddFixedDelayPerRequest() {
9596

9697
// When
9798
StopWatch stopWatch = new StopWatch();

src/test/resources/simulations/v5-simulation-with-unknown-fields.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@
7575
},
7676
"removesState": [
7777
"removesStateKey"
78-
]
78+
],
79+
"fixedDelay": 3000
7980
}
8081
}
8182
],

src/test/resources/simulations/v5-simulation.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@
7474
},
7575
"removesState": [
7676
"removesStateKey"
77-
]
77+
],
78+
"fixedDelay": 3000
7879
}
7980
}
8081
],

0 commit comments

Comments
 (0)