Skip to content

Commit 865e216

Browse files
imitbnartembilan
authored andcommitted
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**
1 parent 1a4de86 commit 865e216

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

spring-integration-http/src/main/java/org/springframework/integration/http/dsl/HttpMessageHandlerSpec.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@
3434
*
3535
* @author Artem Bilan
3636
* @author Shiliang Li
37+
* @author Oleksii Komlyk
3738
*
3839
* @since 5.0
3940
*
@@ -63,8 +64,7 @@ public class HttpMessageHandlerSpec
6364
* @return the spec
6465
*/
6566
public HttpMessageHandlerSpec requestFactory(ClientHttpRequestFactory requestFactory) {
66-
Assert.isNull(this.restTemplate,
67-
"the 'requestFactory' must be specified on the provided 'restTemplate': " + this.restTemplate);
67+
Assert.isTrue(!isClientSet(), "the 'requestFactory' must be specified on the provided 'restTemplate'");
6868
this.target.setRequestFactory(requestFactory);
6969
return this;
7070
}
@@ -75,8 +75,7 @@ public HttpMessageHandlerSpec requestFactory(ClientHttpRequestFactory requestFac
7575
* @return the spec
7676
*/
7777
public HttpMessageHandlerSpec errorHandler(ResponseErrorHandler errorHandler) {
78-
Assert.isTrue(this.isClientSet(),
79-
"the 'errorHandler' must be specified on the provided 'restTemplate'");
78+
Assert.isTrue(!isClientSet(), "the 'errorHandler' must be specified on the provided 'restTemplate'");
8079
this.target.setErrorHandler(errorHandler);
8180
return _this();
8281
}
@@ -88,7 +87,7 @@ public HttpMessageHandlerSpec errorHandler(ResponseErrorHandler errorHandler) {
8887
* @return the spec
8988
*/
9089
public HttpMessageHandlerSpec messageConverters(HttpMessageConverter<?>... messageConverters) {
91-
Assert.isTrue(!isClientSet(), "the 'messageConverters' must be specified on the provided restTemplate");
90+
Assert.isTrue(!isClientSet(), "the 'messageConverters' must be specified on the provided 'restTemplate'");
9291
this.target.setMessageConverters(Arrays.asList(messageConverters));
9392
return _this();
9493
}

spring-integration-http/src/test/java/org/springframework/integration/http/dsl/HttpDslTests.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,20 @@
2222
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
2323
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
2424

25+
import java.nio.charset.Charset;
2526
import java.util.Collections;
2627
import java.util.List;
2728

2829
import org.junit.Before;
2930
import org.junit.Test;
3031
import org.junit.runner.RunWith;
3132

32-
import org.springframework.beans.DirectFieldAccessor;
3333
import org.springframework.beans.factory.annotation.Autowired;
3434
import org.springframework.context.annotation.Bean;
3535
import org.springframework.context.annotation.Configuration;
3636
import org.springframework.http.ResponseEntity;
37+
import org.springframework.http.client.ClientHttpRequestFactory;
38+
import org.springframework.http.client.ClientHttpResponse;
3739
import org.springframework.integration.channel.DirectChannel;
3840
import org.springframework.integration.config.EnableIntegration;
3941
import org.springframework.integration.dsl.IntegrationFlow;
@@ -59,14 +61,15 @@
5961
import org.springframework.test.web.client.MockMvcClientHttpRequestFactory;
6062
import org.springframework.test.web.servlet.MockMvc;
6163
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
64+
import org.springframework.web.client.DefaultResponseErrorHandler;
6265
import org.springframework.web.client.HttpClientErrorException;
63-
import org.springframework.web.client.RestTemplate;
6466
import org.springframework.web.context.WebApplicationContext;
6567

6668
/**
6769
* @author Artem Bilan
6870
* @author Shiliang Li
6971
* @author Gary Russell
72+
* @author Oleksii Komlyk
7073
*
7174
* @since 5.0
7275
*/
@@ -97,9 +100,8 @@ public void setup() {
97100

98101
@Test
99102
public void testHttpProxyFlow() throws Exception {
100-
RestTemplate mockMvcRestTemplate = new RestTemplate(new MockMvcClientHttpRequestFactory(this.mockMvc));
101-
new DirectFieldAccessor(this.serviceInternalGatewayHandler)
102-
.setPropertyValue("restTemplate", mockMvcRestTemplate);
103+
ClientHttpRequestFactory mockRequestFactory = new MockMvcClientHttpRequestFactory(this.mockMvc);
104+
this.serviceInternalGatewayHandler.setRequestFactory(mockRequestFactory);
103105

104106
this.mockMvc.perform(
105107
get("/service")
@@ -115,7 +117,10 @@ public void testHttpProxyFlow() throws Exception {
115117
.param("name", "name"))
116118
.andExpect(
117119
status()
118-
.isForbidden());
120+
.isForbidden())
121+
.andExpect(
122+
content()
123+
.string("Error"));
119124
}
120125

121126
@Test
@@ -213,7 +218,8 @@ public IntegrationFlow httpProxyFlow() {
213218
.errorChannel("httpProxyErrorFlow.input"))
214219
.handle(Http.outboundGateway("/service/internal?{params}")
215220
.uriVariable("params", "payload")
216-
.expectedResponseType(String.class),
221+
.expectedResponseType(String.class)
222+
.errorHandler(new HttpProxyResponseErrorHandler()),
217223
e -> e.id("serviceInternalGateway"))
218224
.get();
219225
}
@@ -242,4 +248,15 @@ public ChannelSecurityInterceptor channelSecurityInterceptor(AccessDecisionManag
242248

243249
}
244250

251+
public static class HttpProxyResponseErrorHandler extends DefaultResponseErrorHandler {
252+
253+
@Override
254+
protected byte[] getResponseBody(ClientHttpResponse response) {
255+
Charset charset = getCharset(response);
256+
String content = "Error";
257+
return charset != null ? content.getBytes(charset) : content.getBytes();
258+
}
259+
260+
}
261+
245262
}

0 commit comments

Comments
 (0)