类org.springframework.web.socket.sockjs.transport.session.AbstractSockJsSession源码实例Demo

下面列出了怎么用org.springframework.web.socket.sockjs.transport.session.AbstractSockJsSession的API类实例代码及写法,或者点击链接到github查看源代码。

@Test
public void handleRequestXhr() throws Exception {
	XhrPollingTransportHandler transportHandler = new XhrPollingTransportHandler();
	transportHandler.initialize(this.sockJsConfig);

	AbstractSockJsSession session = transportHandler.createSession("1", this.webSocketHandler, null);
	transportHandler.handleRequest(this.request, this.response, this.webSocketHandler, session);

	assertEquals("application/javascript;charset=UTF-8", this.response.getHeaders().getContentType().toString());
	assertEquals("o\n", this.servletResponse.getContentAsString());
	assertFalse("Polling request should complete after open frame", this.servletRequest.isAsyncStarted());
	verify(this.webSocketHandler).afterConnectionEstablished(session);

	resetRequestAndResponse();
	transportHandler.handleRequest(this.request, this.response, this.webSocketHandler, session);

	assertTrue("Polling request should remain open", this.servletRequest.isAsyncStarted());
	verify(this.taskScheduler).schedule(any(Runnable.class), any(Date.class));

	resetRequestAndResponse();
	transportHandler.handleRequest(this.request, this.response, this.webSocketHandler, session);

	assertFalse("Request should have been rejected", this.servletRequest.isAsyncStarted());
	assertEquals("c[2010,\"Another connection still open\"]\n", this.servletResponse.getContentAsString());
}
 
@Test
public void handleRequestXhr() throws Exception {
	XhrPollingTransportHandler transportHandler = new XhrPollingTransportHandler();
	transportHandler.initialize(this.sockJsConfig);

	AbstractSockJsSession session = transportHandler.createSession("1", this.webSocketHandler, null);
	transportHandler.handleRequest(this.request, this.response, this.webSocketHandler, session);

	assertEquals("application/javascript;charset=UTF-8", this.response.getHeaders().getContentType().toString());
	assertEquals("o\n", this.servletResponse.getContentAsString());
	assertFalse("Polling request should complete after open frame", this.servletRequest.isAsyncStarted());
	verify(this.webSocketHandler).afterConnectionEstablished(session);

	resetRequestAndResponse();
	transportHandler.handleRequest(this.request, this.response, this.webSocketHandler, session);

	assertTrue("Polling request should remain open", this.servletRequest.isAsyncStarted());
	verify(this.taskScheduler).schedule(any(Runnable.class), any(Date.class));

	resetRequestAndResponse();
	transportHandler.handleRequest(this.request, this.response, this.webSocketHandler, session);

	assertFalse("Request should have been rejected", this.servletRequest.isAsyncStarted());
	assertEquals("c[2010,\"Another connection still open\"]\n", this.servletResponse.getContentAsString());
}
 
@Test
public void handleRequestXhr() throws Exception {
	XhrPollingTransportHandler transportHandler = new XhrPollingTransportHandler();
	transportHandler.initialize(this.sockJsConfig);

	AbstractSockJsSession session = transportHandler.createSession("1", this.webSocketHandler, null);
	transportHandler.handleRequest(this.request, this.response, this.webSocketHandler, session);

	assertEquals("application/javascript;charset=UTF-8", this.response.getHeaders().getContentType().toString());
	assertEquals("o\n", this.servletResponse.getContentAsString());
	assertFalse("Polling request should complete after open frame", this.servletRequest.isAsyncStarted());
	verify(this.webSocketHandler).afterConnectionEstablished(session);

	resetRequestAndResponse();
	transportHandler.handleRequest(this.request, this.response, this.webSocketHandler, session);

	assertTrue("Polling request should remain open", this.servletRequest.isAsyncStarted());
	verify(this.taskScheduler).schedule(any(Runnable.class), any(Date.class));

	resetRequestAndResponse();
	transportHandler.handleRequest(this.request, this.response, this.webSocketHandler, session);

	assertFalse("Request should have been rejected", this.servletRequest.isAsyncStarted());
	assertEquals("c[2010,\"Another connection still open\"]\n", this.servletResponse.getContentAsString());
}
 
@Test
public void handleRequestXhrStreaming() throws Exception {
	XhrStreamingTransportHandler transportHandler = new XhrStreamingTransportHandler();
	transportHandler.initialize(this.sockJsConfig);
	AbstractSockJsSession session = transportHandler.createSession("1", this.webSocketHandler, null);

	transportHandler.handleRequest(this.request, this.response, this.webSocketHandler, session);

	assertEquals("application/javascript;charset=UTF-8", this.response.getHeaders().getContentType().toString());
	assertTrue("Streaming request not started", this.servletRequest.isAsyncStarted());
	verify(this.webSocketHandler).afterConnectionEstablished(session);
}
 
private void handleRequest(AbstractHttpReceivingTransportHandler transportHandler) throws Exception {
	WebSocketHandler wsHandler = mock(WebSocketHandler.class);
	AbstractSockJsSession session = new TestHttpSockJsSession("1", new StubSockJsServiceConfig(), wsHandler, null);

	transportHandler.initialize(new StubSockJsServiceConfig());
	transportHandler.handleRequest(this.request, this.response, wsHandler, session);

	assertEquals("text/plain;charset=UTF-8", this.response.getHeaders().getContentType().toString());
	verify(wsHandler).handleMessage(session, new TextMessage("x"));
}
 
private void handleRequestAndExpectFailure() throws Exception {
	resetResponse();

	WebSocketHandler wsHandler = mock(WebSocketHandler.class);
	AbstractSockJsSession session = new TestHttpSockJsSession("1", new StubSockJsServiceConfig(), wsHandler, null);

	new XhrReceivingTransportHandler().handleRequest(this.request, this.response, wsHandler, session);

	assertEquals(500, this.servletResponse.getStatus());
	verifyNoMoreInteractions(wsHandler);
}
 
@Test
public void handleRequestXhrStreaming() throws Exception {
	XhrStreamingTransportHandler transportHandler = new XhrStreamingTransportHandler();
	transportHandler.initialize(this.sockJsConfig);
	AbstractSockJsSession session = transportHandler.createSession("1", this.webSocketHandler, null);

	transportHandler.handleRequest(this.request, this.response, this.webSocketHandler, session);

	assertEquals("application/javascript;charset=UTF-8", this.response.getHeaders().getContentType().toString());
	assertTrue("Streaming request not started", this.servletRequest.isAsyncStarted());
	verify(this.webSocketHandler).afterConnectionEstablished(session);
}
 
private void handleRequest(AbstractHttpReceivingTransportHandler transportHandler) throws Exception {
	WebSocketHandler wsHandler = mock(WebSocketHandler.class);
	AbstractSockJsSession session = new TestHttpSockJsSession("1", new StubSockJsServiceConfig(), wsHandler, null);

	transportHandler.initialize(new StubSockJsServiceConfig());
	transportHandler.handleRequest(this.request, this.response, wsHandler, session);

	assertEquals("text/plain;charset=UTF-8", this.response.getHeaders().getContentType().toString());
	verify(wsHandler).handleMessage(session, new TextMessage("x"));
}
 
private void handleRequestAndExpectFailure() throws Exception {
	resetResponse();

	WebSocketHandler wsHandler = mock(WebSocketHandler.class);
	AbstractSockJsSession session = new TestHttpSockJsSession("1", new StubSockJsServiceConfig(), wsHandler, null);

	new XhrReceivingTransportHandler().handleRequest(this.request, this.response, wsHandler, session);

	assertEquals(500, this.servletResponse.getStatus());
	verifyNoMoreInteractions(wsHandler);
}
 
@Test
public void handleRequestXhrStreaming() throws Exception {
	XhrStreamingTransportHandler transportHandler = new XhrStreamingTransportHandler();
	transportHandler.initialize(this.sockJsConfig);
	AbstractSockJsSession session = transportHandler.createSession("1", this.webSocketHandler, null);

	transportHandler.handleRequest(this.request, this.response, this.webSocketHandler, session);

	assertEquals("application/javascript;charset=UTF-8", this.response.getHeaders().getContentType().toString());
	assertTrue("Streaming request not started", this.servletRequest.isAsyncStarted());
	verify(this.webSocketHandler).afterConnectionEstablished(session);
}
 
private void handleRequest(AbstractHttpReceivingTransportHandler transportHandler) throws Exception {
	WebSocketHandler wsHandler = mock(WebSocketHandler.class);
	AbstractSockJsSession session = new TestHttpSockJsSession("1", new StubSockJsServiceConfig(), wsHandler, null);

	transportHandler.initialize(new StubSockJsServiceConfig());
	transportHandler.handleRequest(this.request, this.response, wsHandler, session);

	assertEquals("text/plain;charset=UTF-8", this.response.getHeaders().getContentType().toString());
	verify(wsHandler).handleMessage(session, new TextMessage("x"));
}
 
private void handleRequestAndExpectFailure() throws Exception {
	resetResponse();

	WebSocketHandler wsHandler = mock(WebSocketHandler.class);
	AbstractSockJsSession session = new TestHttpSockJsSession("1", new StubSockJsServiceConfig(), wsHandler, null);

	new XhrReceivingTransportHandler().handleRequest(this.request, this.response, wsHandler, session);

	assertEquals(500, this.servletResponse.getStatus());
	verifyNoMoreInteractions(wsHandler);
}
 
@Override
public AbstractSockJsSession createSession(String id, WebSocketHandler handler, Map<String, Object> attrs) {
	return new WebSocketServerSockJsSession(id, getServiceConfig(), handler, attrs);
}
 
@Override
public AbstractSockJsSession createSession(String id, WebSocketHandler handler, Map<String, Object> attrs) {
	return new WebSocketServerSockJsSession(id, getServiceConfig(), handler, attrs);
}
 
@Override
public AbstractSockJsSession createSession(String id, WebSocketHandler handler, Map<String, Object> attrs) {
	return new WebSocketServerSockJsSession(id, getServiceConfig(), handler, attrs);
}
 
 类方法
 同包方法