Browse Source

Adds AfterFilterFunctions.removeResponseHeader()

See gh-2949
pull/3006/head
sgibb 1 year ago
parent
commit
89ef7dfd7c
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 7
      spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/AfterFilterFunctions.java
  2. 23
      spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/ServerMvcIntegrationTests.java

7
spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/AfterFilterFunctions.java

@ -36,6 +36,13 @@ public interface AfterFilterFunctions { @@ -36,6 +36,13 @@ public interface AfterFilterFunctions {
};
}
static BiFunction<ServerRequest, ServerResponse, ServerResponse> removeResponseHeader(String name) {
return (request, response) -> {
response.headers().remove(name);
return response;
};
}
static BiFunction<ServerRequest, ServerResponse, ServerResponse> setResponseHeader(String name, String value) {
return (request, response) -> {
String expandedValue = MvcUtils.expand(request, value);

23
spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/ServerMvcIntegrationTests.java

@ -70,6 +70,7 @@ import org.springframework.web.servlet.function.ServerResponse; @@ -70,6 +70,7 @@ import org.springframework.web.servlet.function.ServerResponse;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.cloud.gateway.server.mvc.filter.AfterFilterFunctions.addResponseHeader;
import static org.springframework.cloud.gateway.server.mvc.filter.AfterFilterFunctions.removeResponseHeader;
import static org.springframework.cloud.gateway.server.mvc.filter.AfterFilterFunctions.setResponseHeader;
import static org.springframework.cloud.gateway.server.mvc.filter.AfterFilterFunctions.setStatus;
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.preserveHost;
@ -426,6 +427,15 @@ public class ServerMvcIntegrationTests { @@ -426,6 +427,15 @@ public class ServerMvcIntegrationTests {
});
}
@Test
public void removeResponseHeaderWorks() {
restClient.get().uri("/anything/removeresponseheader").header("test", "removeresponseheader").exchange()
.expectStatus().isOk().expectBody(Map.class).consumeWith(res -> {
HttpHeaders headers = res.getResponseHeaders();
assertThat(headers).doesNotContainKey("X-Test");
});
}
@SpringBootConfiguration
@EnableAutoConfiguration
@LoadBalancerClient(name = "httpbin", configuration = TestLoadBalancerConfig.Httpbin.class)
@ -734,6 +744,19 @@ public class ServerMvcIntegrationTests { @@ -734,6 +744,19 @@ public class ServerMvcIntegrationTests {
// @formatter:on
}
@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsRemoveResponseHeader() {
// @formatter:off
return route("testremoveresponseheader")
.GET("/anything/removeresponseheader", header("test", "removeresponseheader"), http())
.before(new HttpbinUriResolver())
// reverse order for "post" filters
.after(removeResponseHeader("X-Test"))
.after(addResponseHeader("X-Test", "value1"))
.build();
// @formatter:on
}
}
@RestController

Loading…
Cancel
Save