org.springframework.web.socket.handler.TextWebSocketHandler Java Examples

The following examples show how to use org.springframework.web.socket.handler.TextWebSocketHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultHandshakeHandlerTests.java    From spring-analysis-note with MIT License 7 votes vote down vote up
@Test
public void supportedSubProtocols() {
	this.handshakeHandler.setSupportedProtocols("stomp", "mqtt");
	given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[] {"13"});
	this.servletRequest.setMethod("GET");

	WebSocketHttpHeaders headers = new WebSocketHttpHeaders(this.request.getHeaders());
	headers.setUpgrade("WebSocket");
	headers.setConnection("Upgrade");
	headers.setSecWebSocketVersion("13");
	headers.setSecWebSocketKey("82/ZS2YHjEnUN97HLL8tbw==");
	headers.setSecWebSocketProtocol("STOMP");

	WebSocketHandler handler = new TextWebSocketHandler();
	Map<String, Object> attributes = Collections.emptyMap();
	this.handshakeHandler.doHandshake(this.request, this.response, handler, attributes);

	verify(this.upgradeStrategy).upgrade(this.request, this.response, "STOMP",
			Collections.emptyList(), null, handler, attributes);
}
 
Example #2
Source File: DefaultHandshakeHandlerTests.java    From spring-analysis-note with MIT License 7 votes vote down vote up
@Test
public void supportedExtensions() {
	WebSocketExtension extension1 = new WebSocketExtension("ext1");
	WebSocketExtension extension2 = new WebSocketExtension("ext2");

	given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[] {"13"});
	given(this.upgradeStrategy.getSupportedExtensions(this.request)).willReturn(Collections.singletonList(extension1));

	this.servletRequest.setMethod("GET");

	WebSocketHttpHeaders headers = new WebSocketHttpHeaders(this.request.getHeaders());
	headers.setUpgrade("WebSocket");
	headers.setConnection("Upgrade");
	headers.setSecWebSocketVersion("13");
	headers.setSecWebSocketKey("82/ZS2YHjEnUN97HLL8tbw==");
	headers.setSecWebSocketExtensions(Arrays.asList(extension1, extension2));

	WebSocketHandler handler = new TextWebSocketHandler();
	Map<String, Object> attributes = Collections.<String, Object>emptyMap();
	this.handshakeHandler.doHandshake(this.request, this.response, handler, attributes);

	verify(this.upgradeStrategy).upgrade(this.request, this.response, null,
			Collections.singletonList(extension1), null, handler, attributes);
}
 
Example #3
Source File: WebSocketConnectionManagerTests.java    From java-technology-stack with MIT License 7 votes vote down vote up
@Test
public void openConnection() throws Exception {
	List<String> subprotocols = Arrays.asList("abc");

	TestLifecycleWebSocketClient client = new TestLifecycleWebSocketClient(false);
	WebSocketHandler handler = new TextWebSocketHandler();

	WebSocketConnectionManager manager = new WebSocketConnectionManager(client, handler , "/path/{id}", "123");
	manager.setSubProtocols(subprotocols);
	manager.openConnection();

	WebSocketHttpHeaders expectedHeaders = new WebSocketHttpHeaders();
	expectedHeaders.setSecWebSocketProtocol(subprotocols);

	assertEquals(expectedHeaders, client.headers);
	assertEquals(new URI("/path/123"), client.uri);

	WebSocketHandlerDecorator loggingHandler = (WebSocketHandlerDecorator) client.webSocketHandler;
	assertEquals(LoggingWebSocketHandlerDecorator.class, loggingHandler.getClass());

	assertSame(handler, loggingHandler.getDelegate());
}
 
Example #4
Source File: DefaultHandshakeHandlerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void supportedSubProtocols() throws Exception {

	this.handshakeHandler.setSupportedProtocols("stomp", "mqtt");

	given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[] {"13"});

	this.servletRequest.setMethod("GET");

	WebSocketHttpHeaders headers = new WebSocketHttpHeaders(this.request.getHeaders());
	headers.setUpgrade("WebSocket");
	headers.setConnection("Upgrade");
	headers.setSecWebSocketVersion("13");
	headers.setSecWebSocketKey("82/ZS2YHjEnUN97HLL8tbw==");
	headers.setSecWebSocketProtocol("STOMP");

	WebSocketHandler handler = new TextWebSocketHandler();
	Map<String, Object> attributes = Collections.<String, Object>emptyMap();
	this.handshakeHandler.doHandshake(this.request, this.response, handler, attributes);

	verify(this.upgradeStrategy).upgrade(this.request, this.response,
			"STOMP", Collections.<WebSocketExtension>emptyList(), null, handler, attributes);
}
 
Example #5
Source File: DefaultHandshakeHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void supportedExtensions() {
	WebSocketExtension extension1 = new WebSocketExtension("ext1");
	WebSocketExtension extension2 = new WebSocketExtension("ext2");

	given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[] {"13"});
	given(this.upgradeStrategy.getSupportedExtensions(this.request)).willReturn(Collections.singletonList(extension1));

	this.servletRequest.setMethod("GET");

	WebSocketHttpHeaders headers = new WebSocketHttpHeaders(this.request.getHeaders());
	headers.setUpgrade("WebSocket");
	headers.setConnection("Upgrade");
	headers.setSecWebSocketVersion("13");
	headers.setSecWebSocketKey("82/ZS2YHjEnUN97HLL8tbw==");
	headers.setSecWebSocketExtensions(Arrays.asList(extension1, extension2));

	WebSocketHandler handler = new TextWebSocketHandler();
	Map<String, Object> attributes = Collections.<String, Object>emptyMap();
	this.handshakeHandler.doHandshake(this.request, this.response, handler, attributes);

	verify(this.upgradeStrategy).upgrade(this.request, this.response, null,
			Collections.singletonList(extension1), null, handler, attributes);
}
 
Example #6
Source File: WebSocketConnectionManagerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void openConnection() throws Exception {
	List<String> subprotocols = Arrays.asList("abc");

	TestLifecycleWebSocketClient client = new TestLifecycleWebSocketClient(false);
	WebSocketHandler handler = new TextWebSocketHandler();

	WebSocketConnectionManager manager = new WebSocketConnectionManager(client, handler , "/path/{id}", "123");
	manager.setSubProtocols(subprotocols);
	manager.openConnection();

	WebSocketHttpHeaders expectedHeaders = new WebSocketHttpHeaders();
	expectedHeaders.setSecWebSocketProtocol(subprotocols);

	assertEquals(expectedHeaders, client.headers);
	assertEquals(new URI("/path/123"), client.uri);

	WebSocketHandlerDecorator loggingHandler = (WebSocketHandlerDecorator) client.webSocketHandler;
	assertEquals(LoggingWebSocketHandlerDecorator.class, loggingHandler.getClass());

	assertSame(handler, loggingHandler.getDelegate());
}
 
Example #7
Source File: WebSocketHandlerRegistrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void interceptors() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor);

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo", mapping.path);
	assertNotNull(mapping.interceptors);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #8
Source File: WebSocketHandlerRegistrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void emptyAllowedOrigin() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor).setAllowedOrigins();

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo", mapping.path);
	assertNotNull(mapping.interceptors);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #9
Source File: WebSocketHandlerRegistrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void interceptorsWithAllowedOrigins() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor).setAllowedOrigins("http://mydomain1.com");

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo", mapping.path);
	assertNotNull(mapping.interceptors);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #10
Source File: WebSocketHandlerRegistrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void interceptorsPassedToSockJsRegistration() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo")
			.addInterceptors(interceptor)
			.setAllowedOrigins("http://mydomain1.com")
			.withSockJS();

	this.registration.getSockJsServiceRegistration().setTaskScheduler(this.taskScheduler);

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo/**", mapping.path);
	assertNotNull(mapping.sockJsService);
	assertTrue(mapping.sockJsService.getAllowedOrigins().contains("http://mydomain1.com"));
	List<HandshakeInterceptor> interceptors = mapping.sockJsService.getHandshakeInterceptors();
	assertEquals(interceptor, interceptors.get(0));
	assertEquals(OriginHandshakeInterceptor.class, interceptors.get(1).getClass());
}
 
Example #11
Source File: WebSocketHandlerRegistrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void handshakeHandlerPassedToSockJsRegistration() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HandshakeHandler handshakeHandler = new DefaultHandshakeHandler();

	this.registration.addHandler(handler, "/foo").setHandshakeHandler(handshakeHandler).withSockJS();
	this.registration.getSockJsServiceRegistration().setTaskScheduler(this.taskScheduler);

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo/**", mapping.path);
	assertNotNull(mapping.sockJsService);

	WebSocketTransportHandler transportHandler =
			(WebSocketTransportHandler) mapping.sockJsService.getTransportHandlers().get(TransportType.WEBSOCKET);
	assertSame(handshakeHandler, transportHandler.getHandshakeHandler());
}
 
Example #12
Source File: WebSocketConnectionManagerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void openConnection() throws Exception {
	List<String> subprotocols = Arrays.asList("abc");

	TestLifecycleWebSocketClient client = new TestLifecycleWebSocketClient(false);
	WebSocketHandler handler = new TextWebSocketHandler();

	WebSocketConnectionManager manager = new WebSocketConnectionManager(client, handler , "/path/{id}", "123");
	manager.setSubProtocols(subprotocols);
	manager.openConnection();

	WebSocketHttpHeaders expectedHeaders = new WebSocketHttpHeaders();
	expectedHeaders.setSecWebSocketProtocol(subprotocols);

	assertEquals(expectedHeaders, client.headers);
	assertEquals(new URI("/path/123"), client.uri);

	WebSocketHandlerDecorator loggingHandler = (WebSocketHandlerDecorator) client.webSocketHandler;
	assertEquals(LoggingWebSocketHandlerDecorator.class, loggingHandler.getClass());

	assertSame(handler, loggingHandler.getDelegate());
}
 
Example #13
Source File: JettyWebSocketClientTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void doHandshake() throws Exception {

	WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
	headers.setSecWebSocketProtocol(Arrays.asList("echo"));

	this.wsSession = this.client.doHandshake(new TextWebSocketHandler(), headers, new URI(this.wsUrl)).get();

	assertEquals(this.wsUrl, this.wsSession.getUri().toString());
	assertEquals("echo", this.wsSession.getAcceptedProtocol());
}
 
Example #14
Source File: WebSocketHandlerRegistrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void minimal() {
	WebSocketHandler handler = new TextWebSocketHandler();
	this.registration.addHandler(handler, "/foo", "/bar");

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(2, mappings.size());

	Mapping m1 = mappings.get(0);
	assertEquals(handler, m1.webSocketHandler);
	assertEquals("/foo", m1.path);
	assertNotNull(m1.interceptors);
	assertEquals(1, m1.interceptors.length);
	assertEquals(OriginHandshakeInterceptor.class, m1.interceptors[0].getClass());

	Mapping m2 = mappings.get(1);
	assertEquals(handler, m2.webSocketHandler);
	assertEquals("/bar", m2.path);
	assertNotNull(m2.interceptors);
	assertEquals(1, m2.interceptors.length);
	assertEquals(OriginHandshakeInterceptor.class, m2.interceptors[0].getClass());
}
 
Example #15
Source File: DefaultHandshakeHandlerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void supportedExtensions() throws Exception {

	WebSocketExtension extension1 = new WebSocketExtension("ext1");
	WebSocketExtension extension2 = new WebSocketExtension("ext2");

	given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[] {"13"});
	given(this.upgradeStrategy.getSupportedExtensions(this.request)).willReturn(Arrays.asList(extension1));

	this.servletRequest.setMethod("GET");

	WebSocketHttpHeaders headers = new WebSocketHttpHeaders(this.request.getHeaders());
	headers.setUpgrade("WebSocket");
	headers.setConnection("Upgrade");
	headers.setSecWebSocketVersion("13");
	headers.setSecWebSocketKey("82/ZS2YHjEnUN97HLL8tbw==");
	headers.setSecWebSocketExtensions(Arrays.asList(extension1, extension2));

	WebSocketHandler handler = new TextWebSocketHandler();
	Map<String, Object> attributes = Collections.<String, Object>emptyMap();
	this.handshakeHandler.doHandshake(this.request, this.response, handler, attributes);

	verify(this.upgradeStrategy).upgrade(this.request, this.response, null, Arrays.asList(extension1),
			null, handler, attributes);
}
 
Example #16
Source File: WebSocketHandlerRegistrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void minimal() {
	WebSocketHandler handler = new TextWebSocketHandler();
	this.registration.addHandler(handler, "/foo", "/bar");

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(2, mappings.size());

	Mapping m1 = mappings.get(0);
	assertEquals(handler, m1.webSocketHandler);
	assertEquals("/foo", m1.path);
	assertEquals(1, m1.interceptors.length);
	assertEquals(OriginHandshakeInterceptor.class, m1.interceptors[0].getClass());

	Mapping m2 = mappings.get(1);
	assertEquals(handler, m2.webSocketHandler);
	assertEquals("/bar", m2.path);
	assertEquals(1, m2.interceptors.length);
	assertEquals(OriginHandshakeInterceptor.class, m2.interceptors[0].getClass());
}
 
Example #17
Source File: WebSocketHandlerRegistrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void interceptors() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor);

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo", mapping.path);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #18
Source File: WebSocketHandlerRegistrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void emptyAllowedOrigin() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor).setAllowedOrigins();

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo", mapping.path);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #19
Source File: WebSocketHandlerRegistrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void interceptorsWithAllowedOrigins() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor).setAllowedOrigins("http://mydomain1.com");

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo", mapping.path);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #20
Source File: WebSocketHandlerRegistrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void interceptorsPassedToSockJsRegistration() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor)
			.setAllowedOrigins("http://mydomain1.com").withSockJS();

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo/**", mapping.path);
	assertNotNull(mapping.sockJsService);
	assertTrue(mapping.sockJsService.getAllowedOrigins().contains("http://mydomain1.com"));
	List<HandshakeInterceptor> interceptors = mapping.sockJsService.getHandshakeInterceptors();
	assertEquals(interceptor, interceptors.get(0));
	assertEquals(OriginHandshakeInterceptor.class, interceptors.get(1).getClass());
}
 
Example #21
Source File: WebSocketHandlerRegistrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void handshakeHandlerPassedToSockJsRegistration() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HandshakeHandler handshakeHandler = new DefaultHandshakeHandler();

	this.registration.addHandler(handler, "/foo").setHandshakeHandler(handshakeHandler).withSockJS();

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo/**", mapping.path);
	assertNotNull(mapping.sockJsService);

	WebSocketTransportHandler transportHandler =
			(WebSocketTransportHandler) mapping.sockJsService.getTransportHandlers().get(TransportType.WEBSOCKET);
	assertSame(handshakeHandler, transportHandler.getHandshakeHandler());
}
 
Example #22
Source File: WebSocketHandlerRegistrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void minimal() {
	WebSocketHandler handler = new TextWebSocketHandler();
	this.registration.addHandler(handler, "/foo", "/bar");

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(2, mappings.size());

	Mapping m1 = mappings.get(0);
	assertEquals(handler, m1.webSocketHandler);
	assertEquals("/foo", m1.path);
	assertNotNull(m1.interceptors);
	assertEquals(1, m1.interceptors.length);
	assertEquals(OriginHandshakeInterceptor.class, m1.interceptors[0].getClass());

	Mapping m2 = mappings.get(1);
	assertEquals(handler, m2.webSocketHandler);
	assertEquals("/bar", m2.path);
	assertNotNull(m2.interceptors);
	assertEquals(1, m2.interceptors.length);
	assertEquals(OriginHandshakeInterceptor.class, m2.interceptors[0].getClass());
}
 
Example #23
Source File: WebSocketHandlerRegistrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void handshakeHandlerPassedToSockJsRegistration() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HandshakeHandler handshakeHandler = new DefaultHandshakeHandler();

	this.registration.addHandler(handler, "/foo").setHandshakeHandler(handshakeHandler).withSockJS();
	this.registration.getSockJsServiceRegistration().setTaskScheduler(this.taskScheduler);

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo/**", mapping.path);
	assertNotNull(mapping.sockJsService);

	WebSocketTransportHandler transportHandler =
			(WebSocketTransportHandler) mapping.sockJsService.getTransportHandlers().get(TransportType.WEBSOCKET);
	assertSame(handshakeHandler, transportHandler.getHandshakeHandler());
}
 
Example #24
Source File: WebSocketHandlerRegistrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void interceptors() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor);

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo", mapping.path);
	assertNotNull(mapping.interceptors);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #25
Source File: WebSocketHandlerRegistrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void emptyAllowedOrigin() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor).setAllowedOrigins();

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo", mapping.path);
	assertNotNull(mapping.interceptors);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #26
Source File: WebSocketHandlerRegistrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void interceptorsPassedToSockJsRegistration() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo")
			.addInterceptors(interceptor)
			.setAllowedOrigins("https://mydomain1.com")
			.withSockJS();

	this.registration.getSockJsServiceRegistration().setTaskScheduler(this.taskScheduler);

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo/**", mapping.path);
	assertNotNull(mapping.sockJsService);
	assertTrue(mapping.sockJsService.getAllowedOrigins().contains("https://mydomain1.com"));
	List<HandshakeInterceptor> interceptors = mapping.sockJsService.getHandshakeInterceptors();
	assertEquals(interceptor, interceptors.get(0));
	assertEquals(OriginHandshakeInterceptor.class, interceptors.get(1).getClass());
}
 
Example #27
Source File: DefaultHandshakeHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void supportedSubProtocols() {
	this.handshakeHandler.setSupportedProtocols("stomp", "mqtt");
	given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[] {"13"});
	this.servletRequest.setMethod("GET");

	WebSocketHttpHeaders headers = new WebSocketHttpHeaders(this.request.getHeaders());
	headers.setUpgrade("WebSocket");
	headers.setConnection("Upgrade");
	headers.setSecWebSocketVersion("13");
	headers.setSecWebSocketKey("82/ZS2YHjEnUN97HLL8tbw==");
	headers.setSecWebSocketProtocol("STOMP");

	WebSocketHandler handler = new TextWebSocketHandler();
	Map<String, Object> attributes = Collections.emptyMap();
	this.handshakeHandler.doHandshake(this.request, this.response, handler, attributes);

	verify(this.upgradeStrategy).upgrade(this.request, this.response, "STOMP",
			Collections.emptyList(), null, handler, attributes);
}
 
Example #28
Source File: WebSocketHandlerRegistrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void interceptorsWithAllowedOrigins() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor).setAllowedOrigins("https://mydomain1.com");

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo", mapping.path);
	assertNotNull(mapping.interceptors);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #29
Source File: WebSocketConnectionManagerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void clientLifecycle() throws Exception {
	TestLifecycleWebSocketClient client = new TestLifecycleWebSocketClient(false);
	WebSocketHandler handler = new TextWebSocketHandler();
	WebSocketConnectionManager manager = new WebSocketConnectionManager(client, handler , "/a");

	manager.startInternal();
	assertTrue(client.isRunning());

	manager.stopInternal();
	assertFalse(client.isRunning());
}
 
Example #30
Source File: WebSocketConnectionManagerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void clientLifecycle() throws Exception {
	TestLifecycleWebSocketClient client = new TestLifecycleWebSocketClient(false);
	WebSocketHandler handler = new TextWebSocketHandler();
	WebSocketConnectionManager manager = new WebSocketConnectionManager(client, handler , "/a");

	manager.startInternal();
	assertTrue(client.isRunning());

	manager.stopInternal();
	assertFalse(client.isRunning());
}