Prevent CastCastException when stripping root URI
Update RootUriRequestExpectationManager to only wrap requests when they cannot be cast to MockClientHttpRequest. This prevents later ClassCastExceptions from being thrown with @RestClientTests that define expected body content. Fixes gh-6845
This commit is contained in:
parent
c56f30fd91
commit
41a36c4d40
|
@ -42,4 +42,8 @@ public class ExampleRestClient {
|
|||
return this.restTemplate.getForEntity("/test", String.class).getBody();
|
||||
}
|
||||
|
||||
public void testPostWithBody(String body) {
|
||||
this.restTemplate.postForObject("/test", body, String.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||
import org.springframework.test.web.client.MockRestServiceServer;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.content;
|
||||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
|
||||
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
|
||||
|
||||
|
@ -57,4 +58,11 @@ public class RestClientRestIntegrationTests {
|
|||
assertThat(this.client.test()).isEqualTo("2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mockServerCallWithContent() throws Exception {
|
||||
this.server.expect(requestTo("/test")).andExpect(content().string("test"))
|
||||
.andRespond(withSuccess("1", MediaType.TEXT_HTML));
|
||||
this.client.testPostWithBody("test");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ import org.springframework.boot.web.client.RootUriTemplateHandler;
|
|||
import org.springframework.http.client.ClientHttpRequest;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.http.client.support.HttpRequestWrapper;
|
||||
import org.springframework.mock.http.client.MockClientHttpRequest;
|
||||
import org.springframework.test.web.client.ExpectedCount;
|
||||
import org.springframework.test.web.client.MockRestServiceServer;
|
||||
import org.springframework.test.web.client.MockRestServiceServer.MockRestServiceServerBuilder;
|
||||
|
@ -77,8 +78,7 @@ public class RootUriRequestExpectationManager implements RequestExpectationManag
|
|||
throws IOException {
|
||||
String uri = request.getURI().toString();
|
||||
if (uri.startsWith(this.rootUri)) {
|
||||
uri = uri.substring(this.rootUri.length());
|
||||
request = new ReplaceUriClientHttpRequest(uri, request);
|
||||
request = replaceURI(request, uri.substring(this.rootUri.length()));
|
||||
}
|
||||
try {
|
||||
return this.expectationManager.validateRequest(request);
|
||||
|
@ -94,6 +94,22 @@ public class RootUriRequestExpectationManager implements RequestExpectationManag
|
|||
}
|
||||
}
|
||||
|
||||
private ClientHttpRequest replaceURI(ClientHttpRequest request,
|
||||
String replacementUri) {
|
||||
URI uri;
|
||||
try {
|
||||
uri = new URI(replacementUri);
|
||||
if (request instanceof MockClientHttpRequest) {
|
||||
((MockClientHttpRequest) request).setURI(uri);
|
||||
return request;
|
||||
}
|
||||
return new ReplaceUriClientHttpRequest(uri, request);
|
||||
}
|
||||
catch (URISyntaxException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void verify() {
|
||||
this.expectationManager.verify();
|
||||
|
@ -156,14 +172,9 @@ public class RootUriRequestExpectationManager implements RequestExpectationManag
|
|||
|
||||
private final URI uri;
|
||||
|
||||
ReplaceUriClientHttpRequest(String uri, ClientHttpRequest request) {
|
||||
ReplaceUriClientHttpRequest(URI uri, ClientHttpRequest request) {
|
||||
super(request);
|
||||
try {
|
||||
this.uri = new URI(uri);
|
||||
}
|
||||
catch (URISyntaxException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue