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

Tit*_*ito 8 junit spring spring-mvc spring-security spring-boot

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

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

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

mad*_*fox 0

看来这可以通过提供测试袜子配置来完成。前任

    @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");
        }
    }
Run Code Online (Sandbox Code Playgroud)

完整的类可以在这里找到: 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 /投资组合/网络