Skip to content

Commit

Permalink
Use URI#create instead of URI constructor where feasible in spring-we…
Browse files Browse the repository at this point in the history
…bsocket
  • Loading branch information
sbrannen committed Dec 9, 2022
1 parent 284cb12 commit 9b38e43
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 103 deletions.
Expand Up @@ -59,7 +59,7 @@ void subProtocolNegotiation(WebSocketTestServer server, WebSocketClient webSocke

WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
headers.setSecWebSocketProtocol("foo");
URI url = new URI(getWsBaseUrl() + "/ws");
URI url = URI.create(getWsBaseUrl() + "/ws");
WebSocketSession session = this.webSocketClient.doHandshake(new TextWebSocketHandler(), headers, url).get();
assertThat(session.getAcceptedProtocol()).isEqualTo("foo");
session.close();
Expand Down
Expand Up @@ -39,11 +39,10 @@
*
* @author Rossen Stoyanchev
*/
public class WebSocketConnectionManagerTests {

class WebSocketConnectionManagerTests {

@Test
public void openConnection() throws Exception {
void openConnection() {
List<String> subprotocols = List.of("abc");

TestLifecycleWebSocketClient client = new TestLifecycleWebSocketClient(false);
Expand All @@ -57,7 +56,7 @@ public void openConnection() throws Exception {
expectedHeaders.setSecWebSocketProtocol(subprotocols);

assertThat(client.headers).isEqualTo(expectedHeaders);
assertThat(client.uri).isEqualTo(new URI("/path/123"));
assertThat(client.uri).isEqualTo(URI.create("/path/123"));

WebSocketHandlerDecorator loggingHandler = (WebSocketHandlerDecorator) client.webSocketHandler;
assertThat(loggingHandler.getClass()).isEqualTo(LoggingWebSocketHandlerDecorator.class);
Expand All @@ -66,7 +65,7 @@ public void openConnection() throws Exception {
}

@Test
public void clientLifecycle() throws Exception {
void clientLifecycle() throws Exception {
TestLifecycleWebSocketClient client = new TestLifecycleWebSocketClient(false);
WebSocketHandler handler = new TextWebSocketHandler();
WebSocketConnectionManager manager = new WebSocketConnectionManager(client, handler , "/a");
Expand Down
Expand Up @@ -25,7 +25,6 @@
import jakarta.websocket.ClientEndpointConfig;
import jakarta.websocket.Endpoint;
import jakarta.websocket.WebSocketContainer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

Expand All @@ -46,31 +45,21 @@
*
* @author Rossen Stoyanchev
*/
public class StandardWebSocketClientTests {
class StandardWebSocketClientTests {

private StandardWebSocketClient wsClient;
private final WebSocketHandler wsHandler = new AbstractWebSocketHandler() {};

private WebSocketContainer wsContainer;
private final WebSocketHttpHeaders headers = new WebSocketHttpHeaders();

private WebSocketHandler wsHandler;
private final WebSocketContainer wsContainer = mock(WebSocketContainer.class);

private WebSocketHttpHeaders headers;


@BeforeEach
public void setup() {
this.headers = new WebSocketHttpHeaders();
this.wsHandler = new AbstractWebSocketHandler() {
};
this.wsContainer = mock(WebSocketContainer.class);
this.wsClient = new StandardWebSocketClient(this.wsContainer);
}
private final StandardWebSocketClient wsClient = new StandardWebSocketClient(this.wsContainer);


@Test
@SuppressWarnings("deprecation")
public void testGetLocalAddress() throws Exception {
URI uri = new URI("ws://localhost/abc");
void getLocalAddress() throws Exception {
URI uri = URI.create("ws://localhost/abc");
WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();

assertThat(session.getLocalAddress()).isNotNull();
Expand All @@ -79,8 +68,8 @@ public void testGetLocalAddress() throws Exception {

@Test
@SuppressWarnings("deprecation")
public void testGetLocalAddressWss() throws Exception {
URI uri = new URI("wss://localhost/abc");
void getLocalAddressWss() throws Exception {
URI uri = URI.create("wss://localhost/abc");
WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();

assertThat(session.getLocalAddress()).isNotNull();
Expand All @@ -89,16 +78,16 @@ public void testGetLocalAddressWss() throws Exception {

@Test
@SuppressWarnings("deprecation")
public void testGetLocalAddressNoScheme() throws Exception {
URI uri = new URI("localhost/abc");
void getLocalAddressNoScheme() {
URI uri = URI.create("localhost/abc");
assertThatIllegalArgumentException().isThrownBy(() ->
this.wsClient.doHandshake(this.wsHandler, this.headers, uri));
}

@Test
@SuppressWarnings("deprecation")
public void testGetRemoteAddress() throws Exception {
URI uri = new URI("wss://localhost/abc");
void getRemoteAddress() throws Exception {
URI uri = URI.create("wss://localhost/abc");
WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();

assertThat(session.getRemoteAddress()).isNotNull();
Expand All @@ -108,8 +97,8 @@ public void testGetRemoteAddress() throws Exception {

@Test
@SuppressWarnings("deprecation")
public void handshakeHeaders() throws Exception {
URI uri = new URI("ws://localhost/abc");
void handshakeHeaders() throws Exception {
URI uri = URI.create("ws://localhost/abc");
List<String> protocols = Collections.singletonList("abc");
this.headers.setSecWebSocketProtocol(protocols);
this.headers.add("foo", "bar");
Expand All @@ -122,8 +111,8 @@ public void handshakeHeaders() throws Exception {

@Test
@SuppressWarnings("deprecation")
public void clientEndpointConfig() throws Exception {
URI uri = new URI("ws://localhost/abc");
void clientEndpointConfig() throws Exception {
URI uri = URI.create("ws://localhost/abc");
List<String> protocols = Collections.singletonList("abc");
this.headers.setSecWebSocketProtocol(protocols);

Expand All @@ -138,10 +127,10 @@ public void clientEndpointConfig() throws Exception {

@Test
@SuppressWarnings("deprecation")
public void clientEndpointConfigWithUserProperties() throws Exception {
void clientEndpointConfigWithUserProperties() throws Exception {
Map<String,Object> userProperties = Collections.singletonMap("foo", "bar");

URI uri = new URI("ws://localhost/abc");
URI uri = URI.create("ws://localhost/abc");
this.wsClient.setUserProperties(userProperties);
this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();

Expand All @@ -154,8 +143,8 @@ public void clientEndpointConfigWithUserProperties() throws Exception {

@Test
@SuppressWarnings("deprecation")
public void standardWebSocketClientConfiguratorInsertsHandshakeHeaders() throws Exception {
URI uri = new URI("ws://localhost/abc");
void standardWebSocketClientConfiguratorInsertsHandshakeHeaders() throws Exception {
URI uri = URI.create("ws://localhost/abc");
this.headers.add("foo", "bar");

this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();
Expand All @@ -171,8 +160,8 @@ public void standardWebSocketClientConfiguratorInsertsHandshakeHeaders() throws

@Test
@SuppressWarnings("deprecation")
public void taskExecutor() throws Exception {
URI uri = new URI("ws://localhost/abc");
void taskExecutor() throws Exception {
URI uri = URI.create("ws://localhost/abc");
this.wsClient.setTaskExecutor(new SimpleAsyncTaskExecutor());
WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();

Expand Down
Expand Up @@ -266,7 +266,7 @@ private void testEcho(int messageCount, Transport transport, WebSocketHttpHeader
}
TestClientHandler handler = new TestClientHandler();
initSockJsClient(transport);
URI url = new URI(this.baseUrl + "/echo");
URI url = URI.create(this.baseUrl + "/echo");
WebSocketSession session = this.sockJsClient.doHandshake(handler, headers, url).get();
for (TextMessage message : messages) {
session.sendMessage(message);
Expand All @@ -285,7 +285,7 @@ private void testReceiveOneMessage(Transport transport, WebSocketHttpHeaders hea

TestClientHandler clientHandler = new TestClientHandler();
initSockJsClient(transport);
this.sockJsClient.doHandshake(clientHandler, headers, new URI(this.baseUrl + "/test")).get();
this.sockJsClient.doHandshake(clientHandler, headers, URI.create(this.baseUrl + "/test")).get();
TestServerHandler serverHandler = this.wac.getBean(TestServerHandler.class);

assertThat(clientHandler.session).as("afterConnectionEstablished should have been called").isNotNull();
Expand Down
Expand Up @@ -48,7 +48,7 @@
*
* @author Rossen Stoyanchev
*/
public class ClientSockJsSessionTests {
class ClientSockJsSessionTests {

private static final Jackson2SockJsMessageCodec CODEC = new Jackson2SockJsMessageCodec();

Expand All @@ -60,8 +60,8 @@ public class ClientSockJsSessionTests {


@BeforeEach
public void setup() throws Exception {
SockJsUrlInfo urlInfo = new SockJsUrlInfo(new URI("https://example.com"));
void setup() {
SockJsUrlInfo urlInfo = new SockJsUrlInfo(URI.create("https://example.com"));
Transport transport = mock(Transport.class);
TransportRequest request = new DefaultTransportRequest(urlInfo, null, null, transport, TransportType.XHR, CODEC);
this.handler = mock(WebSocketHandler.class);
Expand All @@ -71,7 +71,7 @@ public void setup() throws Exception {


@Test
public void handleFrameOpen() throws Exception {
void handleFrameOpen() throws Exception {
assertThat(this.session.isOpen()).isFalse();
this.session.handleFrame(SockJsFrame.openFrame().getContent());
assertThat(this.session.isOpen()).isTrue();
Expand All @@ -82,22 +82,22 @@ public void handleFrameOpen() throws Exception {
}

@Test
public void handleFrameOpenWhenStatusNotNew() throws Exception {
void handleFrameOpenWhenStatusNotNew() throws Exception {
this.session.handleFrame(SockJsFrame.openFrame().getContent());
assertThat(this.session.isOpen()).isTrue();
this.session.handleFrame(SockJsFrame.openFrame().getContent());
assertThat(this.session.disconnectStatus).isEqualTo(new CloseStatus(1006, "Server lost session"));
}

@Test
public void handleFrameOpenWithWebSocketHandlerException() throws Exception {
void handleFrameOpenWithWebSocketHandlerException() throws Exception {
willThrow(new IllegalStateException("Fake error")).given(this.handler).afterConnectionEstablished(this.session);
this.session.handleFrame(SockJsFrame.openFrame().getContent());
assertThat(this.session.isOpen()).isTrue();
}

@Test
public void handleFrameMessage() throws Exception {
void handleFrameMessage() throws Exception {
this.session.handleFrame(SockJsFrame.openFrame().getContent());
this.session.handleFrame(SockJsFrame.messageFrame(CODEC, "foo", "bar").getContent());
verify(this.handler).afterConnectionEstablished(this.session);
Expand All @@ -107,7 +107,7 @@ public void handleFrameMessage() throws Exception {
}

@Test
public void handleFrameMessageWhenNotOpen() throws Exception {
void handleFrameMessageWhenNotOpen() throws Exception {
this.session.handleFrame(SockJsFrame.openFrame().getContent());
this.session.close();
reset(this.handler);
Expand All @@ -116,7 +116,7 @@ public void handleFrameMessageWhenNotOpen() throws Exception {
}

@Test
public void handleFrameMessageWithBadData() throws Exception {
void handleFrameMessageWithBadData() throws Exception {
this.session.handleFrame(SockJsFrame.openFrame().getContent());
this.session.handleFrame("a['bad data");
assertThat(this.session.isOpen()).isFalse();
Expand All @@ -126,7 +126,7 @@ public void handleFrameMessageWithBadData() throws Exception {
}

@Test
public void handleFrameMessageWithWebSocketHandlerException() throws Exception {
void handleFrameMessageWithWebSocketHandlerException() throws Exception {
this.session.handleFrame(SockJsFrame.openFrame().getContent());
willThrow(new IllegalStateException("Fake error")).given(this.handler)
.handleMessage(this.session, new TextMessage("foo"));
Expand All @@ -141,7 +141,7 @@ public void handleFrameMessageWithWebSocketHandlerException() throws Exception {
}

@Test
public void handleFrameClose() throws Exception {
void handleFrameClose() throws Exception {
this.session.handleFrame(SockJsFrame.openFrame().getContent());
this.session.handleFrame(SockJsFrame.closeFrame(1007, "").getContent());
assertThat(this.session.isOpen()).isFalse();
Expand All @@ -151,15 +151,15 @@ public void handleFrameClose() throws Exception {
}

@Test
public void handleTransportError() throws Exception {
void handleTransportError() throws Exception {
final IllegalStateException ex = new IllegalStateException("Fake error");
this.session.handleTransportError(ex);
verify(this.handler).handleTransportError(this.session, ex);
verifyNoMoreInteractions(this.handler);
}

@Test
public void afterTransportClosed() throws Exception {
void afterTransportClosed() throws Exception {
this.session.handleFrame(SockJsFrame.openFrame().getContent());
this.session.afterTransportClosed(CloseStatus.SERVER_ERROR);
assertThat(this.session.isOpen()).isFalse();
Expand All @@ -169,7 +169,7 @@ public void afterTransportClosed() throws Exception {
}

@Test
public void close() throws Exception {
void close() throws Exception {
this.session.handleFrame(SockJsFrame.openFrame().getContent());
this.session.close();
assertThat(this.session.isOpen()).isFalse();
Expand All @@ -179,36 +179,36 @@ public void close() throws Exception {
}

@Test
public void closeWithStatus() throws Exception {
void closeWithStatus() throws Exception {
this.session.handleFrame(SockJsFrame.openFrame().getContent());
this.session.close(new CloseStatus(3000, "reason"));
assertThat(this.session.disconnectStatus).isEqualTo(new CloseStatus(3000, "reason"));
}

@Test
public void closeWithNullStatus() throws Exception {
void closeWithNullStatus() throws Exception {
this.session.handleFrame(SockJsFrame.openFrame().getContent());
assertThatIllegalArgumentException().isThrownBy(() ->
this.session.close(null))
.withMessageContaining("Invalid close status");
}

@Test
public void closeWithStatusOutOfRange() throws Exception {
void closeWithStatusOutOfRange() throws Exception {
this.session.handleFrame(SockJsFrame.openFrame().getContent());
assertThatIllegalArgumentException().isThrownBy(() ->
this.session.close(new CloseStatus(2999, "reason")))
.withMessageContaining("Invalid close status");
}

@Test
public void timeoutTask() {
void timeoutTask() {
this.session.getTimeoutTask().run();
assertThat(this.session.disconnectStatus).isEqualTo(new CloseStatus(2007, "Transport timed out"));
}

@Test
public void send() throws Exception {
void send() throws Exception {
this.session.handleFrame(SockJsFrame.openFrame().getContent());
this.session.sendMessage(new TextMessage("foo"));
assertThat(this.session.sentMessage).isEqualTo(new TextMessage("[\"foo\"]"));
Expand Down

0 comments on commit 9b38e43

Please sign in to comment.