在Spring中,用于websockets测试的MockHttpServletRequestBuilder相当于什么

问题描述 投票:7回答:1

对于websockets,MockHttpServletRequestBuilder的等价物是什么。即在我想测试Websockets的情况下,我想测试一个运行时间很长的websocket应用程序,并避免在第一次应该进行升级的http get调用之后SecurityContextPersistenceFilter覆盖SecurityContex的情况。对于普通休息的http应用程序,这是通过利用SecurityMockMvcRequestPostProcessors到目前为止完成的。例如using the SecurityMockMvcRequestPostProcessors

但是当我想测试一个长期运行的websocket应用程序时该怎么做。即我想要为websockets创建类似MockHttpServletRequestBuilder的东西。春天有类似的东西吗?或者有没有办法为此目的使用MockHttpServletRequestBuilder?即目标是创建websocket端点并避免升级后SecurityContex被清除的情况。

我找到了一些替代方法,比如传递会话,如here所述,但这对我来说不是一个替代方案,因为使用方法级安全性的代码不起作用,因为SecurityContex正在被更改。

spring spring-mvc spring-boot junit spring-security
1个回答
0
投票

看来这可以通过提供测试sock配置来完成。防爆

    @EnableWebSocketMessageBroker
    static class TestWebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

        @Autowired
        Environment env;

        @Override
        public void registerStompEndpoints(StompEndpointRegistry registry) {
            registry.addEndpoint("/portfolio").withSockJS();
        }

        @Override
        public void configureMessageBroker(MessageBrokerRegistry registry) {
//          registry.enableSimpleBroker("/queue/", "/topic/");
            registry.enableStompBrokerRelay("/queue/", "/topic/");
            registry.setApplicationDestinationPrefixes("/app");
        }
    }

完整的课程可以在这里找到:https://github.com/rstoyanchev/spring-websocket-portfolio/blob/master/src/test/java/org/springframework/samples/portfolio/web/context/ContextPortfolioControllerTests.java

以下是Spring提供的一些其他示例,演示了测试Web套接字的3种不同方法:https://github.com/rstoyanchev/spring-websocket-portfolio/tree/master/src/test/java/org/springframework/samples/portfolio/web

© www.soinside.com 2019 - 2024. All rights reserved.