|
| 1 | +/* |
| 2 | + * Copyright 2002-2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.test.web.reactive.server; |
| 17 | + |
| 18 | +import org.junit.Test; |
| 19 | +import reactor.core.publisher.Mono; |
| 20 | + |
| 21 | +import org.springframework.http.HttpStatus; |
| 22 | +import org.springframework.web.reactive.function.server.HandlerStrategies; |
| 23 | +import org.springframework.web.reactive.function.server.RouterFunction; |
| 24 | +import org.springframework.web.reactive.function.server.RouterFunctions; |
| 25 | +import org.springframework.web.reactive.function.server.ServerResponse; |
| 26 | + |
| 27 | +/** |
| 28 | + * Unit tests for {@link DefaultRouterFunctionSpec}. |
| 29 | + * @author Rossen Stoyanchev |
| 30 | + */ |
| 31 | +public class DefaultRouterFunctionSpecTests { |
| 32 | + |
| 33 | + @Test |
| 34 | + public void webFilter() { |
| 35 | + |
| 36 | + RouterFunction<ServerResponse> routerFunction = RouterFunctions.route() |
| 37 | + .GET("/", request -> ServerResponse.ok().build()) |
| 38 | + .build(); |
| 39 | + |
| 40 | + new DefaultRouterFunctionSpec(routerFunction) |
| 41 | + .handlerStrategies(HandlerStrategies.builder() |
| 42 | + .webFilter((exchange, chain) -> { |
| 43 | + exchange.getResponse().getHeaders().set("foo", "123"); |
| 44 | + return chain.filter(exchange); |
| 45 | + }) |
| 46 | + .build()) |
| 47 | + .build() |
| 48 | + .get() |
| 49 | + .uri("/") |
| 50 | + .exchange() |
| 51 | + .expectStatus().isOk() |
| 52 | + .expectHeader().valueEquals("foo", "123"); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void exceptionHandler() { |
| 57 | + |
| 58 | + RouterFunction<ServerResponse> routerFunction = RouterFunctions.route() |
| 59 | + .GET("/error", request -> Mono.error(new IllegalStateException("boo"))) |
| 60 | + .build(); |
| 61 | + |
| 62 | + new DefaultRouterFunctionSpec(routerFunction) |
| 63 | + .handlerStrategies(HandlerStrategies.builder() |
| 64 | + .exceptionHandler((exchange, ex) -> { |
| 65 | + exchange.getResponse().setStatusCode(HttpStatus.BAD_REQUEST); |
| 66 | + return Mono.empty(); |
| 67 | + }) |
| 68 | + .build()) |
| 69 | + .build() |
| 70 | + .get() |
| 71 | + .uri("/error") |
| 72 | + .exchange() |
| 73 | + .expectStatus().isBadRequest(); |
| 74 | + } |
| 75 | +} |
0 commit comments