Skip to content

Commit

Permalink
SimpleRequestExpectationManager properly handles sequential requests …
Browse files Browse the repository at this point in the history
…with different count

Issue: SPR-15672
(cherry picked from commit efb735a)
  • Loading branch information
jhoeller committed Jun 30, 2017
1 parent 35efdf6 commit a17255a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 41 deletions.
Expand Up @@ -38,6 +38,7 @@
* to expectations following the order of declaration or not.
*
* @author Rossen Stoyanchev
* @author Juergen Hoeller
* @since 4.3
*/
public abstract class AbstractRequestExpectationManager implements RequestExpectationManager {
Expand All @@ -46,8 +47,6 @@ public abstract class AbstractRequestExpectationManager implements RequestExpect

private final List<ClientHttpRequest> requests = new LinkedList<ClientHttpRequest>();

private final Object lock = new Object();


protected List<RequestExpectation> getExpectations() {
return this.expectations;
Expand All @@ -68,12 +67,13 @@ public ResponseActions expectRequest(ExpectedCount count, RequestMatcher matcher

@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;
}
}
Expand Down Expand Up @@ -186,7 +186,7 @@ public RequestExpectation findExpectation(ClientHttpRequest request) throws IOEx
}

public void reset() {
this.expectations.clear();
getExpectations().clear();
}
}

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

Expand Down
Expand Up @@ -32,6 +32,7 @@
* Subsequent request executions may be inserted anywhere thereafter.
*
* @author Rossen Stoyanchev
* @author Juergen Hoeller
* @since 4.3
*/
public class SimpleRequestExpectationManager extends AbstractRequestExpectationManager {
Expand All @@ -49,29 +50,19 @@ protected void afterExpectationsDeclared() {

@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();
Expand Down
@@ -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.
Expand All @@ -25,26 +25,22 @@

import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.mock.http.client.MockAsyncClientHttpRequest;

import static org.junit.Assert.assertEquals;
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.test.web.client.ExpectedCount.max;
import static org.springframework.test.web.client.ExpectedCount.min;
import static org.springframework.test.web.client.ExpectedCount.once;
import static org.springframework.test.web.client.ExpectedCount.times;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
import org.springframework.mock.http.client.MockClientHttpRequest;

import static org.junit.Assert.*;
import static org.springframework.http.HttpMethod.*;
import static org.springframework.test.web.client.ExpectedCount.*;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
import static org.springframework.test.web.client.response.MockRestResponseCreators.*;

/**
* Unit tests for {@link SimpleRequestExpectationManager}.
*
* @author Rossen Stoyanchev
*/
public class SimpleRequestExpectationManagerTests {

private SimpleRequestExpectationManager manager = new SimpleRequestExpectationManager();
private final SimpleRequestExpectationManager manager = new SimpleRequestExpectationManager();

@Rule
public ExpectedException thrown = ExpectedException.none();
Expand Down Expand Up @@ -105,13 +101,15 @@ public void sequentialRequestsTooFew() throws Exception {

@Test
public void repeatedRequests() throws Exception {
this.manager.expectRequest(times(2), requestTo("/foo")).andExpect(method(GET)).andRespond(withSuccess());
this.manager.expectRequest(times(2), requestTo("/bar")).andExpect(method(GET)).andRespond(withSuccess());
this.manager.expectRequest(times(3), requestTo("/foo")).andExpect(method(GET)).andRespond(withSuccess());
this.manager.expectRequest(times(3), requestTo("/bar")).andExpect(method(GET)).andRespond(withSuccess());

this.manager.validateRequest(createRequest(GET, "/foo"));
this.manager.validateRequest(createRequest(GET, "/bar"));
this.manager.validateRequest(createRequest(GET, "/foo"));
this.manager.validateRequest(createRequest(GET, "/bar"));
this.manager.validateRequest(createRequest(GET, "/foo"));
this.manager.validateRequest(createRequest(GET, "/bar"));
this.manager.verify();
}

Expand Down Expand Up @@ -160,10 +158,21 @@ public void repeatedRequestsNotInOrder() throws Exception {
this.manager.validateRequest(createRequest(POST, "/foo"));
}

@Test // SPR-15672
public void sequentialRequestsWithDifferentCount() throws Exception {
this.manager.expectRequest(times(2), requestTo("/foo")).andExpect(method(GET)).andRespond(withSuccess());
this.manager.expectRequest(once(), requestTo("/bar")).andExpect(method(GET)).andRespond(withSuccess());

this.manager.validateRequest(createRequest(GET, "/foo"));
this.manager.validateRequest(createRequest(GET, "/foo"));
this.manager.validateRequest(createRequest(GET, "/bar"));
}


@SuppressWarnings("deprecation")
private ClientHttpRequest createRequest(HttpMethod method, String url) {
try {
return new MockAsyncClientHttpRequest(method, new URI(url));
return new MockClientHttpRequest(method, new URI(url));
}
catch (URISyntaxException ex) {
throw new IllegalStateException(ex);
Expand Down

0 comments on commit a17255a

Please sign in to comment.