|
| 1 | +/* |
| 2 | + * Copyright 2026-present 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 | + |
| 17 | +package org.springframework.integration.websocket.server; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.mockito.Answers; |
| 21 | + |
| 22 | +import org.springframework.integration.websocket.ServerWebSocketContainer; |
| 23 | +import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistration; |
| 24 | +import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; |
| 25 | + |
| 26 | +import static org.mockito.ArgumentMatchers.any; |
| 27 | +import static org.mockito.BDDMockito.given; |
| 28 | +import static org.mockito.Mockito.mock; |
| 29 | +import static org.mockito.Mockito.never; |
| 30 | +import static org.mockito.Mockito.verify; |
| 31 | + |
| 32 | +/** |
| 33 | + * Tests for {@link ServerWebSocketContainer}. |
| 34 | + * |
| 35 | + * @author Marcin Nowicki |
| 36 | + * |
| 37 | + * @since 7.0.5 |
| 38 | + */ |
| 39 | +public class ServerWebSocketContainerTests { |
| 40 | + |
| 41 | + @Test |
| 42 | + public void setAllowedOriginPatternsIsUsedInRegistration() { |
| 43 | + WebSocketHandlerRegistry registry = mock(); |
| 44 | + WebSocketHandlerRegistration registration = mock(Answers.RETURNS_SELF); |
| 45 | + given(registry.addHandler(any(), any(String[].class))).willReturn(registration); |
| 46 | + |
| 47 | + ServerWebSocketContainer container = new ServerWebSocketContainer("/test") |
| 48 | + .setAllowedOriginPatterns("https://example.com"); |
| 49 | + |
| 50 | + container.registerWebSocketHandlers(registry); |
| 51 | + |
| 52 | + verify(registration).setAllowedOriginPatterns("https://example.com"); |
| 53 | + verify(registration, never()).setAllowedOrigins(any()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void setAllowedOriginsIsUsedWhenNoPatternsConfigured() { |
| 58 | + WebSocketHandlerRegistry registry = mock(); |
| 59 | + WebSocketHandlerRegistration registration = mock(Answers.RETURNS_SELF); |
| 60 | + given(registry.addHandler(any(), any(String[].class))).willReturn(registration); |
| 61 | + |
| 62 | + ServerWebSocketContainer container = new ServerWebSocketContainer("/test") |
| 63 | + .setAllowedOrigins("https://example.com"); |
| 64 | + |
| 65 | + container.registerWebSocketHandlers(registry); |
| 66 | + |
| 67 | + verify(registration).setAllowedOrigins("https://example.com"); |
| 68 | + verify(registration, never()).setAllowedOriginPatterns(any()); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void bothOriginsAndOriginPatternsAreUsedWhenBothConfigured() { |
| 73 | + WebSocketHandlerRegistry registry = mock(); |
| 74 | + WebSocketHandlerRegistration registration = mock(Answers.RETURNS_SELF); |
| 75 | + given(registry.addHandler(any(), any(String[].class))).willReturn(registration); |
| 76 | + |
| 77 | + ServerWebSocketContainer container = new ServerWebSocketContainer("/test") |
| 78 | + .setAllowedOrigins("https://example.com") |
| 79 | + .setAllowedOriginPatterns("https://*.example.com"); |
| 80 | + |
| 81 | + container.registerWebSocketHandlers(registry); |
| 82 | + |
| 83 | + verify(registration).setAllowedOriginPatterns("https://*.example.com"); |
| 84 | + verify(registration).setAllowedOrigins("https://example.com"); |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments