Skip to content

Commit 26bc248

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** (cherry picked from commit 865e216)
1 parent 0635c25 commit 26bc248

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;
@@ -58,14 +60,15 @@
5860
import org.springframework.test.web.client.MockMvcClientHttpRequestFactory;
5961
import org.springframework.test.web.servlet.MockMvc;
6062
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
63+
import org.springframework.web.client.DefaultResponseErrorHandler;
6164
import org.springframework.web.client.HttpClientErrorException;
62-
import org.springframework.web.client.RestTemplate;
6365
import org.springframework.web.context.WebApplicationContext;
6466

6567
/**
6668
* @author Artem Bilan
6769
* @author Shiliang Li
6870
* @author Gary Russell
71+
* @author Oleksii Komlyk
6972
*
7073
* @since 5.0
7174
*/
@@ -93,9 +96,8 @@ public void setup() {
9396

9497
@Test
9598
public void testHttpProxyFlow() throws Exception {
96-
RestTemplate mockMvcRestTemplate = new RestTemplate(new MockMvcClientHttpRequestFactory(this.mockMvc));
97-
new DirectFieldAccessor(this.serviceInternalGatewayHandler)
98-
.setPropertyValue("restTemplate", mockMvcRestTemplate);
99+
ClientHttpRequestFactory mockRequestFactory = new MockMvcClientHttpRequestFactory(this.mockMvc);
100+
this.serviceInternalGatewayHandler.setRequestFactory(mockRequestFactory);
99101

100102
this.mockMvc.perform(
101103
get("/service")
@@ -111,7 +113,10 @@ public void testHttpProxyFlow() throws Exception {
111113
.param("name", "name"))
112114
.andExpect(
113115
status()
114-
.isForbidden());
116+
.isForbidden())
117+
.andExpect(
118+
content()
119+
.string("Error"));
115120
}
116121

117122
@Configuration
@@ -178,7 +183,8 @@ public IntegrationFlow httpProxyFlow() {
178183
.errorChannel("httpProxyErrorFlow.input"))
179184
.handle(Http.outboundGateway("/service/internal?{params}")
180185
.uriVariable("params", "payload")
181-
.expectedResponseType(String.class),
186+
.expectedResponseType(String.class)
187+
.errorHandler(new HttpProxyResponseErrorHandler()),
182188
e -> e.id("serviceInternalGateway"))
183189
.get();
184190
}
@@ -207,4 +213,15 @@ public ChannelSecurityInterceptor channelSecurityInterceptor(AccessDecisionManag
207213

208214
}
209215

216+
public static class HttpProxyResponseErrorHandler extends DefaultResponseErrorHandler {
217+
218+
@Override
219+
protected byte[] getResponseBody(ClientHttpResponse response) {
220+
Charset charset = getCharset(response);
221+
String content = "Error";
222+
return charset != null ? content.getBytes(charset) : content.getBytes();
223+
}
224+
225+
}
226+
210227
}

0 commit comments

Comments
 (0)