|
| 1 | +/* |
| 2 | + * Copyright 2013-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.gateway.server.mvc; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import org.springframework.beans.factory.annotation.Autowired; |
| 22 | +import org.springframework.boot.SpringBootConfiguration; |
| 23 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 24 | +import org.springframework.boot.test.context.SpringBootTest; |
| 25 | +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
| 26 | +import org.springframework.boot.test.web.server.LocalServerPort; |
| 27 | +import org.springframework.cloud.gateway.server.mvc.test.HttpbinTestcontainers; |
| 28 | +import org.springframework.cloud.gateway.server.mvc.test.HttpbinUriResolver; |
| 29 | +import org.springframework.cloud.gateway.server.mvc.test.TestLoadBalancerConfig; |
| 30 | +import org.springframework.cloud.gateway.server.mvc.test.client.TestRestClient; |
| 31 | +import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient; |
| 32 | +import org.springframework.context.annotation.Bean; |
| 33 | +import org.springframework.test.context.ContextConfiguration; |
| 34 | +import org.springframework.web.servlet.function.RouterFunction; |
| 35 | +import org.springframework.web.servlet.function.ServerResponse; |
| 36 | + |
| 37 | +import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route; |
| 38 | +import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http; |
| 39 | +import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.host; |
| 40 | + |
| 41 | +@SuppressWarnings("unchecked") |
| 42 | +@SpringBootTest(properties = { "spring.cloud.gateway.mvc.http-client.type=autodetect" }, |
| 43 | + webEnvironment = WebEnvironment.RANDOM_PORT) |
| 44 | +@ContextConfiguration(initializers = HttpbinTestcontainers.class) |
| 45 | +public class SimpleHttpClientIntegrationTests { |
| 46 | + |
| 47 | + static { |
| 48 | + // if set type to autodetect above |
| 49 | + System.setProperty("sun.net.http.allowRestrictedHeaders", "true"); |
| 50 | + } |
| 51 | + |
| 52 | + @LocalServerPort |
| 53 | + int port; |
| 54 | + |
| 55 | + @Autowired |
| 56 | + TestRestClient restClient; |
| 57 | + |
| 58 | + @Test |
| 59 | + public void simpleHttpClientNotFoundWorks() { |
| 60 | + restClient.get().uri("/status/404").header("Host", "www.notfound.org").exchange().expectStatus().isNotFound(); |
| 61 | + } |
| 62 | + |
| 63 | + @SpringBootConfiguration |
| 64 | + @EnableAutoConfiguration |
| 65 | + @LoadBalancerClient(name = "httpbin", configuration = TestLoadBalancerConfig.Httpbin.class) |
| 66 | + protected static class TestConfiguration { |
| 67 | + |
| 68 | + @Bean |
| 69 | + public RouterFunction<ServerResponse> gatewayRouterFunctions404() { |
| 70 | + // @formatter:off |
| 71 | + return route("testnotfound") |
| 72 | + .GET("/status/404", host("**.notfound.org"), http()) |
| 73 | + .before(new HttpbinUriResolver()) |
| 74 | + .build(); |
| 75 | + // @formatter:on |
| 76 | + } |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments