11/*
2- * Copyright 2013-2020 the original author or authors.
2+ * Copyright 2013-2025 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.
1616
1717package org .springframework .cloud .gateway .filter ;
1818
19+ import java .util .Collections ;
20+ import java .util .HashMap ;
21+
22+ import io .netty .channel .ChannelOption ;
1923import org .hamcrest .Matchers ;
2024import org .junit .jupiter .api .BeforeAll ;
2125import org .junit .jupiter .api .Disabled ;
2226import org .junit .jupiter .api .Test ;
2327import reactor .core .publisher .Mono ;
2428import reactor .netty .DisposableServer ;
29+ import reactor .netty .http .client .HttpClient ;
30+ import reactor .netty .http .client .HttpClientConfig ;
2531import reactor .netty .http .server .HttpServer ;
2632
2733import org .springframework .beans .factory .annotation .Autowired ;
2834import org .springframework .boot .SpringBootConfiguration ;
2935import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
3036import org .springframework .boot .test .context .SpringBootTest ;
37+ import org .springframework .cloud .context .environment .EnvironmentChangeEvent ;
38+ import org .springframework .cloud .gateway .route .Route ;
3139import org .springframework .cloud .gateway .route .RouteLocator ;
3240import org .springframework .cloud .gateway .route .builder .RouteLocatorBuilder ;
3341import org .springframework .cloud .gateway .test .BaseWebClientTests ;
3442import org .springframework .cloud .gateway .test .PermitAllSecurityConfiguration ;
3543import org .springframework .context .ApplicationContext ;
3644import org .springframework .context .annotation .Bean ;
3745import org .springframework .context .annotation .Import ;
46+ import org .springframework .core .env .ConfigurableEnvironment ;
47+ import org .springframework .core .env .MapPropertySource ;
3848import org .springframework .test .util .TestSocketUtils ;
3949import org .springframework .test .web .reactive .server .WebTestClient ;
4050
@@ -48,6 +58,12 @@ class NettyRoutingFilterTests extends BaseWebClientTests {
4858 @ Autowired
4959 private ApplicationContext context ;
5060
61+ @ Autowired
62+ private NettyRoutingFilter nettyRoutingFilter ;
63+
64+ @ Autowired
65+ private RouteLocator routeLocator ;
66+
5167 @ BeforeAll
5268 public static void beforeAll () {
5369 port = TestSocketUtils .findAvailableTcpPort ();
@@ -88,6 +104,30 @@ void testCaseInsensitiveScheme() {
88104 }
89105 }
90106
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+
91131 @ SpringBootConfiguration
92132 @ EnableAutoConfiguration
93133 @ Import (PermitAllSecurityConfiguration .class )
@@ -98,6 +138,7 @@ public RouteLocator routes(RouteLocatorBuilder builder) {
98138 return builder .routes ()
99139 .route (p -> p .path ("/mockexample" ).filters (f -> f .prefixPath ("/httpbin" )).uri ("http://example.com" ))
100140 .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" ))
101142 .build ();
102143 }
103144
0 commit comments