1
1
/*
2
- * Copyright 2013-2020 the original author or authors.
2
+ * Copyright 2013-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .cloud .gateway .filter ;
18
18
19
+ import java .util .Collections ;
20
+ import java .util .HashMap ;
21
+
22
+ import io .netty .channel .ChannelOption ;
19
23
import org .hamcrest .Matchers ;
20
24
import org .junit .jupiter .api .BeforeAll ;
21
25
import org .junit .jupiter .api .Disabled ;
22
26
import org .junit .jupiter .api .Test ;
23
27
import reactor .core .publisher .Mono ;
24
28
import reactor .netty .DisposableServer ;
29
+ import reactor .netty .http .client .HttpClient ;
30
+ import reactor .netty .http .client .HttpClientConfig ;
25
31
import reactor .netty .http .server .HttpServer ;
26
32
27
33
import org .springframework .beans .factory .annotation .Autowired ;
28
34
import org .springframework .boot .SpringBootConfiguration ;
29
35
import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
30
36
import org .springframework .boot .test .context .SpringBootTest ;
37
+ import org .springframework .cloud .context .environment .EnvironmentChangeEvent ;
38
+ import org .springframework .cloud .gateway .route .Route ;
31
39
import org .springframework .cloud .gateway .route .RouteLocator ;
32
40
import org .springframework .cloud .gateway .route .builder .RouteLocatorBuilder ;
33
41
import org .springframework .cloud .gateway .test .BaseWebClientTests ;
34
42
import org .springframework .cloud .gateway .test .PermitAllSecurityConfiguration ;
35
43
import org .springframework .context .ApplicationContext ;
36
44
import org .springframework .context .annotation .Bean ;
37
45
import org .springframework .context .annotation .Import ;
46
+ import org .springframework .core .env .ConfigurableEnvironment ;
47
+ import org .springframework .core .env .MapPropertySource ;
38
48
import org .springframework .test .util .TestSocketUtils ;
39
49
import org .springframework .test .web .reactive .server .WebTestClient ;
40
50
@@ -48,6 +58,12 @@ class NettyRoutingFilterTests extends BaseWebClientTests {
48
58
@ Autowired
49
59
private ApplicationContext context ;
50
60
61
+ @ Autowired
62
+ private NettyRoutingFilter nettyRoutingFilter ;
63
+
64
+ @ Autowired
65
+ private RouteLocator routeLocator ;
66
+
51
67
@ BeforeAll
52
68
public static void beforeAll () {
53
69
port = TestSocketUtils .findAvailableTcpPort ();
@@ -88,6 +104,30 @@ void testCaseInsensitiveScheme() {
88
104
}
89
105
}
90
106
107
+ @ Test
108
+ void testConnectTimeoutConfigurationReloadWorks () {
109
+ ConfigurableEnvironment environment = (ConfigurableEnvironment ) this .context .getEnvironment ();
110
+ HashMap <String , Object > map = new HashMap <>();
111
+ environment .getPropertySources ().addFirst (new MapPropertySource ("mock" , map ));
112
+ map .put ("spring.cloud.gateway.httpclient.connect-timeout" , "1000" );
113
+ this .context .publishEvent (new EnvironmentChangeEvent (this .context ,
114
+ Collections .singleton ("spring.cloud.gateway.httpclient.connect-timeout" )));
115
+ Route route = routeLocator .getRoutes ()
116
+ .filter (r -> r .getId ().equals ("refreshable_configuration_test" )).blockLast ();
117
+ assertThat (route ).isNotNull ();
118
+ HttpClient httpClient = nettyRoutingFilter .getHttpClient (route , null );
119
+ HttpClientConfig configuration = httpClient .configuration ();
120
+ assertThat (configuration .options ().get (ChannelOption .CONNECT_TIMEOUT_MILLIS )).isEqualTo (1000 );
121
+
122
+ map .put ("spring.cloud.gateway.httpclient.connect-timeout" , "5000" );
123
+ this .context .publishEvent (new EnvironmentChangeEvent (this .context ,
124
+ Collections .singleton ("spring.cloud.gateway.httpclient.connect-timeout" )));
125
+ httpClient = nettyRoutingFilter .getHttpClient (route , null );
126
+ configuration = httpClient .configuration ();
127
+ assertThat (configuration .options ().get (ChannelOption .CONNECT_TIMEOUT_MILLIS )).isEqualTo (5000 );
128
+ environment .getPropertySources ().remove ("mock" );
129
+ }
130
+
91
131
@ SpringBootConfiguration
92
132
@ EnableAutoConfiguration
93
133
@ Import (PermitAllSecurityConfiguration .class )
@@ -98,6 +138,7 @@ public RouteLocator routes(RouteLocatorBuilder builder) {
98
138
return builder .routes ()
99
139
.route (p -> p .path ("/mockexample" ).filters (f -> f .prefixPath ("/httpbin" )).uri ("http://example.com" ))
100
140
.route (p -> p .path ("/issue" ).uri ("HTTP://127.0.0.1:" + port ))
141
+ .route ("refreshable_configuration_test" , p -> p .path ("/refresh" ).uri ("http://example.com" ))
101
142
.build ();
102
143
}
103
144
0 commit comments