From 2f35e7756b079c7715fd1df31a158d98c55be590 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sun, 4 Jun 2023 16:25:21 +0200 Subject: [PATCH] Integration test status quo for SpEL 'selector' support in messaging See gh-30550 --- ...essageBrokerConfigurationSupportTests.java | 45 +++++++++++++++++++ .../StompWebSocketIntegrationTests.java | 9 ++-- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/config/annotation/WebSocketMessageBrokerConfigurationSupportTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/config/annotation/WebSocketMessageBrokerConfigurationSupportTests.java index 2623257123bf..3f4ee1ef989a 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/config/annotation/WebSocketMessageBrokerConfigurationSupportTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/config/annotation/WebSocketMessageBrokerConfigurationSupportTests.java @@ -35,6 +35,7 @@ import org.springframework.messaging.handler.annotation.SendTo; import org.springframework.messaging.simp.SimpMessageType; import org.springframework.messaging.simp.annotation.SubscribeMapping; +import org.springframework.messaging.simp.broker.DefaultSubscriptionRegistry; import org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler; import org.springframework.messaging.simp.config.MessageBrokerRegistry; import org.springframework.messaging.simp.stomp.StompCommand; @@ -60,6 +61,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.InstanceOfAssertFactories.BOOLEAN; +import static org.assertj.core.api.InstanceOfAssertFactories.type; import static org.mockito.Mockito.mock; /** @@ -67,6 +69,7 @@ * * @author Rossen Stoyanchev * @author Sebastien Deleuze + * @author Sam Brannen */ class WebSocketMessageBrokerConfigurationSupportTests { @@ -161,6 +164,32 @@ void taskScheduler() { assertThat(handler.getHeartbeatValue()).containsExactly(15000, 15000); } + @Test + void selectorHeaderEnabledByDefault() { + ApplicationContext context = createContext(TestChannelConfig.class, TestConfigurer.class); + SimpleBrokerMessageHandler simpleBrokerMessageHandler = simpleBrokerMessageHandler(context); + + assertThat(simpleBrokerMessageHandler.getSubscriptionRegistry()) + .asInstanceOf(type(DefaultSubscriptionRegistry.class)) + .extracting(DefaultSubscriptionRegistry::getSelectorHeaderName) + .isEqualTo("selector"); + } + + @Test + void selectorHeaderDisabled() { + ApplicationContext context = createContext(TestChannelConfig.class, SelectorHeaderConfigurer.class); + SimpleBrokerMessageHandler simpleBrokerMessageHandler = simpleBrokerMessageHandler(context); + + assertThat(simpleBrokerMessageHandler.getSubscriptionRegistry()) + .asInstanceOf(type(DefaultSubscriptionRegistry.class)) + .extracting(DefaultSubscriptionRegistry::getSelectorHeaderName) + .isNull(); + } + + private static SimpleBrokerMessageHandler simpleBrokerMessageHandler(ApplicationContext context) { + return context.getBean("simpleBrokerMessageHandler", SimpleBrokerMessageHandler.class); + } + @Test void webSocketMessageBrokerStats() { ApplicationContext context = createContext(TestChannelConfig.class, TestConfigurer.class); @@ -243,6 +272,22 @@ public void configureMessageBroker(MessageBrokerRegistry registry) { } } + @Configuration + static class SelectorHeaderConfigurer implements WebSocketMessageBrokerConfigurer { + + @Override + public void registerStompEndpoints(StompEndpointRegistry registry) { + registry.addEndpoint("/simpleBroker"); + } + + @Override + public void configureMessageBroker(MessageBrokerRegistry registry) { + // Explicitly disable selector header support + registry.enableSimpleBroker().setSelectorHeaderName(null); + } + + } + @Configuration static class TestChannelConfig extends DelegatingWebSocketMessageBrokerConfiguration { diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/messaging/StompWebSocketIntegrationTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/messaging/StompWebSocketIntegrationTests.java index 9245df9aae42..ba91b7d7bad1 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/messaging/StompWebSocketIntegrationTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/messaging/StompWebSocketIntegrationTests.java @@ -105,12 +105,15 @@ void sendMessageToControllerAndReceiveReplyViaTopic(WebSocketTestServer server, } @ParameterizedWebSocketTest // SPR-10930 - void sendMessageToBrokerAndReceiveReplyViaTopic(WebSocketTestServer server, WebSocketClient webSocketClient, TestInfo testInfo) throws Exception { + void sendMessageToBrokerAndReceiveReplyViaTopicWithSelectorHeader(WebSocketTestServer server, WebSocketClient webSocketClient, TestInfo testInfo) throws Exception { super.setup(server, webSocketClient, testInfo); + String destination = "destination:/topic/foo"; + String selector = "selector:headers.foo == 'bar'"; + TextMessage m0 = create(StompCommand.CONNECT).headers("accept-version:1.1").build(); - TextMessage m1 = create(StompCommand.SUBSCRIBE).headers("id:subs1", "destination:/topic/foo").build(); - TextMessage m2 = create(StompCommand.SEND).headers("destination:/topic/foo").body("5").build(); + TextMessage m1 = create(StompCommand.SUBSCRIBE).headers("id:subs1", destination, selector).build(); + TextMessage m2 = create(StompCommand.SEND).headers(destination, "foo:bar").body("5").build(); TestClientWebSocketHandler clientHandler = new TestClientWebSocketHandler(2, m0, m1, m2);