INT-4503: Fix assertion in HttpMessageHandlerSpec
JIRA: https://jira.spring.io/browse/INT-4503 Custom `ErrorHandler` can't be used if an external `RestTemplate` is provided * Replace `isClientSet()`` with ``!isClientSet()`` for `isTrue` assertion in `errorHandler` method * Replace assertion of `restTemplate` `isNull` with assertion `!isClientSet()` `isTrue` in the `requestFactory()` method * Add test coverage for the `errorHandler` into the `HttpDslTests` **Cherry-pick to 5.0.x**
This commit is contained in:
committed by
Artem Bilan
parent
1a4de86b30
commit
865e2162e5
@@ -22,6 +22,7 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@@ -29,11 +30,12 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.integration.channel.DirectChannel;
|
||||
import org.springframework.integration.config.EnableIntegration;
|
||||
import org.springframework.integration.dsl.IntegrationFlow;
|
||||
@@ -59,14 +61,15 @@ import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.client.MockMvcClientHttpRequestFactory;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.client.DefaultResponseErrorHandler;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Artem Bilan
|
||||
* @author Shiliang Li
|
||||
* @author Gary Russell
|
||||
* @author Oleksii Komlyk
|
||||
*
|
||||
* @since 5.0
|
||||
*/
|
||||
@@ -97,9 +100,8 @@ public class HttpDslTests {
|
||||
|
||||
@Test
|
||||
public void testHttpProxyFlow() throws Exception {
|
||||
RestTemplate mockMvcRestTemplate = new RestTemplate(new MockMvcClientHttpRequestFactory(this.mockMvc));
|
||||
new DirectFieldAccessor(this.serviceInternalGatewayHandler)
|
||||
.setPropertyValue("restTemplate", mockMvcRestTemplate);
|
||||
ClientHttpRequestFactory mockRequestFactory = new MockMvcClientHttpRequestFactory(this.mockMvc);
|
||||
this.serviceInternalGatewayHandler.setRequestFactory(mockRequestFactory);
|
||||
|
||||
this.mockMvc.perform(
|
||||
get("/service")
|
||||
@@ -115,7 +117,10 @@ public class HttpDslTests {
|
||||
.param("name", "name"))
|
||||
.andExpect(
|
||||
status()
|
||||
.isForbidden());
|
||||
.isForbidden())
|
||||
.andExpect(
|
||||
content()
|
||||
.string("Error"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -213,7 +218,8 @@ public class HttpDslTests {
|
||||
.errorChannel("httpProxyErrorFlow.input"))
|
||||
.handle(Http.outboundGateway("/service/internal?{params}")
|
||||
.uriVariable("params", "payload")
|
||||
.expectedResponseType(String.class),
|
||||
.expectedResponseType(String.class)
|
||||
.errorHandler(new HttpProxyResponseErrorHandler()),
|
||||
e -> e.id("serviceInternalGateway"))
|
||||
.get();
|
||||
}
|
||||
@@ -242,4 +248,15 @@ public class HttpDslTests {
|
||||
|
||||
}
|
||||
|
||||
public static class HttpProxyResponseErrorHandler extends DefaultResponseErrorHandler {
|
||||
|
||||
@Override
|
||||
protected byte[] getResponseBody(ClientHttpResponse response) {
|
||||
Charset charset = getCharset(response);
|
||||
String content = "Error";
|
||||
return charset != null ? content.getBytes(charset) : content.getBytes();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user