[BAEL-3313] spring-cloud/spring-cloud-gateway | Writing custom Spring Cloud Gateway Filters (#8182)

* updated dependency management in spring-cloud-gateway pom.xml

* * renamed org package to com
* renamed spring.cloud package to springcloudgateway
* deleted SpringContextIntegrationTest, as per BAEL-14304
* updated spring Junit test to jupiter

* separated introduction-application properties from application.yml, fixing launch error due to file not found exception

* Added Service to use as Proxied Service

* Added global filters and debug logs for article

* fixed error in properties source, plus added GatewayFilter Factories

* implemented Modify Request example

* implemented Modify Response example

* implemented Chain Request example

* Added Tests:
* Live Test for gateway
* Integration tests for services
Fixed small issues

* renamed tests that were not following BDD naming
This commit is contained in:
Ger Roza 2019-11-22 23:42:56 -03:00 committed by maibin
parent b1971384e2
commit 7ee5019f7e
31 changed files with 805 additions and 83 deletions

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-gateway</artifactId>
@ -17,8 +18,25 @@
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gateway</artifactId>
<version>${cloud.version}</version>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Junit bom upgrade needed only because of a bug with STS,
should be removed with Spring Boot 2.2.0 -->
<!-- https://github.com/spring-projects/sts4/issues/371 -->
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.5.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
@ -28,20 +46,7 @@
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
@ -54,13 +59,31 @@
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor.ipc</groupId>
<artifactId>reactor-netty</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<properties>
<cloud.version>2.0.1.RELEASE</cloud.version>
<spring-cloud-dependencies.version>Greenwich.SR3</spring-cloud-dependencies.version>
<!-- Spring Boot version compatible with Spring Cloud Release train -->
<spring-boot.version>2.1.9.RELEASE</spring-boot.version>
<hibernate-validator.version>6.0.2.Final</hibernate-validator.version>
</properties>

View File

@ -0,0 +1,15 @@
package com.baeldung.secondservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@PropertySource("classpath:secondservice-application.properties")
public class SecondServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SecondServiceApplication.class, args);
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.secondservice.web;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
@RestController
public class SecondServiceRestController {
@GetMapping("/resource/language")
public Mono<ResponseEntity<String>> getResource() {
return Mono.just(ResponseEntity.ok()
.body("es"));
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@PropertySource("classpath:service-application.properties")
public class ServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.service.web;
import java.util.Locale;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
@RestController
public class ServiceRestController {
@GetMapping("/resource")
public Mono<ResponseEntity<String>> getResource() {
return Mono.just(ResponseEntity.ok()
.header(HttpHeaders.CONTENT_LANGUAGE, Locale.ENGLISH.getLanguage())
.body("Service Resource"));
}
}

View File

@ -1,13 +0,0 @@
package com.baeldung.spring.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.springcloudgateway.customfilters;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@PropertySource("classpath:customfilters-global-application.properties")
public class CustomFiltersGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(CustomFiltersGatewayApplication.class, args);
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.springcloudgateway.customfilters.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
@Configuration
public class WebClientConfig {
@Bean
WebClient client() {
return WebClient.builder()
.build();
}
}

View File

@ -0,0 +1,90 @@
package com.baeldung.springcloudgateway.customfilters.filters.factories;
import java.util.Arrays;
import java.util.List;
import java.util.Locale.LanguageRange;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
@Component
public class ChainRequestGatewayFilterFactory extends AbstractGatewayFilterFactory<ChainRequestGatewayFilterFactory.Config> {
final Logger logger = LoggerFactory.getLogger(ChainRequestGatewayFilterFactory.class);
private final WebClient client;
public ChainRequestGatewayFilterFactory(WebClient client) {
super(Config.class);
this.client = client;
}
@Override
public List<String> shortcutFieldOrder() {
return Arrays.asList("endpoint", "defaultLanguage");
}
@Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> {
return client.get()
.uri(config.getEndpoint())
.exchange()
.flatMap(response -> {
return (response.statusCode()
.is2xxSuccessful()) ? response.bodyToMono(String.class) : Mono.just(config.getDefaultLanguage());
})
.map(LanguageRange::parse)
.map(range -> {
exchange.getRequest()
.mutate()
.headers(h -> h.setAcceptLanguage(range))
.build();
String allOutgoingRequestLanguages = exchange.getRequest()
.getHeaders()
.getAcceptLanguage()
.stream()
.map(r -> r.getRange())
.collect(Collectors.joining(","));
logger.info("Chain Request output - Request contains Accept-Language header: " + allOutgoingRequestLanguages);
return exchange;
})
.flatMap(chain::filter);
};
}
public static class Config {
private String endpoint;
private String defaultLanguage;
public Config() {
}
public String getEndpoint() {
return endpoint;
}
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
public String getDefaultLanguage() {
return defaultLanguage;
}
public void setDefaultLanguage(String defaultLanguage) {
this.defaultLanguage = defaultLanguage;
}
}
}

View File

@ -0,0 +1,85 @@
package com.baeldung.springcloudgateway.customfilters.filters.factories;
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.OrderedGatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
@Component
public class LoggingGatewayFilterFactory extends AbstractGatewayFilterFactory<LoggingGatewayFilterFactory.Config> {
final Logger logger = LoggerFactory.getLogger(LoggingGatewayFilterFactory.class);
public static final String BASE_MSG = "baseMessage";
public static final String PRE_LOGGER = "preLogger";
public static final String POST_LOGGER = "postLogger";
public LoggingGatewayFilterFactory() {
super(Config.class);
}
@Override
public List<String> shortcutFieldOrder() {
return Arrays.asList(BASE_MSG, PRE_LOGGER, POST_LOGGER);
}
@Override
public GatewayFilter apply(Config config) {
return new OrderedGatewayFilter((exchange, chain) -> {
if (config.isPreLogger())
logger.info("Pre GatewayFilter logging: " + config.getBaseMessage());
return chain.filter(exchange)
.then(Mono.fromRunnable(() -> {
if (config.isPostLogger())
logger.info("Post GatewayFilter logging: " + config.getBaseMessage());
}));
}, -2);
}
public static class Config {
private String baseMessage;
private boolean preLogger;
private boolean postLogger;
public Config() {
};
public Config(String baseMessage, boolean preLogger, boolean postLogger) {
super();
this.baseMessage = baseMessage;
this.preLogger = preLogger;
this.postLogger = postLogger;
}
public String getBaseMessage() {
return this.baseMessage;
}
public boolean isPreLogger() {
return preLogger;
}
public boolean isPostLogger() {
return postLogger;
}
public void setBaseMessage(String baseMessage) {
this.baseMessage = baseMessage;
}
public void setPreLogger(boolean preLogger) {
this.preLogger = preLogger;
}
public void setPostLogger(boolean postLogger) {
this.postLogger = postLogger;
}
}
}

View File

@ -0,0 +1,78 @@
package com.baeldung.springcloudgateway.customfilters.filters.factories;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
@Component
public class ModifyRequestGatewayFilterFactory extends AbstractGatewayFilterFactory<ModifyRequestGatewayFilterFactory.Config> {
final Logger logger = LoggerFactory.getLogger(ModifyRequestGatewayFilterFactory.class);
public ModifyRequestGatewayFilterFactory() {
super(Config.class);
}
@Override
public List<String> shortcutFieldOrder() {
return Arrays.asList("defaultLocale");
}
@Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> {
if (exchange.getRequest()
.getHeaders()
.getAcceptLanguage()
.isEmpty()) {
String queryParamLocale = exchange.getRequest()
.getQueryParams()
.getFirst("locale");
Locale requestLocale = Optional.ofNullable(queryParamLocale)
.map(l -> Locale.forLanguageTag(l))
.orElse(config.getDefaultLocale());
exchange.getRequest()
.mutate()
.headers(h -> h.setAcceptLanguageAsLocales(Collections.singletonList(requestLocale)))
.build();
}
String allOutgoingRequestLanguages = exchange.getRequest()
.getHeaders()
.getAcceptLanguage()
.stream()
.map(range -> range.getRange())
.collect(Collectors.joining(","));
logger.info("Modify Request output - Request contains Accept-Language header: " + allOutgoingRequestLanguages);
return chain.filter(exchange);
};
}
public static class Config {
private Locale defaultLocale;
public Config() {
}
public Locale getDefaultLocale() {
return defaultLocale;
}
public void setDefaultLocale(String defaultLocale) {
this.defaultLocale = Locale.forLanguageTag(defaultLocale);
};
}
}

View File

@ -0,0 +1,48 @@
package com.baeldung.springcloudgateway.customfilters.filters.factories;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
@Component
public class ModifyResponseGatewayFilterFactory extends AbstractGatewayFilterFactory<ModifyResponseGatewayFilterFactory.Config> {
final Logger logger = LoggerFactory.getLogger(ModifyResponseGatewayFilterFactory.class);
public ModifyResponseGatewayFilterFactory() {
super(Config.class);
}
@Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> {
return chain.filter(exchange)
.then(Mono.fromRunnable(() -> {
ServerHttpResponse response = exchange.getResponse();
Optional.ofNullable(exchange.getRequest()
.getQueryParams()
.getFirst("locale"))
.ifPresent(qp -> {
String responseContentLanguage = response.getHeaders()
.getContentLanguage()
.getLanguage();
response.getHeaders()
.add("Bael-Custom-Language-Header", responseContentLanguage);
logger.info("Added custom header to Response");
});
}));
};
}
public static class Config {
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.springcloudgateway.customfilters.filters.global;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import reactor.core.publisher.Mono;
@Configuration
public class LoggingGlobalFiltersConfigurations {
final Logger logger = LoggerFactory.getLogger(LoggingGlobalFiltersConfigurations.class);
@Bean
public GlobalFilter postGlobalFilter() {
return (exchange, chain) -> {
return chain.filter(exchange)
.then(Mono.fromRunnable(() -> {
logger.info("Global Post Filter executed");
}));
};
}
@Bean
@Order(-1)
public GlobalFilter FirstPreLastPostGlobalFilter() {
return (exchange, chain) -> {
logger.info("First Pre Global Filter");
return chain.filter(exchange)
.then(Mono.fromRunnable(() -> {
logger.info("Last Post Global Filter");
}));
};
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.springcloudgateway.customfilters.filters.global;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
@Component
public class LoggingGlobalPreFilter implements GlobalFilter, Ordered {
final Logger logger = LoggerFactory.getLogger(LoggingGlobalPreFilter.class);
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
logger.info("Global Pre Filter executed");
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 0;
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.springcloudgateway.customfilters.routes;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import com.baeldung.springcloudgateway.customfilters.filters.factories.LoggingGatewayFilterFactory;
import com.baeldung.springcloudgateway.customfilters.filters.factories.LoggingGatewayFilterFactory.Config;
/**
* Note: We want to keep this as an example of configuring a Route with a custom filter
*
* This corresponds with the properties configuration we have
*/
// @Configuration
public class ServiceRouteConfiguration {
@Bean
public RouteLocator routes(RouteLocatorBuilder builder, LoggingGatewayFilterFactory loggingFactory) {
return builder.routes()
.route("service_route_java_config", r -> r.path("/service/**")
.filters(f -> f.rewritePath("/service(?<segment>/?.*)", "$\\{segment}")
.filter(loggingFactory.apply(new Config("My Custom Message", true, true))))
.uri("http://localhost:8081"))
.build();
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.springcloudgateway.introduction;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@PropertySource("classpath:introduction-application.properties")
public class IntroductionGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(IntroductionGatewayApplication.class, args);
}
}

View File

@ -1,16 +1,4 @@
server:
port: 80
spring:
cloud:
gateway:
routes:
- id: baeldung_route
uri: http://www.baeldung.com
predicates:
- Path=/baeldung
management:
endpoints:
web:
exposure:
include: "*"
logging:
level:
org.springframework.cloud.gateway: DEBUG
reactor.netty.http.client: DEBUG

View File

@ -0,0 +1,19 @@
spring.cloud.gateway.routes[0].id=service_route
spring.cloud.gateway.routes[0].uri=http://localhost:8081
spring.cloud.gateway.routes[0].predicates[0]=Path=/service/**
spring.cloud.gateway.routes[0].filters[0]=RewritePath=/service(?<segment>/?.*), $\{segment}
spring.cloud.gateway.routes[0].filters[1]=Logging=My Custom Message, true, true
# Or, as an alternative:
#spring.cloud.gateway.routes[0].filters[1].name=Logging
#spring.cloud.gateway.routes[0].filters[1].args[baseMessage]=My Custom Message
#spring.cloud.gateway.routes[0].filters[1].args[preLogger]=true
#spring.cloud.gateway.routes[0].filters[1].args[postLogger]=true
spring.cloud.gateway.routes[0].filters[2]=ModifyRequest=en
spring.cloud.gateway.routes[0].filters[3]=ModifyResponse
spring.cloud.gateway.routes[0].filters[4]=ChainRequest=http://localhost:8082/resource/language, fr
management.endpoints.web.exposure.include=*
server.port=80

View File

@ -0,0 +1,7 @@
spring.cloud.gateway.routes[0].id=baeldung_route
spring.cloud.gateway.routes[0].uri=http://www.baeldung.com
spring.cloud.gateway.routes[0].predicates[0]=Path=/baeldung
management.endpoints.web.exposure.include=*
server.port=80

View File

@ -0,0 +1 @@
server.port=8081

View File

@ -0,0 +1,26 @@
package com.baeldung.secondservice;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.test.web.reactive.server.WebTestClient;
import com.baeldung.secondservice.web.SecondServiceRestController;
@WebFluxTest(SecondServiceRestController.class)
public class SecondServiceIntegrationTest {
@Autowired
private WebTestClient webClient;
@Test
public void whenResourceLanguageEndpointCalled_thenRetrievesSpanishLanguageString() throws Exception {
this.webClient.get()
.uri("/resource/language")
.exchange()
.expectStatus()
.isOk()
.expectBody(String.class)
.isEqualTo("es");
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.secondservice;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest(classes = SecondServiceApplication.class)
public class SpringContextTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.service;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.http.HttpHeaders;
import org.springframework.test.web.reactive.server.WebTestClient;
import com.baeldung.service.web.ServiceRestController;
@WebFluxTest(ServiceRestController.class)
public class ServiceIntegrationTest {
@Autowired
private WebTestClient webClient;
@Test
public void whenResourceEndpointCalled_thenRetrievesResourceStringWithContentLanguageHeader() throws Exception {
this.webClient.get()
.uri("/resource")
.exchange()
.expectStatus()
.isOk()
.expectHeader()
.valueEquals(HttpHeaders.CONTENT_LANGUAGE, "en")
.expectBody(String.class)
.isEqualTo("Service Resource");
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.service;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest(classes = ServiceApplication.class)
public class SpringContextTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}

View File

@ -0,0 +1,90 @@
package com.baeldung.springcloudgateway.customfilters;
import static org.assertj.core.api.Assertions.assertThat;
import org.assertj.core.api.Condition;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.test.web.reactive.server.WebTestClient.ResponseSpec;
import com.baeldung.springcloudgateway.customfilters.utils.LoggerListAppender;
import ch.qos.logback.classic.spi.ILoggingEvent;
/**
* This test requires:
* * the service in com.baeldung.service running
* * the 'second service' in com.baeldung.secondservice running
*
*/
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class CustomFiltersLiveTest {
@LocalServerPort
String port;
private WebTestClient client;
@BeforeEach
public void clearLogList() {
LoggerListAppender.clearEventList();
client = WebTestClient.bindToServer()
.baseUrl("http://localhost:" + port)
.build();
}
@Test
public void whenCallServiceThroughGateway_thenAllConfiguredFiltersGetExecuted() {
ResponseSpec response = client.get()
.uri("/service/resource")
.exchange();
response.expectStatus()
.isOk()
.expectBody(String.class)
.isEqualTo("Service Resource");
assertThat(LoggerListAppender.getEvents())
// Global Pre Filter
.haveAtLeastOne(eventContains("Global Pre Filter executed"))
// Global Post Filter
.haveAtLeastOne(eventContains("Global Post Filter executed"))
// Global Pre and Post Filter
.haveAtLeastOne(eventContains("First Pre Global Filter"))
.haveAtLeastOne(eventContains("Last Post Global Filter"))
// Logging Filter Factory
.haveAtLeastOne(eventContains("Pre GatewayFilter logging: My Custom Message"))
.haveAtLeastOne(eventContains("Post GatewayFilter logging: My Custom Message"))
// Modify Request
.haveAtLeastOne(eventContains("Modify Request output - Request contains Accept-Language header:"))
// Modify Response
.areNot(eventContains("Added custom header to Response"))
// Chain Request
.haveAtLeastOne(eventContains("Chain Request output - Request contains Accept-Language header:"));
}
@Test
public void givenRequestWithLocaleQueryParam_whenCallServiceThroughGateway_thenAllConfiguredFiltersGetExecuted() {
ResponseSpec response = client.get()
.uri("/service/resource?locale=en")
.exchange();
response.expectStatus()
.isOk()
.expectBody(String.class)
.isEqualTo("Service Resource");
assertThat(LoggerListAppender.getEvents())
// Modify Response
.haveAtLeastOne(eventContains("Added custom header to Response"));
}
private Condition<ILoggingEvent> eventContains(String substring) {
return new Condition<ILoggingEvent>(entry -> (substring == null || (entry.getFormattedMessage() != null && entry.getFormattedMessage()
.contains(substring))), String.format("entry with message '%s'", substring));
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.springcloudgateway.customfilters.utils;
import java.util.ArrayList;
import java.util.List;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
public class LoggerListAppender extends AppenderBase<ILoggingEvent> {
static private List<ILoggingEvent> events = new ArrayList<>();
@Override
protected void append(ILoggingEvent eventObject) {
events.add(eventObject);
}
public static List<ILoggingEvent> getEvents() {
return events;
}
public static void clearEventList() {
events.clear();
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.springcloudgateway.introduction;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import com.baeldung.springcloudgateway.introduction.IntroductionGatewayApplication;
@SpringBootTest(classes = IntroductionGatewayApplication.class)
public class SpringContextTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}

View File

@ -1,17 +0,0 @@
package org.baeldung;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.spring.cloud.GatewayApplication;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = GatewayApplication.class)
public class SpringContextIntegrationTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}

View File

@ -1,17 +0,0 @@
package org.baeldung;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.spring.cloud.GatewayApplication;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = GatewayApplication.class)
public class SpringContextTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="LISTAPPENDER"
class="com.baeldung.springcloudgateway.customfilters.utils.LoggerListAppender">
</appender>
<root level="info">
<appender-ref ref="LISTAPPENDER" />
</root>
</configuration>