Add tests for LoadBalancerHandlerConfiguration (#3779)

* Add autoconfiguration tests.

Signed-off-by: Olga Maciaszek-Sharma <olga.maciaszek-sharma@broadcom.com>
This commit is contained in:
Olga Maciaszek-Sharma
2025-05-20 17:39:25 +02:00
committed by GitHub
parent d12a5873f4
commit b5afd8883d
3 changed files with 97 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2024 the original author or authors.
* Copyright 2013-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@ import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings;
import org.springframework.boot.http.client.SimpleClientHttpRequestFactoryBuilder;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.gateway.server.mvc.filter.FilterAutoConfiguration;
import org.springframework.cloud.gateway.server.mvc.filter.FormFilter;
@@ -47,6 +48,7 @@ import org.springframework.cloud.gateway.server.mvc.filter.WeightCalculatorFilte
import org.springframework.cloud.gateway.server.mvc.filter.XForwardedRequestHeadersFilter;
import org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctionAutoConfiguration;
import org.springframework.cloud.gateway.server.mvc.predicate.PredicateAutoConfiguration;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
import org.springframework.context.ConfigurableApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
@@ -204,6 +206,27 @@ public class GatewayServerMvcAutoConfigurationTests {
assertThat(builder).isInstanceOf(SimpleClientHttpRequestFactoryBuilder.class);
}
@Test
void loadBalancerFunctionHandlerAdded() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(FilterAutoConfiguration.class, PredicateAutoConfiguration.class,
HandlerFunctionAutoConfiguration.class, GatewayServerMvcAutoConfiguration.class,
HttpClientAutoConfiguration.class, RestTemplateAutoConfiguration.class,
RestClientAutoConfiguration.class))
.run(context -> assertThat(context).hasBean("lbHandlerFunctionDefinition"));
}
@Test
void loadBalancerFunctionHandlerNotAddedWhenNoLoadBalancerClientOnClasspath() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(FilterAutoConfiguration.class, PredicateAutoConfiguration.class,
HandlerFunctionAutoConfiguration.class, GatewayServerMvcAutoConfiguration.class,
HttpClientAutoConfiguration.class, RestTemplateAutoConfiguration.class,
RestClientAutoConfiguration.class))
.withClassLoader(new FilteredClassLoader(LoadBalancerClient.class))
.run(context -> assertThat(context).doesNotHaveBean("lbHandlerFunctionDefinition"));
}
@SpringBootConfiguration
@EnableAutoConfiguration
static class TestConfig {

View File

@@ -0,0 +1,62 @@
/*
* Copyright 2013-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.gateway.server.mvc;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.cloud.gateway.server.mvc.filter.FilterAutoConfiguration;
import org.springframework.cloud.gateway.server.mvc.test.HttpbinTestcontainers;
import org.springframework.cloud.gateway.server.mvc.test.TestLoadBalancerConfig;
import org.springframework.cloud.gateway.server.mvc.test.client.TestRestClient;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
/**
* Integration tests for {@link FilterAutoConfiguration.LoadBalancerHandlerConfiguration}.
*
* @author Olga Maciaszek-Sharma
*
*/
@SpringBootTest(classes = {ServerMvcLoadBalancerIntegrationTests.Config.class, FilterAutoConfiguration.class},
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(initializers = HttpbinTestcontainers.class)
@ActiveProfiles("lb")
public class ServerMvcLoadBalancerIntegrationTests {
@LocalServerPort
int port;
@Autowired
TestRestClient testRestClient;
@Test
void shouldUseLbHandlerFunctionDefinitionToResolveHost() {
testRestClient.get()
.uri("http://localhost:" + port + "/test")
.exchange().expectStatus().isOk();
}
@SpringBootApplication
@LoadBalancerClient(name = "httpbin", configuration = TestLoadBalancerConfig.Httpbin.class)
static class Config {
}
}

View File

@@ -0,0 +1,11 @@
spring:
cloud:
gateway:
mvc:
routes:
- id: test
uri: lb://httpbin
predicates:
- Path=/test/**
filters:
- StripPrefix=1