@Test public void sockJsAttributesSupport() { loadBeanDefinitions("websocket-config-handlers-sockjs-attributes.xml"); SimpleUrlHandlerMapping handlerMapping = appContext.getBean(SimpleUrlHandlerMapping.class); assertNotNull(handlerMapping); SockJsHttpRequestHandler handler = (SockJsHttpRequestHandler) handlerMapping.getUrlMap().get("/test/**"); assertNotNull(handler); checkDelegateHandlerType(handler.getWebSocketHandler(), TestWebSocketHandler.class); SockJsService sockJsService = handler.getSockJsService(); assertNotNull(sockJsService); assertThat(sockJsService, Matchers.instanceOf(TransportHandlingSockJsService.class)); TransportHandlingSockJsService defaultSockJsService = (TransportHandlingSockJsService) sockJsService; assertThat( defaultSockJsService.getTaskScheduler(), Matchers.instanceOf(TestTaskScheduler.class)); assertThat( defaultSockJsService.getTransportHandlers().values(), Matchers.containsInAnyOrder( Matchers.instanceOf(XhrPollingTransportHandler.class), Matchers.instanceOf(XhrStreamingTransportHandler.class))); assertEquals("testSockJsService", defaultSockJsService.getName()); assertFalse(defaultSockJsService.isWebSocketEnabled()); assertFalse(defaultSockJsService.isSessionCookieNeeded()); assertEquals(2048, defaultSockJsService.getStreamBytesLimit()); assertEquals(256, defaultSockJsService.getDisconnectDelay()); assertEquals(1024, defaultSockJsService.getHttpMessageCacheSize()); assertEquals(20, defaultSockJsService.getHeartbeatTime()); }
@Test public void customizedTransportHandlerList() { TransportHandlingSockJsService service = new TransportHandlingSockJsService( mock(TaskScheduler.class), new XhrPollingTransportHandler(), new XhrReceivingTransportHandler()); Map<TransportType, TransportHandler> actualHandlers = service.getTransportHandlers(); assertEquals(2, actualHandlers.size()); }
@Test public void handleTransportRequestWebsocket() throws Exception { TransportHandlingSockJsService wsService = new TransportHandlingSockJsService(this.taskScheduler, this.wsTransportHandler); String sockJsPath = "/websocket"; setRequest("GET", sockJsPrefix + sockJsPath); wsService.handleRequest(this.request, this.response, sockJsPath, this.wsHandler); assertNotEquals(403, this.servletResponse.getStatus()); resetRequestAndResponse(); OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(Arrays.asList("http://mydomain1.com")); wsService.setHandshakeInterceptors(Arrays.asList(interceptor)); setRequest("GET", sockJsPrefix + sockJsPath); setOrigin("http://mydomain1.com"); wsService.handleRequest(this.request, this.response, sockJsPath, this.wsHandler); assertNotEquals(403, this.servletResponse.getStatus()); resetRequestAndResponse(); setRequest("GET", sockJsPrefix + sockJsPath); setOrigin("http://mydomain2.com"); wsService.handleRequest(this.request, this.response, sockJsPath, this.wsHandler); assertEquals(403, this.servletResponse.getStatus()); }
@Test public void handleTransportRequestXhr() throws Exception { String sockJsPath = sessionUrlPrefix + "xhr"; setRequest("POST", sockJsPrefix + sockJsPath); this.service.handleRequest(this.request, this.response, sockJsPath, this.wsHandler); assertEquals(200, this.servletResponse.getStatus()); verify(this.xhrHandler) .handleRequest(this.request, this.response, this.wsHandler, this.session); verify(taskScheduler) .scheduleAtFixedRate(any(Runnable.class), eq(service.getDisconnectDelay())); assertEquals( "no-store, no-cache, must-revalidate, max-age=0", this.response.getHeaders().getCacheControl()); assertNull(this.response.getHeaders().getFirst("Access-Control-Allow-Origin")); assertNull(this.response.getHeaders().getFirst("Access-Control-Allow-Credentials")); }
@Test public void handleTransportRequestJsonp() throws Exception { TransportHandlingSockJsService jsonpService = new TransportHandlingSockJsService( this.taskScheduler, this.jsonpHandler, this.jsonpSendHandler); String sockJsPath = sessionUrlPrefix + "jsonp"; setRequest("GET", sockJsPrefix + sockJsPath); jsonpService.handleRequest(this.request, this.response, sockJsPath, this.wsHandler); assertEquals(404, this.servletResponse.getStatus()); resetRequestAndResponse(); jsonpService.setAllowedOrigins(Arrays.asList("http://mydomain1.com")); setRequest("GET", sockJsPrefix + sockJsPath); jsonpService.handleRequest(this.request, this.response, sockJsPath, this.wsHandler); assertEquals(404, this.servletResponse.getStatus()); resetRequestAndResponse(); jsonpService.setAllowedOrigins(Arrays.asList("*")); setRequest("GET", sockJsPrefix + sockJsPath); jsonpService.handleRequest(this.request, this.response, sockJsPath, this.wsHandler); assertNotEquals(404, this.servletResponse.getStatus()); }