@ -27,6 +27,8 @@ import org.springframework.http.client.AsyncClientHttpRequest;
@@ -27,6 +27,8 @@ import org.springframework.http.client.AsyncClientHttpRequest;
import org.springframework.http.client.AsyncClientHttpRequestFactory ;
import org.springframework.http.client.ClientHttpRequest ;
import org.springframework.http.client.ClientHttpRequestFactory ;
import org.springframework.http.client.ClientHttpResponse ;
import org.springframework.mock.http.client.MockAsyncClientHttpRequest ;
import org.springframework.test.web.client.match.MockRestRequestMatchers ;
import org.springframework.test.web.client.response.MockRestResponseCreators ;
import org.springframework.util.Assert ;
@ -96,11 +98,11 @@ import org.springframework.web.client.support.RestGatewaySupport;
@@ -96,11 +98,11 @@ import org.springframework.web.client.support.RestGatewaySupport;
* /
public class MockRestServiceServer {
private final List < RequestMatcherClientHttpRequest > expectedRequest s =
new LinkedList < RequestMatcherClientHttpRequest > ( ) ;
private final List < DefaultResponseActions > responseAction s =
new LinkedList < DefaultResponseActions > ( ) ;
private final List < RequestMatcherClientHttpRequest > actualR equests =
new LinkedList < RequestMatcher ClientHttpRequest> ( ) ;
private final List < MockAsyncClientHttpRequest > r equests =
new LinkedList < MockAsync ClientHttpRequest> ( ) ;
/ * *
@ -161,9 +163,9 @@ public class MockRestServiceServer {
@@ -161,9 +163,9 @@ public class MockRestServiceServer {
* @return used to set up further expectations or to define a response
* /
public ResponseActions expect ( RequestMatcher requestMatcher ) {
Assert . state ( this . actualR equests. isEmpty ( ) , "Can't add more expected requests with test already underway" ) ;
RequestMatcherClientHttpRequest request = new RequestMatcherClientHttpRequest ( requestMatcher ) ;
this . expectedRequest s. add ( request ) ;
Assert . state ( this . r equests. isEmpty ( ) , "Can't add more expected requests with test already underway" ) ;
DefaultResponseActions request = new DefaultResponseActions ( requestMatcher ) ;
this . responseAction s. add ( request ) ;
return request ;
}
@ -173,7 +175,7 @@ public class MockRestServiceServer {
@@ -173,7 +175,7 @@ public class MockRestServiceServer {
* @throws AssertionError when some expectations were not met
* /
public void verify ( ) {
if ( this . expectedRequest s. isEmpty ( ) | | this . expectedRequests . equals ( this . actualRequests ) ) {
if ( this . responseAction s. isEmpty ( ) | | this . responseActions . size ( ) = = this . requests . size ( ) ) {
return ;
}
throw new AssertionError ( getVerifyMessage ( ) ) ;
@ -181,15 +183,15 @@ public class MockRestServiceServer {
@@ -181,15 +183,15 @@ public class MockRestServiceServer {
private String getVerifyMessage ( ) {
StringBuilder sb = new StringBuilder ( "Further request(s) expected\n" ) ;
if ( this . actualR equests. size ( ) > 0 ) {
if ( this . r equests. size ( ) > 0 ) {
sb . append ( "The following " ) ;
}
sb . append ( this . actualR equests. size ( ) ) . append ( " out of " ) ;
sb . append ( this . expectedRequest s. size ( ) ) . append ( " were executed" ) ;
sb . append ( this . r equests. size ( ) ) . append ( " out of " ) ;
sb . append ( this . responseAction s. size ( ) ) . append ( " were executed" ) ;
if ( this . actualR equests. size ( ) > 0 ) {
if ( this . r equests. size ( ) > 0 ) {
sb . append ( ":\n" ) ;
for ( RequestMatcher ClientHttpRequest request : this . actualR equests) {
for ( MockAsync ClientHttpRequest request : this . r equests) {
sb . append ( request . toString ( ) ) . append ( "\n" ) ;
}
}
@ -199,12 +201,12 @@ public class MockRestServiceServer {
@@ -199,12 +201,12 @@ public class MockRestServiceServer {
/ * *
* Mock ClientHttpRequestFactory that creates requests by iterating
* over the list of expected { @link RequestMatcherClientHttpRequest } ' s .
* over the list of expected { @link DefaultResponseActions } ' s .
* /
private class RequestMatcherClientHttpRequestFactory
implements ClientHttpRequestFactory , AsyncClientHttpRequestFactory {
private Iterator < RequestMatcherClientHttpRequest > requestIterator ;
private Iterator < DefaultResponseActions > requestIterator ;
@Override
public ClientHttpRequest createRequest ( URI uri , HttpMethod httpMethod ) throws IOException {
@ -216,23 +218,38 @@ public class MockRestServiceServer {
@@ -216,23 +218,38 @@ public class MockRestServiceServer {
return createRequestInternal ( uri , httpMethod ) ;
}
private RequestMatcher ClientHttpRequest createRequestInternal ( URI uri , HttpMethod httpMethod ) {
private MockAsync ClientHttpRequest createRequestInternal ( URI uri , HttpMethod httpMethod ) {
Assert . notNull ( uri , "'uri' must not be null" ) ;
Assert . notNull ( httpMethod , "'httpMethod' must not be null" ) ;
MockAsyncClientHttpRequest request = new MockAsyncClientHttpRequest ( httpMethod , uri ) {
@Override
protected ClientHttpResponse executeInternal ( ) throws IOException {
ClientHttpResponse response = validateRequest ( this ) ;
setResponse ( response ) ;
return response ;
}
} ;
MockRestServiceServer . this . requests . add ( request ) ;
return request ;
}
private ClientHttpResponse validateRequest ( MockAsyncClientHttpRequest request )
throws IOException {
if ( this . requestIterator = = null ) {
this . requestIterator = MockRestServiceServer . this . expectedRequests . iterator ( ) ;
this . requestIterator = MockRestServiceServer . this . responseAction s. iterator ( ) ;
}
if ( ! this . requestIterator . hasNext ( ) ) {
throw new AssertionError ( "No further requests expected: HTTP " + httpMethod + " " + uri ) ;
throw new AssertionError ( "No further requests expected: HTTP " +
request . getMethod ( ) + " " + request . getURI ( ) ) ;
}
RequestMatcherClientHttpRequest request = this . requestIterator . next ( ) ;
request . setURI ( uri ) ;
request . setMethod ( httpMethod ) ;
DefaultResponseActions responseActions = this . requestIterator . next ( ) ;
responseActions . match ( request ) ;
MockRestServiceServer . this . actualRequests . add ( request ) ;
return request ;
return responseActions . createResponse ( request ) ;
}
}