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:
Oleksii Komlyk
2018-07-24 16:26:26 +03:00
committed by Artem Bilan
parent 1a4de86b30
commit 865e2162e5
2 changed files with 29 additions and 13 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2016-2017 the original author or authors. * Copyright 2016-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@ import org.springframework.web.client.RestTemplate;
* *
* @author Artem Bilan * @author Artem Bilan
* @author Shiliang Li * @author Shiliang Li
* @author Oleksii Komlyk
* *
* @since 5.0 * @since 5.0
* *
@@ -63,8 +64,7 @@ public class HttpMessageHandlerSpec
* @return the spec * @return the spec
*/ */
public HttpMessageHandlerSpec requestFactory(ClientHttpRequestFactory requestFactory) { public HttpMessageHandlerSpec requestFactory(ClientHttpRequestFactory requestFactory) {
Assert.isNull(this.restTemplate, Assert.isTrue(!isClientSet(), "the 'requestFactory' must be specified on the provided 'restTemplate'");
"the 'requestFactory' must be specified on the provided 'restTemplate': " + this.restTemplate);
this.target.setRequestFactory(requestFactory); this.target.setRequestFactory(requestFactory);
return this; return this;
} }
@@ -75,8 +75,7 @@ public class HttpMessageHandlerSpec
* @return the spec * @return the spec
*/ */
public HttpMessageHandlerSpec errorHandler(ResponseErrorHandler errorHandler) { public HttpMessageHandlerSpec errorHandler(ResponseErrorHandler errorHandler) {
Assert.isTrue(this.isClientSet(), Assert.isTrue(!isClientSet(), "the 'errorHandler' must be specified on the provided 'restTemplate'");
"the 'errorHandler' must be specified on the provided 'restTemplate'");
this.target.setErrorHandler(errorHandler); this.target.setErrorHandler(errorHandler);
return _this(); return _this();
} }
@@ -88,7 +87,7 @@ public class HttpMessageHandlerSpec
* @return the spec * @return the spec
*/ */
public HttpMessageHandlerSpec messageConverters(HttpMessageConverter<?>... messageConverters) { public HttpMessageHandlerSpec messageConverters(HttpMessageConverter<?>... messageConverters) {
Assert.isTrue(!isClientSet(), "the 'messageConverters' must be specified on the provided restTemplate"); Assert.isTrue(!isClientSet(), "the 'messageConverters' must be specified on the provided 'restTemplate'");
this.target.setMessageConverters(Arrays.asList(messageConverters)); this.target.setMessageConverters(Arrays.asList(messageConverters));
return _this(); return _this();
} }

View File

@@ -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.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.nio.charset.Charset;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@@ -29,11 +30,12 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity; 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.channel.DirectChannel;
import org.springframework.integration.config.EnableIntegration; import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.dsl.IntegrationFlow; 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.client.MockMvcClientHttpRequestFactory;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; 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.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
/** /**
* @author Artem Bilan * @author Artem Bilan
* @author Shiliang Li * @author Shiliang Li
* @author Gary Russell * @author Gary Russell
* @author Oleksii Komlyk
* *
* @since 5.0 * @since 5.0
*/ */
@@ -97,9 +100,8 @@ public class HttpDslTests {
@Test @Test
public void testHttpProxyFlow() throws Exception { public void testHttpProxyFlow() throws Exception {
RestTemplate mockMvcRestTemplate = new RestTemplate(new MockMvcClientHttpRequestFactory(this.mockMvc)); ClientHttpRequestFactory mockRequestFactory = new MockMvcClientHttpRequestFactory(this.mockMvc);
new DirectFieldAccessor(this.serviceInternalGatewayHandler) this.serviceInternalGatewayHandler.setRequestFactory(mockRequestFactory);
.setPropertyValue("restTemplate", mockMvcRestTemplate);
this.mockMvc.perform( this.mockMvc.perform(
get("/service") get("/service")
@@ -115,7 +117,10 @@ public class HttpDslTests {
.param("name", "name")) .param("name", "name"))
.andExpect( .andExpect(
status() status()
.isForbidden()); .isForbidden())
.andExpect(
content()
.string("Error"));
} }
@Test @Test
@@ -213,7 +218,8 @@ public class HttpDslTests {
.errorChannel("httpProxyErrorFlow.input")) .errorChannel("httpProxyErrorFlow.input"))
.handle(Http.outboundGateway("/service/internal?{params}") .handle(Http.outboundGateway("/service/internal?{params}")
.uriVariable("params", "payload") .uriVariable("params", "payload")
.expectedResponseType(String.class), .expectedResponseType(String.class)
.errorHandler(new HttpProxyResponseErrorHandler()),
e -> e.id("serviceInternalGateway")) e -> e.id("serviceInternalGateway"))
.get(); .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();
}
}
} }