Skip to content

Commit

Permalink
Merge branch '4.1.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
spencergibb committed Oct 28, 2024
2 parents bd980af + 57c0258 commit c444594
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.cloud.gateway.server.mvc.handler;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -69,9 +70,15 @@ private static int copyBody(Request request, OutputStream outputStream) throws I
}

private ServerResponse doExchange(Request request, ClientHttpResponse clientResponse) throws IOException {
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);
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();
}
ServerResponse serverResponse = GatewayServerResponse.status(clientResponse.getStatusCode())
.build((req, httpServletResponse) -> {
try (clientResponse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"); // .expectBody(String.class).isEqualTo("Failed
// with 201");
.valueEquals("x-status", "201");
}

@Test
Expand Down Expand Up @@ -981,6 +981,11 @@ 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)
Expand Down Expand Up @@ -1032,7 +1037,7 @@ public RouterFunction<ServerResponse> gatewayRouterFunctionsAddReqHeader() {
public RouterFunction<ServerResponse> gatewayRouterFunctionsSetStatusAndAddRespHeader() {
// @formatter:off
return route("testsetstatus")
.GET("/status/{status}", http())
.GET("/status/{status}", host("**.setstatus.org"), http())
.before(new HttpbinUriResolver())
.after(setStatus(HttpStatus.TOO_MANY_REQUESTS))
.after(addResponseHeader("X-Status", "{status}"))
Expand Down Expand Up @@ -1600,6 +1605,16 @@ public RouterFunction<ServerResponse> gatewayRouterFunctionsReadResponseBody() {
// @formatter:on
}

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctions404() {
// @formatter:off
return route("testnotfound")
.GET("/status/404", host("**.notfound.org"), http())
.before(new HttpbinUriResolver())
.build();
// @formatter:on
}

@Bean
public FilterRegistrationBean myFilter() {
FilterRegistrationBean<MyFilter> reg = new FilterRegistrationBean<>(new MyFilter());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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<ServerResponse> gatewayRouterFunctions404() {
// @formatter:off
return route("testnotfound")
.GET("/status/404", host("**.notfound.org"), http())
.before(new HttpbinUriResolver())
.build();
// @formatter:on
}

}

}

0 comments on commit c444594

Please sign in to comment.