Skip to content

Commit 0076677

Browse files
committed
add unit-test
Signed-off-by: jiangyuan <[email protected]>
1 parent aeffc5e commit 0076677

File tree

4 files changed

+74
-5
lines changed

4 files changed

+74
-5
lines changed

spring-cloud-gateway-server-mvc/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,10 @@
135135
<artifactId>rabbitmq</artifactId>
136136
<scope>test</scope>
137137
</dependency>
138+
<dependency>
139+
<groupId>org.apache.httpcomponents.client5</groupId>
140+
<artifactId>httpclient5</artifactId>
141+
<scope>test</scope>
142+
</dependency>
138143
</dependencies>
139144
</project>

spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/GatewayServerMvcAutoConfigurationTests.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import org.springframework.boot.builder.SpringApplicationBuilder;
3434
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
3535
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings;
36-
import org.springframework.boot.http.client.SimpleClientHttpRequestFactoryBuilder;
36+
import org.springframework.boot.http.client.ReactorClientHttpRequestFactoryBuilder;
3737
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3838
import org.springframework.cloud.gateway.server.mvc.filter.FormFilter;
3939
import org.springframework.cloud.gateway.server.mvc.filter.ForwardedRequestHeadersFilter;
@@ -47,6 +47,7 @@
4747
import org.springframework.context.ConfigurableApplicationContext;
4848

4949
import static org.assertj.core.api.Assertions.assertThat;
50+
import static org.springframework.boot.autoconfigure.http.client.HttpClientProperties.Factory.REACTOR;
5051

5152
public class GatewayServerMvcAutoConfigurationTests {
5253

@@ -151,7 +152,7 @@ void gatewayHttpClientPropertiesWork() {
151152
assertThat(properties.getConnectTimeout()).hasSeconds(1);
152153
assertThat(properties.getReadTimeout()).hasSeconds(2);
153154
assertThat(properties.getSsl().getBundle()).isEqualTo("mybundle");
154-
assertThat(properties.getFactory()).isNull();
155+
assertThat(properties.getFactory()).isEqualTo(REACTOR);
155156
assertThat(settings.readTimeout()).isEqualTo(Duration.ofSeconds(2));
156157
assertThat(settings.connectTimeout()).isEqualTo(Duration.ofSeconds(1));
157158
assertThat(settings.sslBundle()).isNotNull();
@@ -187,10 +188,10 @@ void bootHttpClientPropertiesWork() {
187188
@Test
188189
void settingHttpClientFactoryWorks() {
189190
ConfigurableApplicationContext context = new SpringApplicationBuilder(TestConfig.class)
190-
.properties("spring.main.web-application-type=none", "spring.http.client.factory=simple")
191+
.properties("spring.main.web-application-type=none")
191192
.run();
192193
ClientHttpRequestFactoryBuilder<?> builder = context.getBean(ClientHttpRequestFactoryBuilder.class);
193-
assertThat(builder).isInstanceOf(SimpleClientHttpRequestFactoryBuilder.class);
194+
assertThat(builder).isInstanceOf(ReactorClientHttpRequestFactoryBuilder.class);
194195
}
195196

196197
@SpringBootConfiguration

spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/filter/RetryFilterFunctionTests.java

+60
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,56 @@
1616

1717
package org.springframework.cloud.gateway.server.mvc.filter;
1818

19+
import java.time.Duration;
1920
import java.util.Set;
2021
import java.util.concurrent.ConcurrentHashMap;
2122
import java.util.concurrent.atomic.AtomicInteger;
2223

2324
import org.apache.commons.logging.Log;
2425
import org.apache.commons.logging.LogFactory;
26+
import org.apache.hc.core5.util.Timeout;
2527
import org.junit.jupiter.api.Test;
2628

29+
import org.springframework.beans.factory.ObjectProvider;
2730
import org.springframework.beans.factory.annotation.Autowired;
2831
import org.springframework.boot.SpringBootConfiguration;
2932
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
33+
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
3034
import org.springframework.boot.test.context.SpringBootTest;
3135
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
3236
import org.springframework.boot.test.web.server.LocalServerPort;
37+
import org.springframework.cloud.gateway.server.mvc.config.GatewayMvcProperties;
38+
import org.springframework.cloud.gateway.server.mvc.handler.ProxyExchange;
39+
import org.springframework.cloud.gateway.server.mvc.handler.ProxyExchangeHandlerFunction;
40+
import org.springframework.cloud.gateway.server.mvc.handler.RestClientProxyExchange;
3341
import org.springframework.cloud.gateway.server.mvc.test.HttpbinTestcontainers;
3442
import org.springframework.cloud.gateway.server.mvc.test.LocalServerPortUriResolver;
3543
import org.springframework.cloud.gateway.server.mvc.test.TestLoadBalancerConfig;
3644
import org.springframework.cloud.gateway.server.mvc.test.client.TestRestClient;
3745
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
46+
import org.springframework.context.ApplicationContext;
3847
import org.springframework.context.annotation.Bean;
48+
import org.springframework.context.event.ContextRefreshedEvent;
3949
import org.springframework.core.log.LogMessage;
4050
import org.springframework.http.HttpMethod;
4151
import org.springframework.http.HttpStatus;
4252
import org.springframework.http.ResponseEntity;
53+
import org.springframework.http.client.ClientHttpRequestFactory;
4354
import org.springframework.test.context.ContextConfiguration;
4455
import org.springframework.util.StringUtils;
4556
import org.springframework.web.bind.annotation.GetMapping;
4657
import org.springframework.web.bind.annotation.PostMapping;
4758
import org.springframework.web.bind.annotation.RequestBody;
4859
import org.springframework.web.bind.annotation.RequestParam;
4960
import org.springframework.web.bind.annotation.RestController;
61+
import org.springframework.web.client.RestClient;
62+
import org.springframework.web.servlet.function.HandlerFunction;
5063
import org.springframework.web.servlet.function.RouterFunction;
5164
import org.springframework.web.servlet.function.ServerResponse;
5265

5366
import static org.springframework.cloud.gateway.server.mvc.filter.FilterFunctions.adaptCachedBody;
5467
import static org.springframework.cloud.gateway.server.mvc.filter.FilterFunctions.prefixPath;
68+
import static org.springframework.cloud.gateway.server.mvc.filter.FilterFunctions.setPath;
5569
import static org.springframework.cloud.gateway.server.mvc.filter.RetryFilterFunctions.retry;
5670
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
5771
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;
@@ -93,6 +107,17 @@ public void retryBodyWorks() {
93107
.isEqualTo("3");
94108
}
95109

110+
@Test
111+
public void retryWorksWithHttpComponentsClient() {
112+
restClient.get()
113+
.uri("/retrywithhttpcomponentsclient?key=retryWorksWithHttpComponentsClient")
114+
.exchange()
115+
.expectStatus()
116+
.isOk()
117+
.expectBody(String.class)
118+
.isEqualTo("3");
119+
}
120+
96121
@SpringBootConfiguration
97122
@EnableAutoConfiguration
98123
@LoadBalancerClient(name = "httpbin", configuration = TestLoadBalancerConfig.Httpbin.class)
@@ -110,6 +135,41 @@ public RouterFunction<ServerResponse> gatewayRouterFunctionsRetry() {
110135
// @formatter:on
111136
}
112137

138+
@Bean
139+
public RouterFunction<ServerResponse> gatewayRouterFunctionsRetryWithHttpComponentsClient(
140+
GatewayMvcProperties properties,
141+
ObjectProvider<HttpHeadersFilter.RequestHttpHeadersFilter> requestHttpHeadersFilters,
142+
ObjectProvider<HttpHeadersFilter.ResponseHttpHeadersFilter> responseHttpHeadersFilters,
143+
ApplicationContext applicationContext) {
144+
145+
// build httpComponents client factory
146+
ClientHttpRequestFactory clientHttpRequestFactory = ClientHttpRequestFactoryBuilder.httpComponents()
147+
.withConnectionManagerCustomizer(builder -> builder.setMaxConnTotal(2).setMaxConnPerRoute(2))
148+
.withDefaultRequestConfigCustomizer(
149+
c -> c.setConnectionRequestTimeout(Timeout.of(Duration.ofMillis(3000))))
150+
.build();
151+
152+
// build proxyExchange use httpComponents
153+
RestClient.Builder restClientBuilder = RestClient.builder();
154+
restClientBuilder.requestFactory(clientHttpRequestFactory);
155+
ProxyExchange proxyExchange = new RestClientProxyExchange(restClientBuilder.build(), properties);
156+
157+
// build handler function use httpComponents
158+
ProxyExchangeHandlerFunction function = new ProxyExchangeHandlerFunction(proxyExchange,
159+
requestHttpHeadersFilters, responseHttpHeadersFilters);
160+
function.onApplicationEvent(new ContextRefreshedEvent(applicationContext));
161+
162+
// @formatter:off
163+
return route("testretrywithhttpcomponentsclient")
164+
.GET("/retrywithhttpcomponentsclient", function)
165+
.before(new LocalServerPortUriResolver())
166+
.filter(retry(3))
167+
.filter(setPath("/retry"))
168+
.filter(prefixPath("/do"))
169+
.build();
170+
// @formatter:on
171+
}
172+
113173
@Bean
114174
public RouterFunction<ServerResponse> gatewayRouterFunctionsRetryBody() {
115175
// @formatter:off

spring-cloud-gateway-server-mvc/src/test/resources/application.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ logging:
66
org.springframework.retry: TRACE
77
spring:
88
mvc:
9-
log-request-details: true
9+
log-request-details: true
10+
http:
11+
client:
12+
factory: REACTOR

0 commit comments

Comments
 (0)