From e21dce1cf63d6b1385f7761e4b589a7574d2fe3f Mon Sep 17 00:00:00 2001 From: Olga Maciaszek-Sharma Date: Fri, 25 Apr 2025 18:25:01 +0200 Subject: [PATCH] Add integration test. Signed-off-by: Olga Maciaszek-Sharma --- ...ServerMvcLoadBalancerIntegrationTests.java | 62 +++++++++++++++++++ .../src/test/resources/application-lb.yml | 11 ++++ 2 files changed, 73 insertions(+) create mode 100644 spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/ServerMvcLoadBalancerIntegrationTests.java create mode 100644 spring-cloud-gateway-server-mvc/src/test/resources/application-lb.yml diff --git a/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/ServerMvcLoadBalancerIntegrationTests.java b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/ServerMvcLoadBalancerIntegrationTests.java new file mode 100644 index 00000000..f0b91066 --- /dev/null +++ b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/ServerMvcLoadBalancerIntegrationTests.java @@ -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 { + } +} diff --git a/spring-cloud-gateway-server-mvc/src/test/resources/application-lb.yml b/spring-cloud-gateway-server-mvc/src/test/resources/application-lb.yml new file mode 100644 index 00000000..9d0437b3 --- /dev/null +++ b/spring-cloud-gateway-server-mvc/src/test/resources/application-lb.yml @@ -0,0 +1,11 @@ +spring: + cloud: + gateway: + mvc: + routes: + - id: test + uri: lb://httpbin + predicates: + - Path=/test/** + filters: + - StripPrefix=1 \ No newline at end of file