Fixed classcast in sc-WireMock; fixes #399
This commit is contained in:
@@ -43,9 +43,11 @@ public class WireMockRestTemplateConfiguration {
|
||||
return new RestTemplateCustomizer() {
|
||||
@Override
|
||||
public void customize(RestTemplate restTemplate) {
|
||||
HttpComponentsClientHttpRequestFactory factory = (HttpComponentsClientHttpRequestFactory) restTemplate
|
||||
.getRequestFactory();
|
||||
factory.setHttpClient(createSslHttpClient());
|
||||
if (restTemplate.getRequestFactory() instanceof HttpComponentsClientHttpRequestFactory) {
|
||||
HttpComponentsClientHttpRequestFactory factory = (HttpComponentsClientHttpRequestFactory) restTemplate
|
||||
.getRequestFactory();
|
||||
factory.setHttpClient(createSslHttpClient());
|
||||
}
|
||||
}
|
||||
|
||||
private HttpClient createSslHttpClient() {
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.springframework.cloud.contract.wiremock.issues.issue399;
|
||||
|
||||
import org.assertj.core.api.BDDAssertions;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.get;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@AutoConfigureWireMock(port = 0)
|
||||
public class NotOrderedCustomizerTest {
|
||||
|
||||
@Value("${wiremock.server.port}") Integer port;
|
||||
@Autowired RestTemplateBuilder restTemplateBuilder;
|
||||
|
||||
@Test
|
||||
public void should_not_fail_when_ordered_customizer_added_interceptor_to_rest_template() {
|
||||
stubFor(get(urlEqualTo("/some-url"))
|
||||
.willReturn(aResponse().withStatus(200).withBody("Yeah!")));
|
||||
RestTemplateClient client = new RestTemplateClient(
|
||||
restTemplateBuilder.rootUri("http://localhost:" + port));
|
||||
|
||||
String body = client.get();
|
||||
|
||||
BDDAssertions.then(body).isEqualTo("Yeah!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.springframework.cloud.contract.wiremock.issues.issue399;
|
||||
|
||||
import org.assertj.core.api.BDDAssertions;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.get;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@AutoConfigureWireMock(port = 0)
|
||||
@ActiveProfiles("bug")
|
||||
public class OrderedCustomizerTest {
|
||||
|
||||
@Value("${wiremock.server.port}") Integer port;
|
||||
@Autowired RestTemplateBuilder restTemplateBuilder;
|
||||
|
||||
@Test
|
||||
public void should_not_fail_when_ordered_customizer_added_interceptor_to_rest_template() {
|
||||
stubFor(get(urlEqualTo("/some-url"))
|
||||
.willReturn(aResponse().withStatus(200).withBody("Yeah!")));
|
||||
RestTemplateClient client = new RestTemplateClient(
|
||||
restTemplateBuilder.rootUri("http://localhost:" + port));
|
||||
|
||||
String body = client.get();
|
||||
|
||||
BDDAssertions.then(body).isEqualTo("Yeah!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package org.springframework.cloud.contract.wiremock.issues.issue399;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.boot.web.client.RestTemplateCustomizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.client.ClientHttpRequestExecution;
|
||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringContractWiremockIssueDemoApplication {
|
||||
|
||||
private static final int SOME_NOT_LOWEST_PRECEDENCE = Ordered.LOWEST_PRECEDENCE - 1;
|
||||
|
||||
@Bean
|
||||
@Order(SOME_NOT_LOWEST_PRECEDENCE)
|
||||
@Profile("bug")
|
||||
public RestTemplateCustomizer someOrderedInterceptorCustomizer() {
|
||||
return new RestTemplateCustomizer() {
|
||||
@Override public void customize(RestTemplate restTemplate) {
|
||||
ClientHttpRequestInterceptor emptyInterceptor = new ClientHttpRequestInterceptor() {
|
||||
@Override public ClientHttpResponse intercept(HttpRequest request,
|
||||
byte[] body, ClientHttpRequestExecution execution)
|
||||
throws IOException {
|
||||
return execution.execute(request, body);
|
||||
}
|
||||
};
|
||||
restTemplate.getInterceptors().add(emptyInterceptor);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RestTemplateCustomizer someNotOrderedInterceptorCustomizer() {
|
||||
return new RestTemplateCustomizer() {
|
||||
@Override public void customize(RestTemplate restTemplate) {
|
||||
ClientHttpRequestInterceptor emptyInterceptor = new ClientHttpRequestInterceptor() {
|
||||
@Override public ClientHttpResponse intercept(HttpRequest request,
|
||||
byte[] body, ClientHttpRequestExecution execution)
|
||||
throws IOException {
|
||||
return execution.execute(request, body);
|
||||
}
|
||||
};
|
||||
restTemplate.getInterceptors().add(emptyInterceptor);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Order
|
||||
public RestTemplateCustomizer someLowestPrecedenceOrderedInterceptorCustomizer() {
|
||||
return new RestTemplateCustomizer() {
|
||||
@Override public void customize(RestTemplate restTemplate) {
|
||||
ClientHttpRequestInterceptor emptyInterceptor = new ClientHttpRequestInterceptor() {
|
||||
@Override public ClientHttpResponse intercept(HttpRequest request,
|
||||
byte[] body, ClientHttpRequestExecution execution)
|
||||
throws IOException {
|
||||
return execution.execute(request, body);
|
||||
}
|
||||
};
|
||||
restTemplate.getInterceptors().add(emptyInterceptor);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class RestTemplateClient {
|
||||
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
public RestTemplateClient(RestTemplateBuilder restTemplateBuilder) {
|
||||
this.restTemplate = restTemplateBuilder.build();
|
||||
}
|
||||
|
||||
public String get() {
|
||||
|
||||
return restTemplate.getForObject("/some-url", String.class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user