【问题标题】:What is the equivalent of MockHttpServletRequestBuilder for websockets tests in SpringSpring中用于websockets测试的MockHttpServletRequestBuilder的等价物是什么
【发布时间】:2017-11-23 02:06:47
【问题描述】:

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

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

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

【问题讨论】:

    标签: spring spring-mvc spring-boot junit spring-security


    【解决方案1】:

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

        @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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-13
      • 2017-08-24
      • 2012-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-12
      相关资源
      最近更新 更多