SimpleRequestExpectationManager properly handles sequential requests with different count
Issue: SPR-15672
This commit is contained in:
@@ -39,6 +39,7 @@ import org.springframework.util.Assert;
|
||||
* to expectations following the order of declaration or not.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Juergen Hoeller
|
||||
* @since 4.3
|
||||
*/
|
||||
public abstract class AbstractRequestExpectationManager implements RequestExpectationManager {
|
||||
@@ -47,8 +48,6 @@ public abstract class AbstractRequestExpectationManager implements RequestExpect
|
||||
|
||||
private final List<ClientHttpRequest> requests = new LinkedList<>();
|
||||
|
||||
private final Object lock = new Object();
|
||||
|
||||
|
||||
protected List<RequestExpectation> getExpectations() {
|
||||
return this.expectations;
|
||||
@@ -69,12 +68,13 @@ public abstract class AbstractRequestExpectationManager implements RequestExpect
|
||||
|
||||
@Override
|
||||
public ClientHttpResponse validateRequest(ClientHttpRequest request) throws IOException {
|
||||
synchronized (this.lock) {
|
||||
if (getRequests().isEmpty()) {
|
||||
List<ClientHttpRequest> requests = getRequests();
|
||||
synchronized (requests) {
|
||||
if (requests.isEmpty()) {
|
||||
afterExpectationsDeclared();
|
||||
}
|
||||
ClientHttpResponse response = validateRequestInternal(request);
|
||||
getRequests().add(request);
|
||||
requests.add(request);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -188,7 +188,7 @@ public abstract class AbstractRequestExpectationManager implements RequestExpect
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
this.expectations.clear();
|
||||
getExpectations().clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -45,7 +45,7 @@ public interface RequestExpectationManager {
|
||||
* @param request the request
|
||||
* @return the response to return if the request was validated.
|
||||
* @throws AssertionError when some expectations were not met
|
||||
* @throws IOException
|
||||
* @throws IOException in case of any validation errors
|
||||
*/
|
||||
ClientHttpResponse validateRequest(ClientHttpRequest request) throws IOException;
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.springframework.util.Assert;
|
||||
* Subsequent request executions may be inserted anywhere thereafter.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Juergen Hoeller
|
||||
* @since 4.3
|
||||
*/
|
||||
public class SimpleRequestExpectationManager extends AbstractRequestExpectationManager {
|
||||
@@ -49,29 +50,19 @@ public class SimpleRequestExpectationManager extends AbstractRequestExpectationM
|
||||
|
||||
@Override
|
||||
public ClientHttpResponse validateRequestInternal(ClientHttpRequest request) throws IOException {
|
||||
RequestExpectation expectation;
|
||||
try {
|
||||
expectation = next(request);
|
||||
expectation.match(request);
|
||||
}
|
||||
catch (AssertionError error) {
|
||||
expectation = this.repeatExpectations.findExpectation(request);
|
||||
if (expectation == null) {
|
||||
throw error;
|
||||
RequestExpectation expectation = this.repeatExpectations.findExpectation(request);
|
||||
if (expectation == null) {
|
||||
if (!this.expectationIterator.hasNext()) {
|
||||
throw createUnexpectedRequestError(request);
|
||||
}
|
||||
expectation = this.expectationIterator.next();
|
||||
expectation.match(request);
|
||||
}
|
||||
ClientHttpResponse response = expectation.createResponse(request);
|
||||
this.repeatExpectations.update(expectation);
|
||||
return response;
|
||||
}
|
||||
|
||||
private RequestExpectation next(ClientHttpRequest request) {
|
||||
if (this.expectationIterator.hasNext()) {
|
||||
return this.expectationIterator.next();
|
||||
}
|
||||
throw createUnexpectedRequestError(request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
super.reset();
|
||||
|
||||
Reference in New Issue
Block a user