do not used fixed port in core WebSocketProxyTest

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-12-02 00:01:27 +11:00
parent 38aec0b081
commit b4fc70430c
2 changed files with 40 additions and 39 deletions

View File

@ -99,7 +99,7 @@ class WebSocketProxy
break; break;
default: default:
failure = new IllegalStateException(); failure = new IllegalStateException(state.name());
break; break;
} }
} }
@ -129,7 +129,7 @@ class WebSocketProxy
break; break;
default: default:
failure = new IllegalStateException(); failure = new IllegalStateException(state.name());
break; break;
} }
} }
@ -161,7 +161,7 @@ class WebSocketProxy
break; break;
default: default:
failure = new IllegalStateException(); failure = new IllegalStateException(state.name());
break; break;
} }
} }
@ -203,7 +203,7 @@ class WebSocketProxy
break; break;
default: default:
failure = new IllegalStateException(); failure = new IllegalStateException(state.name());
break; break;
} }
} }
@ -333,7 +333,7 @@ class WebSocketProxy
break; break;
default: default:
failure = new IllegalStateException(); failure = new IllegalStateException(state.name());
break; break;
} }
} }
@ -406,7 +406,7 @@ class WebSocketProxy
default: default:
state = State.FAILED; state = State.FAILED;
error = new IllegalStateException(); error = new IllegalStateException(state.name());
failure = error; failure = error;
break; break;
} }
@ -435,7 +435,7 @@ class WebSocketProxy
default: default:
state = State.FAILED; state = State.FAILED;
error = new IllegalStateException(); error = new IllegalStateException(state.name());
failure = error; failure = error;
break; break;
} }
@ -468,7 +468,7 @@ class WebSocketProxy
default: default:
state = State.FAILED; state = State.FAILED;
error = new IllegalStateException(); error = new IllegalStateException(state.name());
failure = error; failure = error;
break; break;
} }
@ -498,7 +498,7 @@ class WebSocketProxy
break; break;
default: default:
failure = new IllegalStateException(); failure = new IllegalStateException(state.name());
break; break;
} }
} }
@ -541,7 +541,7 @@ class WebSocketProxy
break; break;
default: default:
failure = new IllegalStateException(); failure = new IllegalStateException(state.name());
break; break;
} }
} }
@ -671,7 +671,7 @@ class WebSocketProxy
break; break;
default: default:
failure = new IllegalStateException(); failure = new IllegalStateException(state.name());
break; break;
} }
} }

View File

@ -21,11 +21,11 @@ package org.eclipse.jetty.websocket.core.proxy;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.time.Duration; import java.time.Duration;
import java.util.Objects;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -58,23 +58,22 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class WebSocketProxyTest public class WebSocketProxyTest
{ {
// Port chosen to (hopefully) not conflict with existing servers on your test machine.
// So don't choose ports like 8080, 9090, 8888, etc.
private static final int PROXY_PORT = 49999;
private Server _server; private Server _server;
private WebSocketCoreClient _client; private WebSocketCoreClient _client;
private WebSocketProxy proxy; private WebSocketProxy proxy;
private EchoFrameHandler serverFrameHandler; private EchoFrameHandler serverFrameHandler;
private TestHandler testHandler; private TestHandler testHandler;
Configuration.ConfigurationCustomizer defaultCustomizer; private Configuration.ConfigurationCustomizer defaultCustomizer;
private URI proxyUri;
private class TestHandler extends AbstractHandler private static class TestHandler extends AbstractHandler
{ {
public void blockServerUpgradeRequests() public void blockServerUpgradeRequests()
{ {
@ -84,7 +83,7 @@ public class WebSocketProxyTest
public boolean blockServerUpgradeRequests = false; public boolean blockServerUpgradeRequests = false;
@Override @Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
{ {
if (baseRequest.getHeader("Upgrade") != null) if (baseRequest.getHeader("Upgrade") != null)
{ {
@ -102,7 +101,6 @@ public class WebSocketProxyTest
{ {
_server = new Server(); _server = new Server();
ServerConnector connector = new ServerConnector(_server); ServerConnector connector = new ServerConnector(_server);
connector.setPort(PROXY_PORT);
_server.addConnector(connector); _server.addConnector(connector);
HandlerList handlers = new HandlerList(); HandlerList handlers = new HandlerList();
@ -119,12 +117,7 @@ public class WebSocketProxyTest
serverContext.setHandler(upgradeHandler); serverContext.setHandler(upgradeHandler);
handlers.addHandler(serverContext); handlers.addHandler(serverContext);
_client = new WebSocketCoreClient();
_client.start();
URI uri = new URI("ws://localhost:" + PROXY_PORT + "/server/");
ContextHandler proxyContext = new ContextHandler("/proxy"); ContextHandler proxyContext = new ContextHandler("/proxy");
proxy = new WebSocketProxy(_client, uri);
negotiator = WebSocketNegotiator.from((negotiation) -> proxy.client2Proxy, defaultCustomizer); negotiator = WebSocketNegotiator.from((negotiation) -> proxy.client2Proxy, defaultCustomizer);
upgradeHandler = new WebSocketUpgradeHandler(negotiator); upgradeHandler = new WebSocketUpgradeHandler(negotiator);
proxyContext.setHandler(upgradeHandler); proxyContext.setHandler(upgradeHandler);
@ -132,6 +125,12 @@ public class WebSocketProxyTest
_server.setHandler(handlers); _server.setHandler(handlers);
_server.start(); _server.start();
_client = new WebSocketCoreClient();
_client.start();
URI uri = new URI("ws://localhost:" + connector.getLocalPort());
proxyUri = uri.resolve("/proxy");
proxy = new WebSocketProxy(_client, uri.resolve("/server"));
} }
@AfterEach @AfterEach
@ -144,14 +143,10 @@ public class WebSocketProxyTest
public void awaitProxyClose(WebSocketProxy.Client2Proxy client2Proxy, WebSocketProxy.Server2Proxy server2Proxy) throws Exception public void awaitProxyClose(WebSocketProxy.Client2Proxy client2Proxy, WebSocketProxy.Server2Proxy server2Proxy) throws Exception
{ {
if (client2Proxy != null && !client2Proxy.closed.await(5, TimeUnit.SECONDS)) if (client2Proxy != null && !client2Proxy.closed.await(5, TimeUnit.SECONDS))
{
throw new TimeoutException("client2Proxy close timeout"); throw new TimeoutException("client2Proxy close timeout");
}
if (server2Proxy != null && !server2Proxy.closed.await(5, TimeUnit.SECONDS)) if (server2Proxy != null && !server2Proxy.closed.await(5, TimeUnit.SECONDS))
{
throw new TimeoutException("server2Proxy close timeout"); throw new TimeoutException("server2Proxy close timeout");
}
} }
@Test @Test
@ -161,7 +156,7 @@ public class WebSocketProxyTest
WebSocketProxy.Client2Proxy proxyClientSide = proxy.client2Proxy; WebSocketProxy.Client2Proxy proxyClientSide = proxy.client2Proxy;
WebSocketProxy.Server2Proxy proxyServerSide = proxy.server2Proxy; WebSocketProxy.Server2Proxy proxyServerSide = proxy.server2Proxy;
CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(_client, new URI("ws://localhost:" + PROXY_PORT + "/proxy/"), clientFrameHandler); CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(_client, proxyUri, clientFrameHandler);
upgradeRequest.setConfiguration(defaultCustomizer); upgradeRequest.setConfiguration(defaultCustomizer);
CompletableFuture<CoreSession> response = _client.connect(upgradeRequest); CompletableFuture<CoreSession> response = _client.connect(upgradeRequest);
@ -175,10 +170,10 @@ public class WebSocketProxyTest
assertThat(proxyClientSide.getState(), is(WebSocketProxy.State.CLOSED)); assertThat(proxyClientSide.getState(), is(WebSocketProxy.State.CLOSED));
assertThat(proxyServerSide.getState(), is(WebSocketProxy.State.CLOSED)); assertThat(proxyServerSide.getState(), is(WebSocketProxy.State.CLOSED));
assertThat(proxyClientSide.receivedFrames.poll().getPayloadAsUTF8(), is("hello world")); assertThat(Objects.requireNonNull(proxyClientSide.receivedFrames.poll()).getPayloadAsUTF8(), is("hello world"));
assertThat(serverFrameHandler.receivedFrames.poll().getPayloadAsUTF8(), is("hello world")); assertThat(Objects.requireNonNull(serverFrameHandler.receivedFrames.poll()).getPayloadAsUTF8(), is("hello world"));
assertThat(proxyServerSide.receivedFrames.poll().getPayloadAsUTF8(), is("hello world")); assertThat(Objects.requireNonNull(proxyServerSide.receivedFrames.poll()).getPayloadAsUTF8(), is("hello world"));
assertThat(clientFrameHandler.receivedFrames.poll().getPayloadAsUTF8(), is("hello world")); assertThat(Objects.requireNonNull(clientFrameHandler.receivedFrames.poll()).getPayloadAsUTF8(), is("hello world"));
assertThat(CloseStatus.getCloseStatus(proxyClientSide.receivedFrames.poll()).getReason(), is("standard close")); assertThat(CloseStatus.getCloseStatus(proxyClientSide.receivedFrames.poll()).getReason(), is("standard close"));
assertThat(CloseStatus.getCloseStatus(serverFrameHandler.receivedFrames.poll()).getReason(), is("standard close")); assertThat(CloseStatus.getCloseStatus(serverFrameHandler.receivedFrames.poll()).getReason(), is("standard close"));
@ -199,9 +194,9 @@ public class WebSocketProxyTest
WebSocketProxy.Server2Proxy proxyServerSide = proxy.server2Proxy; WebSocketProxy.Server2Proxy proxyServerSide = proxy.server2Proxy;
TestAsyncFrameHandler clientFrameHandler = new TestAsyncFrameHandler("CLIENT"); TestAsyncFrameHandler clientFrameHandler = new TestAsyncFrameHandler("CLIENT");
try (StacklessLogging stacklessLogging = new StacklessLogging(WebSocketCoreSession.class)) try (StacklessLogging ignored = new StacklessLogging(WebSocketCoreSession.class))
{ {
CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(_client, new URI("ws://localhost:" + PROXY_PORT + "/proxy/"), clientFrameHandler); CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(_client, proxyUri, clientFrameHandler);
upgradeRequest.setConfiguration(defaultCustomizer); upgradeRequest.setConfiguration(defaultCustomizer);
CompletableFuture<CoreSession> response = _client.connect(upgradeRequest); CompletableFuture<CoreSession> response = _client.connect(upgradeRequest);
response.get(5, TimeUnit.SECONDS); response.get(5, TimeUnit.SECONDS);
@ -238,9 +233,9 @@ public class WebSocketProxyTest
WebSocketProxy.Client2Proxy proxyClientSide = proxy.client2Proxy; WebSocketProxy.Client2Proxy proxyClientSide = proxy.client2Proxy;
WebSocketProxy.Server2Proxy proxyServerSide = proxy.server2Proxy; WebSocketProxy.Server2Proxy proxyServerSide = proxy.server2Proxy;
try (StacklessLogging stacklessLogging = new StacklessLogging(WebSocketCoreSession.class)) try (StacklessLogging ignored = new StacklessLogging(WebSocketCoreSession.class))
{ {
CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(_client, new URI("ws://localhost:" + PROXY_PORT + "/proxy/"), clientFrameHandler); CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(_client, proxyUri, clientFrameHandler);
upgradeRequest.setConfiguration(defaultCustomizer); upgradeRequest.setConfiguration(defaultCustomizer);
CompletableFuture<CoreSession> response = _client.connect(upgradeRequest); CompletableFuture<CoreSession> response = _client.connect(upgradeRequest);
Exception e = assertThrows(ExecutionException.class, () -> response.get(5, TimeUnit.SECONDS)); Exception e = assertThrows(ExecutionException.class, () -> response.get(5, TimeUnit.SECONDS));
@ -271,7 +266,7 @@ public class WebSocketProxyTest
WebSocketProxy.Server2Proxy proxyServerSide = proxy.server2Proxy; WebSocketProxy.Server2Proxy proxyServerSide = proxy.server2Proxy;
TestAsyncFrameHandler clientFrameHandler = new TestAsyncFrameHandler("CLIENT"); TestAsyncFrameHandler clientFrameHandler = new TestAsyncFrameHandler("CLIENT");
CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(_client, new URI("ws://localhost:" + PROXY_PORT + "/proxy/"), clientFrameHandler); CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(_client, proxyUri, clientFrameHandler);
upgradeRequest.setConfiguration(defaultCustomizer); upgradeRequest.setConfiguration(defaultCustomizer);
CompletableFuture<CoreSession> response = _client.connect(upgradeRequest); CompletableFuture<CoreSession> response = _client.connect(upgradeRequest);
@ -286,11 +281,13 @@ public class WebSocketProxyTest
// Client2Proxy // Client2Proxy
frame = proxyClientSide.receivedFrames.poll(); frame = proxyClientSide.receivedFrames.poll();
assertNotNull(frame);
assertThat(frame.getOpCode(), is(OpCode.TEXT)); assertThat(frame.getOpCode(), is(OpCode.TEXT));
assertThat(frame.getPayloadAsUTF8(), is("hello world")); assertThat(frame.getPayloadAsUTF8(), is("hello world"));
// Server // Server
frame = serverFrameHandler.receivedFrames.poll(); frame = serverFrameHandler.receivedFrames.poll();
assertNotNull(frame);
assertThat(frame.getOpCode(), is(OpCode.TEXT)); assertThat(frame.getOpCode(), is(OpCode.TEXT));
assertThat(frame.getPayloadAsUTF8(), is("hello world")); assertThat(frame.getPayloadAsUTF8(), is("hello world"));
frame = serverFrameHandler.receivedFrames.poll(); frame = serverFrameHandler.receivedFrames.poll();
@ -298,12 +295,14 @@ public class WebSocketProxyTest
// Server2Proxy // Server2Proxy
frame = proxyServerSide.receivedFrames.poll(); frame = proxyServerSide.receivedFrames.poll();
assertNotNull(frame);
closeStatus = CloseStatus.getCloseStatus(frame); closeStatus = CloseStatus.getCloseStatus(frame);
assertThat(closeStatus.getCode(), is(CloseStatus.SERVER_ERROR)); assertThat(closeStatus.getCode(), is(CloseStatus.SERVER_ERROR));
assertThat(closeStatus.getReason(), is("intentionally throwing in server onFrame()")); assertThat(closeStatus.getReason(), is("intentionally throwing in server onFrame()"));
// Client // Client
frame = clientFrameHandler.receivedFrames.poll(); frame = clientFrameHandler.receivedFrames.poll();
assertNotNull(frame);
closeStatus = CloseStatus.getCloseStatus(frame); closeStatus = CloseStatus.getCloseStatus(frame);
assertThat(closeStatus.getCode(), is(CloseStatus.SERVER_ERROR)); assertThat(closeStatus.getCode(), is(CloseStatus.SERVER_ERROR));
assertThat(closeStatus.getReason(), is("intentionally throwing in server onFrame()")); assertThat(closeStatus.getReason(), is("intentionally throwing in server onFrame()"));
@ -335,7 +334,7 @@ public class WebSocketProxyTest
} }
}; };
CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(_client, new URI("ws://localhost:" + PROXY_PORT + "/proxy/"), clientFrameHandler); CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(_client, proxyUri, clientFrameHandler);
upgradeRequest.setConfiguration(defaultCustomizer); upgradeRequest.setConfiguration(defaultCustomizer);
CompletableFuture<CoreSession> response = _client.connect(upgradeRequest); CompletableFuture<CoreSession> response = _client.connect(upgradeRequest);
response.get(5, TimeUnit.SECONDS); response.get(5, TimeUnit.SECONDS);
@ -349,11 +348,13 @@ public class WebSocketProxyTest
// Client2Proxy // Client2Proxy
frame = proxyClientSide.receivedFrames.poll(); frame = proxyClientSide.receivedFrames.poll();
assertNotNull(frame);
assertThat(frame.getOpCode(), is(OpCode.TEXT)); assertThat(frame.getOpCode(), is(OpCode.TEXT));
assertThat(frame.getPayloadAsUTF8(), is("hello world")); assertThat(frame.getPayloadAsUTF8(), is("hello world"));
// Server // Server
frame = serverFrameHandler.receivedFrames.poll(); frame = serverFrameHandler.receivedFrames.poll();
assertNotNull(frame);
assertThat(frame.getOpCode(), is(OpCode.TEXT)); assertThat(frame.getOpCode(), is(OpCode.TEXT));
assertThat(frame.getPayloadAsUTF8(), is("hello world")); assertThat(frame.getPayloadAsUTF8(), is("hello world"));
assertNull(serverFrameHandler.receivedFrames.poll()); assertNull(serverFrameHandler.receivedFrames.poll());