|
| 1 | +/* |
| 2 | + * Copyright 2002-2021 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 | + * https://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.web.reactive.socket.server.support; |
| 17 | + |
| 18 | +import java.util.Collections; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import org.springframework.http.HttpHeaders; |
| 23 | +import org.springframework.web.context.support.StaticWebApplicationContext; |
| 24 | +import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping; |
| 25 | +import org.springframework.web.reactive.socket.WebSocketHandler; |
| 26 | +import org.springframework.web.server.ServerWebExchange; |
| 27 | +import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest; |
| 28 | +import org.springframework.web.testfixture.server.MockServerWebExchange; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | +import static org.mockito.Mockito.mock; |
| 32 | + |
| 33 | +/** |
| 34 | + * Unit tests for and related to the use of {@link WebSocketUpgradeHandlerPredicate}. |
| 35 | + * |
| 36 | + * @author Rossen Stoyanchev |
| 37 | + */ |
| 38 | +public class WebSocketUpgradeHandlerPredicateTests { |
| 39 | + |
| 40 | + private final WebSocketUpgradeHandlerPredicate predicate = new WebSocketUpgradeHandlerPredicate(); |
| 41 | + |
| 42 | + private final WebSocketHandler webSocketHandler = mock(WebSocketHandler.class); |
| 43 | + |
| 44 | + ServerWebExchange httpGetExchange = |
| 45 | + MockServerWebExchange.from(MockServerHttpRequest.get("/path")); |
| 46 | + |
| 47 | + ServerWebExchange httpPostExchange = |
| 48 | + MockServerWebExchange.from(MockServerHttpRequest.post("/path")); |
| 49 | + |
| 50 | + ServerWebExchange webSocketExchange = |
| 51 | + MockServerWebExchange.from(MockServerHttpRequest.get("/path").header(HttpHeaders.UPGRADE, "websocket")); |
| 52 | + |
| 53 | + |
| 54 | + @Test |
| 55 | + void match() { |
| 56 | + assertThat(this.predicate.test(this.webSocketHandler, this.webSocketExchange)) |
| 57 | + .as("Should match WebSocketHandler to WebSocket upgrade") |
| 58 | + .isTrue(); |
| 59 | + |
| 60 | + assertThat(this.predicate.test(new Object(), this.httpGetExchange)) |
| 61 | + .as("Should match non-WebSocketHandler to any request") |
| 62 | + .isTrue(); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void noMatch() { |
| 67 | + assertThat(this.predicate.test(this.webSocketHandler, this.httpGetExchange)) |
| 68 | + .as("Should not match WebSocket handler to HTTP GET") |
| 69 | + .isFalse(); |
| 70 | + |
| 71 | + assertThat(this.predicate.test(this.webSocketHandler, this.httpPostExchange)) |
| 72 | + .as("Should not match WebSocket handler to HTTP POST") |
| 73 | + .isFalse(); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void simpleUrlHandlerMapping() { |
| 78 | + SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping(); |
| 79 | + mapping.setUrlMap(Collections.singletonMap("/path", this.webSocketHandler)); |
| 80 | + mapping.setApplicationContext(new StaticWebApplicationContext()); |
| 81 | + |
| 82 | + Object actual = mapping.getHandler(httpGetExchange).block(); |
| 83 | + assertThat(actual).as("Should match HTTP GET by URL path").isSameAs(this.webSocketHandler); |
| 84 | + |
| 85 | + mapping.setHandlerPredicate(new WebSocketUpgradeHandlerPredicate()); |
| 86 | + |
| 87 | + actual = mapping.getHandler(this.httpGetExchange).block(); |
| 88 | + assertThat(actual).as("Should not match if not a WebSocket upgrade").isNull(); |
| 89 | + |
| 90 | + actual = mapping.getHandler(this.httpPostExchange).block(); |
| 91 | + assertThat(actual).as("Should not match if not a WebSocket upgrade").isNull(); |
| 92 | + |
| 93 | + actual = mapping.getHandler(this.webSocketExchange).block(); |
| 94 | + assertThat(actual).as("Should match WebSocket upgrade").isSameAs(this.webSocketHandler); |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments