diff --git a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/RestClientProxyExchange.java b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/RestClientProxyExchange.java index 600181486f..46e9d0326b 100644 --- a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/RestClientProxyExchange.java +++ b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/RestClientProxyExchange.java @@ -16,7 +16,6 @@ package org.springframework.cloud.gateway.server.mvc.handler; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -70,15 +69,9 @@ private static int copyBody(Request request, OutputStream outputStream) throws I } private ServerResponse doExchange(Request request, ClientHttpResponse clientResponse) throws IOException { - try { - InputStream body = clientResponse.getBody(); - // put the body input stream in a request attribute so filters can read it. - MvcUtils.putAttribute(request.getServerRequest(), MvcUtils.CLIENT_RESPONSE_INPUT_STREAM_ATTR, body); - } - catch (FileNotFoundException e) { - // if using SimpleClientHttpRequestFactory - return ServerResponse.notFound().build(); - } + InputStream body = clientResponse.getBody(); + // put the body input stream in a request attribute so filters can read it. + MvcUtils.putAttribute(request.getServerRequest(), MvcUtils.CLIENT_RESPONSE_INPUT_STREAM_ATTR, body); ServerResponse serverResponse = GatewayServerResponse.status(clientResponse.getStatusCode()) .build((req, httpServletResponse) -> { try (clientResponse) { diff --git a/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/ServerMvcIntegrationTests.java b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/ServerMvcIntegrationTests.java index 6ceffe35da..d9a7dc7bb7 100644 --- a/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/ServerMvcIntegrationTests.java +++ b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/ServerMvcIntegrationTests.java @@ -275,12 +275,12 @@ public void stripPrefixPostWorks() { public void setStatusGatewayRouterFunctionWorks() { restClient.get() .uri("/status/201") - .header("Host", "www.setstatus.org") .exchange() .expectStatus() .isEqualTo(HttpStatus.TOO_MANY_REQUESTS) .expectHeader() - .valueEquals("x-status", "201"); + .valueEquals("x-status", "201"); // .expectBody(String.class).isEqualTo("Failed + // with 201"); } @Test @@ -981,11 +981,6 @@ public void clientResponseBodyAttributeWorks() { }); } - @Test - public void notFoundWorks() { - restClient.get().uri("/status/404").header("Host", "www.notfound.org").exchange().expectStatus().isNotFound(); - } - @SpringBootConfiguration @EnableAutoConfiguration @LoadBalancerClient(name = "httpbin", configuration = TestLoadBalancerConfig.Httpbin.class) @@ -1037,7 +1032,7 @@ public RouterFunction gatewayRouterFunctionsAddReqHeader() { public RouterFunction gatewayRouterFunctionsSetStatusAndAddRespHeader() { // @formatter:off return route("testsetstatus") - .GET("/status/{status}", host("**.setstatus.org"), http()) + .GET("/status/{status}", http()) .before(new HttpbinUriResolver()) .after(setStatus(HttpStatus.TOO_MANY_REQUESTS)) .after(addResponseHeader("X-Status", "{status}")) @@ -1605,16 +1600,6 @@ public RouterFunction gatewayRouterFunctionsReadResponseBody() { // @formatter:on } - @Bean - public RouterFunction gatewayRouterFunctions404() { - // @formatter:off - return route("testnotfound") - .GET("/status/404", host("**.notfound.org"), http()) - .before(new HttpbinUriResolver()) - .build(); - // @formatter:on - } - @Bean public FilterRegistrationBean myFilter() { FilterRegistrationBean reg = new FilterRegistrationBean<>(new MyFilter()); diff --git a/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/SimpleHttpClientIntegrationTests.java b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/SimpleHttpClientIntegrationTests.java deleted file mode 100644 index bd42c04eec..0000000000 --- a/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/SimpleHttpClientIntegrationTests.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2013-2023 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.SpringBootConfiguration; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.web.server.LocalServerPort; -import org.springframework.cloud.gateway.server.mvc.test.HttpbinTestcontainers; -import org.springframework.cloud.gateway.server.mvc.test.HttpbinUriResolver; -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.context.annotation.Bean; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.web.servlet.function.RouterFunction; -import org.springframework.web.servlet.function.ServerResponse; - -import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route; -import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http; -import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.host; - -@SuppressWarnings("unchecked") -@SpringBootTest(properties = { "spring.cloud.gateway.mvc.http-client.type=autodetect" }, - webEnvironment = WebEnvironment.RANDOM_PORT) -@ContextConfiguration(initializers = HttpbinTestcontainers.class) -public class SimpleHttpClientIntegrationTests { - - static { - // if set type to autodetect above - System.setProperty("sun.net.http.allowRestrictedHeaders", "true"); - } - - @LocalServerPort - int port; - - @Autowired - TestRestClient restClient; - - @Test - public void simpleHttpClientNotFoundWorks() { - restClient.get().uri("/status/404").header("Host", "www.notfound.org").exchange().expectStatus().isNotFound(); - } - - @SpringBootConfiguration - @EnableAutoConfiguration - @LoadBalancerClient(name = "httpbin", configuration = TestLoadBalancerConfig.Httpbin.class) - protected static class TestConfiguration { - - @Bean - public RouterFunction gatewayRouterFunctions404() { - // @formatter:off - return route("testnotfound") - .GET("/status/404", host("**.notfound.org"), http()) - .before(new HttpbinUriResolver()) - .build(); - // @formatter:on - } - - } - -}