diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/controller/UserController.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/controller/UserController.java new file mode 100644 index 0000000000..f51a674fe8 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/controller/UserController.java @@ -0,0 +1,15 @@ +package com.baeldung.reactive.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; +import reactor.core.publisher.Mono; + +@RestController +public class UserController { + + @GetMapping(path = "/users/{name}") + public Mono getName(@PathVariable String name) { + return Mono.just(name); + } +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/filter/ExampleHandlerFilterFunction.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/filter/ExampleHandlerFilterFunction.java new file mode 100644 index 0000000000..e55f5ce2fe --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/filter/ExampleHandlerFilterFunction.java @@ -0,0 +1,22 @@ +package com.baeldung.reactive.filter; + +import org.springframework.web.reactive.function.server.HandlerFilterFunction; +import org.springframework.web.reactive.function.server.HandlerFunction; +import org.springframework.web.reactive.function.server.ServerRequest; +import org.springframework.web.reactive.function.server.ServerResponse; +import reactor.core.publisher.Mono; + +import static org.springframework.http.HttpStatus.FORBIDDEN; + +public class ExampleHandlerFilterFunction implements HandlerFilterFunction { + + @Override + public Mono filter(ServerRequest serverRequest, HandlerFunction handlerFunction) { + if (serverRequest.pathVariable("name").equalsIgnoreCase("test")) { + return ServerResponse.status(FORBIDDEN).build(); + } + return handlerFunction.handle(serverRequest); + } + +} + diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/filter/ExampleWebFilter.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/filter/ExampleWebFilter.java new file mode 100644 index 0000000000..99bc9f7fc3 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/filter/ExampleWebFilter.java @@ -0,0 +1,17 @@ +package com.baeldung.reactive.filter; + +import org.springframework.stereotype.Component; +import org.springframework.web.server.ServerWebExchange; +import org.springframework.web.server.WebFilter; +import org.springframework.web.server.WebFilterChain; +import reactor.core.publisher.Mono; + +@Component +public class ExampleWebFilter implements WebFilter { + + @Override + public Mono filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) { + serverWebExchange.getResponse().getHeaders().add("web-filter", "web-filter-test"); + return webFilterChain.filter(serverWebExchange); + } +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/handler/PlayerHandler.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/handler/PlayerHandler.java new file mode 100644 index 0000000000..51b4e166d1 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/handler/PlayerHandler.java @@ -0,0 +1,17 @@ +package com.baeldung.reactive.handler; + +import org.springframework.stereotype.Component; +import org.springframework.web.reactive.function.server.ServerRequest; +import org.springframework.web.reactive.function.server.ServerResponse; +import reactor.core.publisher.Mono; + +import static org.springframework.web.reactive.function.server.ServerResponse.ok; + +@Component +public class PlayerHandler { + + public Mono getName(ServerRequest request) { + Mono name = Mono.just(request.pathVariable("name")); + return ok().body(name, String.class); + } +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/router/PlayerRouter.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/router/PlayerRouter.java new file mode 100644 index 0000000000..be721964ca --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/router/PlayerRouter.java @@ -0,0 +1,22 @@ +package com.baeldung.reactive.router; + +import com.baeldung.reactive.filter.ExampleHandlerFilterFunction; +import com.baeldung.reactive.handler.PlayerHandler; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.reactive.function.server.RouterFunction; +import org.springframework.web.reactive.function.server.RouterFunctions; +import org.springframework.web.reactive.function.server.ServerResponse; + +import static org.springframework.web.reactive.function.server.RequestPredicates.GET; + +@Configuration +public class PlayerRouter { + + @Bean + public RouterFunction route(PlayerHandler playerHandler) { + return RouterFunctions + .route(GET("/players/{name}"), playerHandler::getName) + .filter(new ExampleHandlerFilterFunction()::filter); + } +} diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/controller/UserControllerTest.java b/spring-5-reactive/src/test/java/com/baeldung/reactive/controller/UserControllerTest.java new file mode 100644 index 0000000000..31a6a57fcd --- /dev/null +++ b/spring-5-reactive/src/test/java/com/baeldung/reactive/controller/UserControllerTest.java @@ -0,0 +1,43 @@ +package com.baeldung.reactive.controller; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.reactive.server.EntityExchangeResult; +import org.springframework.test.web.reactive.server.WebTestClient; + +import static org.junit.Assert.assertEquals; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class UserControllerTest { + + @Autowired + private WebTestClient webTestClient; + + @Test + public void getUserName() { + EntityExchangeResult result = webTestClient.get().uri("/users/baeldung") + .exchange() + .expectStatus().isOk() + .expectBody(String.class) + .returnResult(); + + assertEquals(result.getResponseBody(), "baeldung"); + assertEquals(result.getResponseHeaders().getFirst("web-filter"), "web-filter-test"); + } + + @Test + public void getUserNameTest() { + EntityExchangeResult result = webTestClient.get().uri("/users/test") + .exchange() + .expectStatus().isOk() + .expectBody(String.class) + .returnResult(); + + assertEquals(result.getResponseBody(), "test"); + assertEquals(result.getResponseHeaders().getFirst("web-filter"), "web-filter-test"); + } +} \ No newline at end of file diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/handler/PlayerHandlerTest.java b/spring-5-reactive/src/test/java/com/baeldung/reactive/handler/PlayerHandlerTest.java new file mode 100644 index 0000000000..83292cf83d --- /dev/null +++ b/spring-5-reactive/src/test/java/com/baeldung/reactive/handler/PlayerHandlerTest.java @@ -0,0 +1,39 @@ +package com.baeldung.reactive.handler; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.reactive.server.EntityExchangeResult; +import org.springframework.test.web.reactive.server.WebTestClient; + +import static org.junit.Assert.assertEquals; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class PlayerHandlerTest { + + @Autowired + private WebTestClient webTestClient; + + @Test + public void getPlayerName() { + EntityExchangeResult result = webTestClient.get().uri("/players/baeldung") + .exchange() + .expectStatus().isOk() + .expectBody(String.class) + .returnResult(); + + assertEquals(result.getResponseBody(), "baeldung"); + assertEquals(result.getResponseHeaders().getFirst("web-filter"), "web-filter-test"); + } + + @Test + public void getForbiddenIfPlayerNameIsTest() { + webTestClient.get().uri("/players/test") + .exchange() + .expectStatus().isForbidden(); + } + +} \ No newline at end of file