org.springframework.web.socket.WebSocketHttpHeaders Java Examples

The following examples show how to use org.springframework.web.socket.WebSocketHttpHeaders. 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: 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 #2
Source File: SockJsClientTests.java    From java-technology-stack with MIT License 7 votes vote down vote up
@Test
public void connectWithHandshakeHeaders() throws Exception {
	ArgumentCaptor<HttpHeaders> headersCaptor = setupInfoRequest(false);
	this.xhrTransport.setStreamingDisabled(true);

	WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
	headers.set("foo", "bar");
	headers.set("auth", "123");
	this.sockJsClient.doHandshake(handler, headers, new URI(URL)).addCallback(this.connectCallback);

	HttpHeaders httpHeaders = headersCaptor.getValue();
	assertEquals(2, httpHeaders.size());
	assertEquals("bar", httpHeaders.getFirst("foo"));
	assertEquals("123", httpHeaders.getFirst("auth"));

	httpHeaders = this.xhrTransport.getRequest().getHttpRequestHeaders();
	assertEquals(2, httpHeaders.size());
	assertEquals("bar", httpHeaders.getFirst("foo"));
	assertEquals("123", httpHeaders.getFirst("auth"));
}
 
Example #3
Source File: WebSocketTransport.java    From java-technology-stack with MIT License 7 votes vote down vote up
@Override
public ListenableFuture<WebSocketSession> connect(TransportRequest request, WebSocketHandler handler) {
	final SettableListenableFuture<WebSocketSession> future = new SettableListenableFuture<>();
	WebSocketClientSockJsSession session = new WebSocketClientSockJsSession(request, handler, future);
	handler = new ClientSockJsWebSocketHandler(session);
	request.addTimeoutTask(session.getTimeoutTask());

	URI url = request.getTransportUrl();
	WebSocketHttpHeaders headers = new WebSocketHttpHeaders(request.getHandshakeHeaders());
	if (logger.isDebugEnabled()) {
		logger.debug("Starting WebSocket session on " + url);
	}
	this.webSocketClient.doHandshake(handler, headers, url).addCallback(
			new ListenableFutureCallback<WebSocketSession>() {
				@Override
				public void onSuccess(@Nullable WebSocketSession webSocketSession) {
					// WebSocket session ready, SockJS Session not yet
				}
				@Override
				public void onFailure(Throwable ex) {
					future.setException(ex);
				}
			});
	return future;
}
 
Example #4
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 #5
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 #6
Source File: DefaultHandshakeHandlerTests.java    From spring4-understanding with Apache License 2.0 7 votes vote down vote up
@Test
public void subProtocolCapableHandlerNoMatch() throws Exception {

	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("v10.stomp");

	WebSocketHandler handler = new SubProtocolCapableHandler("v12.stomp", "v11.stomp");
	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.<WebSocketExtension>emptyList(), null, handler, attributes);
}
 
Example #7
Source File: SockJsClientTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void connectWithHandshakeHeaders() throws Exception {
	ArgumentCaptor<HttpHeaders> headersCaptor = setupInfoRequest(false);
	this.xhrTransport.setStreamingDisabled(true);

	WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
	headers.set("foo", "bar");
	headers.set("auth", "123");
	this.sockJsClient.doHandshake(handler, headers, new URI(URL)).addCallback(this.connectCallback);

	HttpHeaders httpHeaders = headersCaptor.getValue();
	assertEquals(2, httpHeaders.size());
	assertEquals("bar", httpHeaders.getFirst("foo"));
	assertEquals("123", httpHeaders.getFirst("auth"));

	httpHeaders = this.xhrTransport.getRequest().getHttpRequestHeaders();
	assertEquals(2, httpHeaders.size());
	assertEquals("bar", httpHeaders.getFirst("foo"));
	assertEquals("123", httpHeaders.getFirst("auth"));
}
 
Example #8
Source File: WebSocketHttpHeadersTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void parseWebSocketExtensions() {
	List<String> extensions = new ArrayList<String>();
	extensions.add("x-foo-extension, x-bar-extension");
	extensions.add("x-test-extension");
	this.headers.put(WebSocketHttpHeaders.SEC_WEBSOCKET_EXTENSIONS, extensions);

	List<WebSocketExtension> parsedExtensions = this.headers.getSecWebSocketExtensions();
	assertThat(parsedExtensions, Matchers.hasSize(3));
}
 
Example #9
Source File: WebSocketTransport.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public ListenableFuture<WebSocketSession> connect(TransportRequest request, WebSocketHandler handler) {
	final SettableListenableFuture<WebSocketSession> future = new SettableListenableFuture<WebSocketSession>();
	WebSocketClientSockJsSession session = new WebSocketClientSockJsSession(request, handler, future);
	handler = new ClientSockJsWebSocketHandler(session);
	request.addTimeoutTask(session.getTimeoutTask());

	URI url = request.getTransportUrl();
	WebSocketHttpHeaders headers = new WebSocketHttpHeaders(request.getHandshakeHeaders());
	if (logger.isDebugEnabled()) {
		logger.debug("Starting WebSocket session url=" + url);
	}
	this.webSocketClient.doHandshake(handler, headers, url).addCallback(
			new ListenableFutureCallback<WebSocketSession>() {
				@Override
				public void onSuccess(WebSocketSession webSocketSession) {
					// WebSocket session ready, SockJS Session not yet
				}
				@Override
				public void onFailure(Throwable ex) {
					future.setException(ex);
				}
			});
	return future;
}
 
Example #10
Source File: AbstractHandshakeHandler.java    From spring-analysis-note with MIT License 6 votes vote down vote up
protected void handleWebSocketVersionNotSupported(ServerHttpRequest request, ServerHttpResponse response) {
	if (logger.isErrorEnabled()) {
		String version = request.getHeaders().getFirst("Sec-WebSocket-Version");
		logger.error("Handshake failed due to unsupported WebSocket version: " + version +
				". Supported versions: " + Arrays.toString(getSupportedVersions()));
	}
	response.setStatusCode(HttpStatus.UPGRADE_REQUIRED);
	response.getHeaders().set(WebSocketHttpHeaders.SEC_WEBSOCKET_VERSION,
			StringUtils.arrayToCommaDelimitedString(getSupportedVersions()));
}
 
Example #11
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 #12
Source File: SockJsClient.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public final ListenableFuture<WebSocketSession> doHandshake(
		WebSocketHandler handler, @Nullable WebSocketHttpHeaders headers, URI url) {

	Assert.notNull(handler, "WebSocketHandler is required");
	Assert.notNull(url, "URL is required");

	String scheme = url.getScheme();
	if (!supportedProtocols.contains(scheme)) {
		throw new IllegalArgumentException("Invalid scheme: '" + scheme + "'");
	}

	SettableListenableFuture<WebSocketSession> connectFuture = new SettableListenableFuture<>();
	try {
		SockJsUrlInfo sockJsUrlInfo = new SockJsUrlInfo(url);
		ServerInfo serverInfo = getServerInfo(sockJsUrlInfo, getHttpRequestHeaders(headers));
		createRequest(sockJsUrlInfo, headers, serverInfo).connect(handler, connectFuture);
	}
	catch (Throwable exception) {
		if (logger.isErrorEnabled()) {
			logger.error("Initial SockJS \"Info\" request to server failed, url=" + url, exception);
		}
		connectFuture.setException(exception);
	}
	return connectFuture;
}
 
Example #13
Source File: AuthChannelInterceptorAdapterWebAppTest.java    From joal with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldCallAuthServiceWhenUserTriesToConnect() throws InterruptedException, ExecutionException, TimeoutException {
    final WebSocketStompClient stompClient = new WebSocketStompClient(new StandardWebSocketClient());

    final StompHeaders stompHeaders = new StompHeaders();
    stompHeaders.add(AuthChannelInterceptorAdapter.USERNAME_HEADER, "john");
    stompHeaders.add(AuthChannelInterceptorAdapter.TOKEN_HEADER, TestConstant.UI_SECRET_TOKEN);

    stompClient.connect("ws://localhost:" + port + "/" + TestConstant.UI_PATH_PREFIX, new WebSocketHttpHeaders(), stompHeaders, new StompSessionHandlerAdapter() {
    }).get(10, TimeUnit.SECONDS);

    verify(authenticatorService, times(1)).getAuthenticatedOrFail("john", TestConstant.UI_SECRET_TOKEN);
}
 
Example #14
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 #15
Source File: DefaultHandshakeHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void subProtocolCapableHandler() {
	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("v11.stomp");

	WebSocketHandler handler = new SubProtocolCapableHandler("v12.stomp", "v11.stomp");
	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, "v11.stomp",
			Collections.emptyList(), null, handler, attributes);
}
 
Example #16
Source File: WebSocketTransport.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public ListenableFuture<WebSocketSession> connect(TransportRequest request, WebSocketHandler handler) {
	final SettableListenableFuture<WebSocketSession> future = new SettableListenableFuture<>();
	WebSocketClientSockJsSession session = new WebSocketClientSockJsSession(request, handler, future);
	handler = new ClientSockJsWebSocketHandler(session);
	request.addTimeoutTask(session.getTimeoutTask());

	URI url = request.getTransportUrl();
	WebSocketHttpHeaders headers = new WebSocketHttpHeaders(request.getHandshakeHeaders());
	if (logger.isDebugEnabled()) {
		logger.debug("Starting WebSocket session on " + url);
	}
	this.webSocketClient.doHandshake(handler, headers, url).addCallback(
			new ListenableFutureCallback<WebSocketSession>() {
				@Override
				public void onSuccess(@Nullable WebSocketSession webSocketSession) {
					// WebSocket session ready, SockJS Session not yet
				}
				@Override
				public void onFailure(Throwable ex) {
					future.setException(ex);
				}
			});
	return future;
}
 
Example #17
Source File: SockJsClientTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void connectWithHandshakeHeaders() throws Exception {
	ArgumentCaptor<HttpHeaders> headersCaptor = setupInfoRequest(false);
	this.xhrTransport.setStreamingDisabled(true);

	WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
	headers.set("foo", "bar");
	headers.set("auth", "123");
	this.sockJsClient.doHandshake(handler, headers, new URI(URL)).addCallback(this.connectCallback);

	HttpHeaders httpHeaders = headersCaptor.getValue();
	assertEquals(2, httpHeaders.size());
	assertEquals("bar", httpHeaders.getFirst("foo"));
	assertEquals("123", httpHeaders.getFirst("auth"));

	httpHeaders = this.xhrTransport.getRequest().getHttpRequestHeaders();
	assertEquals(2, httpHeaders.size());
	assertEquals("bar", httpHeaders.getFirst("foo"));
	assertEquals("123", httpHeaders.getFirst("auth"));
}
 
Example #18
Source File: SockJsClientTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void connectAndUseSubsetOfHandshakeHeadersForHttpRequests() throws Exception {
	ArgumentCaptor<HttpHeaders> headersCaptor = setupInfoRequest(false);
	this.xhrTransport.setStreamingDisabled(true);

	WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
	headers.set("foo", "bar");
	headers.set("auth", "123");
	this.sockJsClient.setHttpHeaderNames("auth");
	this.sockJsClient.doHandshake(handler, headers, new URI(URL)).addCallback(this.connectCallback);

	assertEquals(1, headersCaptor.getValue().size());
	assertEquals("123", headersCaptor.getValue().getFirst("auth"));
	assertEquals(1, this.xhrTransport.getRequest().getHttpRequestHeaders().size());
	assertEquals("123", this.xhrTransport.getRequest().getHttpRequestHeaders().getFirst("auth"));
}
 
Example #19
Source File: DefaultHandshakeHandlerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void subProtocolCapableHandler() throws Exception {

	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("v11.stomp");

	WebSocketHandler handler = new SubProtocolCapableHandler("v12.stomp", "v11.stomp");
	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,
			"v11.stomp", Collections.<WebSocketExtension>emptyList(), null, handler, attributes);
}
 
Example #20
Source File: RtWebSocketClient.java    From redtorch with MIT License 6 votes vote down vote up
public void connect() {
	try {
		if (usingHttpSession) {
			webSocketClient.doHandshake(webSocketHandler, new WebSocketHttpHeaders(httpHeaders), URI.create(masterServerUri + "?nodeId="
					+ URLEncoder.encode(nodeId + "", "utf-8") + "&skipTradeEvents=" + URLEncoder.encode(skipTradeEvents + "", "utf-8"))).get();
		} else {
			webSocketClient.doHandshake(webSocketHandler, new WebSocketHttpHeaders(),
					URI.create(masterServerUri + "?nodeId=" + URLEncoder.encode(nodeId + "", "utf-8") + "&token=" + URLEncoder.encode(token, "utf-8")
							+ "&skipTradeEvents=" + URLEncoder.encode(skipTradeEvents + "", "utf-8") + "&operatorId="
							+ URLEncoder.encode(operatorId, "utf-8")))
					.get();
		}

	} catch (Exception e) {
		logger.error("连接服务器发生错误", e);
	}
}
 
Example #21
Source File: WebSocketProxyTest.java    From api-layer with Eclipse Public License 2.0 6 votes vote down vote up
@Test
@WebsocketTest
public void shouldRouteHeaders() throws Exception {
    final StringBuilder response = new StringBuilder();
    WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
    headers.add("X-Test", "value");
    WebSocketSession session = appendingWebSocketSession(discoverableClientGatewayUrl(HEADER_URL), headers, response, 1);

    session.sendMessage(new TextMessage("gimme those headers"));
    synchronized (response) {
        response.wait(WAIT_TIMEOUT_MS);
    }

    assertTrue(response.toString().contains("x-test=[value]"));
    session.sendMessage(new TextMessage("bye"));
    session.close();
}
 
Example #22
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 #23
Source File: SockJsClient.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public final ListenableFuture<WebSocketSession> doHandshake(
		WebSocketHandler handler, @Nullable WebSocketHttpHeaders headers, URI url) {

	Assert.notNull(handler, "WebSocketHandler is required");
	Assert.notNull(url, "URL is required");

	String scheme = url.getScheme();
	if (!supportedProtocols.contains(scheme)) {
		throw new IllegalArgumentException("Invalid scheme: '" + scheme + "'");
	}

	SettableListenableFuture<WebSocketSession> connectFuture = new SettableListenableFuture<>();
	try {
		SockJsUrlInfo sockJsUrlInfo = new SockJsUrlInfo(url);
		ServerInfo serverInfo = getServerInfo(sockJsUrlInfo, getHttpRequestHeaders(headers));
		createRequest(sockJsUrlInfo, headers, serverInfo).connect(handler, connectFuture);
	}
	catch (Throwable exception) {
		if (logger.isErrorEnabled()) {
			logger.error("Initial SockJS \"Info\" request to server failed, url=" + url, exception);
		}
		connectFuture.setException(exception);
	}
	return connectFuture;
}
 
Example #24
Source File: SockJsClientTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void connectAndUseSubsetOfHandshakeHeadersForHttpRequests() throws Exception {
	ArgumentCaptor<HttpHeaders> headersCaptor = setupInfoRequest(false);
	this.xhrTransport.setStreamingDisabled(true);

	WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
	headers.set("foo", "bar");
	headers.set("auth", "123");
	this.sockJsClient.setHttpHeaderNames("auth");
	this.sockJsClient.doHandshake(handler, headers, new URI(URL)).addCallback(this.connectCallback);

	assertEquals(1, headersCaptor.getValue().size());
	assertEquals("123", headersCaptor.getValue().getFirst("auth"));
	assertEquals(1, this.xhrTransport.getRequest().getHttpRequestHeaders().size());
	assertEquals("123", this.xhrTransport.getRequest().getHttpRequestHeaders().getFirst("auth"));
}
 
Example #25
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 #26
Source File: DefaultHandshakeHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void subProtocolCapableHandler() {
	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("v11.stomp");

	WebSocketHandler handler = new SubProtocolCapableHandler("v12.stomp", "v11.stomp");
	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, "v11.stomp",
			Collections.emptyList(), null, handler, attributes);
}
 
Example #27
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 #28
Source File: DefaultHandshakeHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void subProtocolCapableHandlerNoMatch() {
	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("v10.stomp");

	WebSocketHandler handler = new SubProtocolCapableHandler("v12.stomp", "v11.stomp");
	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.emptyList(), null, handler, attributes);
}
 
Example #29
Source File: AbstractSockJsIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
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);
}
 
Example #30
Source File: WebSocketConnectionManagerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler handler, WebSocketHttpHeaders headers, URI uri) {
	this.webSocketHandler = handler;
	this.headers = headers;
	this.uri = uri;
	return new ListenableFutureTask<>(() -> null);
}