Skip to content

Commit 3c07afc

Browse files
authored
Merge pull request #1580 from epabst/SPR-16132
Move requests.add(request) into finally block.
2 parents 4978749 + 43d88e4 commit 3c07afc

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

spring-test/src/main/java/org/springframework/test/web/client/AbstractRequestExpectationManager.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,12 @@ public ClientHttpResponse validateRequest(ClientHttpRequest request) throws IOEx
8080
if (requests.isEmpty()) {
8181
afterExpectationsDeclared();
8282
}
83-
ClientHttpResponse response = validateRequestInternal(request);
84-
requests.add(request);
85-
return response;
83+
try {
84+
return validateRequestInternal(request);
85+
}
86+
finally {
87+
requests.add(request);
88+
}
8689
}
8790
}
8891

spring-test/src/test/java/org/springframework/test/web/client/SimpleRequestExpectationManagerTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.test.web.client;
1818

19+
import java.net.SocketException;
1920
import java.net.URI;
2021
import java.net.URISyntaxException;
2122

@@ -30,6 +31,7 @@
3031
import static org.junit.Assert.assertEquals;
3132
import static org.springframework.http.HttpMethod.GET;
3233
import static org.springframework.http.HttpMethod.POST;
34+
import static org.springframework.test.util.AssertionErrors.fail;
3335
import static org.springframework.test.web.client.ExpectedCount.max;
3436
import static org.springframework.test.web.client.ExpectedCount.min;
3537
import static org.springframework.test.web.client.ExpectedCount.once;
@@ -184,6 +186,23 @@ public void repeatedRequestsInSequentialOrder() throws Exception {
184186
this.manager.validateRequest(createRequest(GET, "/bar"));
185187
}
186188

189+
@Test // SPR-16132
190+
public void sequentialRequestsWithFirstFailing() throws Exception {
191+
this.manager.expectRequest(once(), requestTo("/foo")).andExpect(method(GET))
192+
.andRespond(request -> { throw new SocketException("pseudo network error"); });
193+
this.manager.expectRequest(once(), requestTo("/handle-error")).andExpect(method(POST)).andRespond(withSuccess());
194+
195+
try {
196+
this.manager.validateRequest(createRequest(GET, "/foo"));
197+
fail("expected exception");
198+
}
199+
catch (SocketException e) {
200+
//expected
201+
}
202+
this.manager.validateRequest(createRequest(POST, "/handle-error"));
203+
this.manager.verify();
204+
}
205+
187206

188207
private ClientHttpRequest createRequest(HttpMethod method, String url) {
189208
try {

0 commit comments

Comments
 (0)