private MessageChannel clientInboundChannel() { return context.getBean("clientInboundChannel", MessageChannel.class); }
private MessageChannel clientInboundChannel() { return context.getBean("clientInboundChannel", MessageChannel.class); }
private void assertHandshake(HttpServletRequest request) { TestHandshakeHandler handshakeHandler = context .getBean(TestHandshakeHandler.class); assertThat(handshakeHandler.attributes.get(CsrfToken.class.getName())).isSameAs( token); assertThat(handshakeHandler.attributes.get(sessionAttr)).isEqualTo( request.getSession().getAttribute(sessionAttr)); }
@Test public void getEndpointSingletonByType() throws Exception { EchoEndpoint expected = this.webAppContext.getBean(EchoEndpoint.class); EchoEndpoint actual = this.configurator.getEndpointInstance(EchoEndpoint.class); assertSame(expected, actual); }
@Test public void getEndpointSingletonByComponentName() throws Exception { ComponentEchoEndpoint expected = this.webAppContext.getBean(ComponentEchoEndpoint.class); ComponentEchoEndpoint actual = this.configurator.getEndpointInstance(ComponentEchoEndpoint.class); assertSame(expected, actual); }
@Test @SuppressWarnings("resource") public void configLocationWithSingleClass() { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.setConfigLocation(Config.class.getName()); ctx.refresh(); TestBean bean = ctx.getBean(TestBean.class); assertNotNull(bean); }
private HttpRequestHandler handler(HttpServletRequest request) throws Exception { HandlerMapping handlerMapping = context.getBean(HandlerMapping.class); return (HttpRequestHandler) handlerMapping.getHandler(request).getHandler(); }
@Test public void registerWebSocketHandlerWithSockJS() throws Exception { WebSocketSession session = this.webSocketClient.doHandshake( new AbstractWebSocketHandler() {}, getWsBaseUrl() + "/sockjs/websocket").get(); TestHandler serverHandler = this.wac.getBean(TestHandler.class); assertTrue(serverHandler.connectLatch.await(2, TimeUnit.SECONDS)); session.close(); }
@Test public void registerWebSocketHandler() throws Exception { WebSocketSession session = this.webSocketClient.doHandshake( new AbstractWebSocketHandler() {}, getWsBaseUrl() + "/ws").get(); TestHandler serverHandler = this.wac.getBean(TestHandler.class); assertTrue(serverHandler.connectLatch.await(2, TimeUnit.SECONDS)); session.close(); }
@Test public void addsAuthenticationPrincipalResolverWhenNoAuthorization() throws InterruptedException { loadConfig(NoInboundSecurityConfig.class); MessageChannel messageChannel = clientInboundChannel(); Message<String> message = message("/permitAll/authentication"); messageChannel.send(message); assertThat(context.getBean(MyController.class).authenticationPrincipal) .isEqualTo((String) messageUser.getPrincipal()); }
@Test public void addsAuthenticationPrincipalResolver() throws InterruptedException { loadConfig(SockJsSecurityConfig.class); MessageChannel messageChannel = clientInboundChannel(); Message<String> message = message("/permitAll/authentication"); messageChannel.send(message); assertThat(context.getBean(MyController.class).authenticationPrincipal) .isEqualTo((String) messageUser.getPrincipal()); }
@Test @SuppressWarnings("resource") public void registerSingleClass() { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(Config.class); ctx.refresh(); TestBean bean = ctx.getBean(TestBean.class); assertNotNull(bean); }
@Test @SuppressWarnings("resource") public void configLocationWithBasePackage() { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.setConfigLocation("org.springframework.web.context.support"); ctx.refresh(); TestBean bean = ctx.getBean(TestBean.class); assertNotNull(bean); }
@Test(timeout = 5000) public void fallbackAfterConnectTimeout() throws Exception { TestClientHandler clientHandler = new TestClientHandler(); this.testFilter.sleepDelayMap.put("/xhr_streaming", 10000L); this.testFilter.sendErrorMap.put("/xhr_streaming", 503); initSockJsClient(createXhrTransport()); this.sockJsClient.setConnectTimeoutScheduler(this.wac.getBean(ThreadPoolTaskScheduler.class)); WebSocketSession clientSession = sockJsClient.doHandshake(clientHandler, this.baseUrl + "/echo").get(); assertEquals("Fallback didn't occur", XhrClientSockJsSession.class, clientSession.getClass()); TextMessage message = new TextMessage("message1"); clientSession.sendMessage(message); clientHandler.awaitMessage(message, 5000); clientSession.close(); }
@Test // SPR-12727 public void unsolicitedPongWithEmptyPayload() throws Exception { String url = getWsBaseUrl() + "/ws"; WebSocketSession session = this.webSocketClient.doHandshake(new AbstractWebSocketHandler() {}, url).get(); TestWebSocketHandler serverHandler = this.wac.getBean(TestWebSocketHandler.class); serverHandler.setWaitMessageCount(1); session.sendMessage(new PongMessage()); serverHandler.await(); assertNull(serverHandler.getTransportError()); assertEquals(1, serverHandler.getReceivedMessages().size()); assertEquals(PongMessage.class, serverHandler.getReceivedMessages().get(0).getClass()); }
private void testReceiveOneMessage(Transport transport, WebSocketHttpHeaders headers) throws Exception { TestClientHandler clientHandler = new TestClientHandler(); initSockJsClient(transport); this.sockJsClient.doHandshake(clientHandler, headers, new URI(this.baseUrl + "/test")).get(); TestServerHandler serverHandler = this.wac.getBean(TestServerHandler.class); assertNotNull("afterConnectionEstablished should have been called", clientHandler.session); serverHandler.awaitSession(5000); TextMessage message = new TextMessage("message1"); serverHandler.session.sendMessage(message); clientHandler.awaitMessage(message, 5000); }
@Test public void sendMessageToController() throws Exception { TextMessage message = create(StompCommand.SEND).headers("destination:/app/simple").build(); WebSocketSession session = doHandshake(new TestClientWebSocketHandler(0, message), "/ws").get(); SimpleController controller = this.wac.getBean(SimpleController.class); try { assertTrue(controller.latch.await(TIMEOUT, TimeUnit.SECONDS)); } finally { session.close(); } }
@Before @SuppressWarnings("resource") public void setup() throws Exception { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setServletContext(new MockServletContext()); context.register(WebConfig.class); context.refresh(); this.request = new MockHttpServletRequest("GET", "/"); this.request.setContextPath("/myapp"); this.response = new MockHttpServletResponse(); this.filterChain = new MockFilterChain(this.servlet, new ResourceUrlEncodingFilter(), (request, response, chain) -> { Object urlProvider = context.getBean(ResourceUrlProvider.class); request.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, urlProvider); chain.doFilter(request, response); }); }
@Test // SPR-12592 @SuppressWarnings("resource") public void initializeOnce() { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setServletContext(new MockServletContext()); context.register(HandlerMappingConfiguration.class); context.refresh(); assertThat(context.getBean(ResourceUrlProvider.class).getHandlerMap(), Matchers.hasKey(pattern("/resources/**"))); }
@Test // SPR-12592 @SuppressWarnings("resource") public void initializeOnce() throws Exception { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setServletContext(new MockServletContext()); context.register(HandlerMappingConfiguration.class); context.refresh(); ResourceUrlProvider urlProviderBean = context.getBean(ResourceUrlProvider.class); assertThat(urlProviderBean.getHandlerMap(), Matchers.hasKey("/resources/**")); assertFalse(urlProviderBean.isAutodetect()); }