Skip to content

Commit

Permalink
add extra test for server connection close before ws handshake
Browse files Browse the repository at this point in the history
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Nov 22, 2019
1 parent f14ab3f commit dc4ada1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.InterruptedIOException;
import java.net.ConnectException;
import java.net.URI;
import java.nio.channels.ClosedChannelException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
Expand All @@ -45,6 +46,7 @@
import org.eclipse.jetty.server.HttpChannel;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.SecureRequestCustomizer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
Expand All @@ -61,6 +63,7 @@
import org.eclipse.jetty.websocket.server.JettyWebSocketServlet;
import org.eclipse.jetty.websocket.server.JettyWebSocketServletFactory;
import org.eclipse.jetty.websocket.server.config.JettyWebSocketServletContainerInitializer;
import org.eclipse.jetty.websocket.servlet.internal.UpgradeHttpServletRequest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -234,7 +237,8 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
assertTrue(wsEndPoint.closeLatch.await(5, TimeUnit.SECONDS));
}

@Test void testWebSocketConnectPortDoesNotExist() throws Exception
@Test
public void testWebSocketConnectPortDoesNotExist() throws Exception
{
startServer();
startClient(clientConnector -> new ClientConnectionFactoryOverHTTP2.H2(new HTTP2Client(clientConnector)));
Expand All @@ -250,7 +254,8 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
assertThat(cause.getMessage(), containsStringIgnoringCase("Connection refused"));
}

@Test void testWebSocketNotFound() throws Exception
@Test
public void testWebSocketNotFound() throws Exception
{
startServer();
startClient(clientConnector -> new ClientConnectionFactoryOverHTTP2.H2(new HTTP2Client(clientConnector)));
Expand All @@ -266,7 +271,8 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
assertThat(cause.getMessage(), containsStringIgnoringCase("Unexpected HTTP Response Status Code: 501"));
}

@Test void testNotNegotiated() throws Exception
@Test
public void testNotNegotiated() throws Exception
{
startServer();
startClient(clientConnector -> new ClientConnectionFactoryOverHTTP2.H2(new HTTP2Client(clientConnector)));
Expand All @@ -282,7 +288,8 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
assertThat(cause.getMessage(), containsStringIgnoringCase("Unexpected HTTP Response Status Code: 503"));
}

@Test void testThrowFromCreator() throws Exception
@Test
public void testThrowFromCreator() throws Exception
{
startServer();
startClient(clientConnector -> new ClientConnectionFactoryOverHTTP2.H2(new HTTP2Client(clientConnector)));
Expand All @@ -302,6 +309,22 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
assertThat(cause.getMessage(), containsStringIgnoringCase("Unexpected HTTP Response Status Code: 500"));
}

@Test
public void testServerConnectionClose() throws Exception
{
startServer();
startClient(clientConnector -> new ClientConnectionFactoryOverHTTP2.H2(new HTTP2Client(clientConnector)));

EventSocket wsEndPoint = new EventSocket();
URI uri = URI.create("ws://localhost:" + connector.getLocalPort() + "/ws/connectionClose");

ExecutionException failure = Assertions.assertThrows(ExecutionException.class, () ->
wsClient.connect(wsEndPoint, uri).get(5, TimeUnit.SECONDS));

Throwable cause = failure.getCause();
assertThat(cause, instanceOf(ClosedChannelException.class));
}

private static class TestJettyWebSocketServlet extends JettyWebSocketServlet
{
@Override
Expand All @@ -313,6 +336,13 @@ protected void configure(JettyWebSocketServletFactory factory)
{
throw new RuntimeException("throwing from creator");
});
factory.addMapping("/ws/connectionClose", (request, response) ->
{
UpgradeHttpServletRequest servletRequest = (UpgradeHttpServletRequest)request.getHttpServletRequest();
Request baseRequest = servletRequest.getBaseRequest();
baseRequest.getHttpChannel().getEndPoint().close();
return new EchoSocket();
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import javax.servlet.AsyncContext;
import javax.servlet.DispatcherType;
Expand All @@ -55,6 +56,7 @@ public class UpgradeHttpServletRequest implements HttpServletRequest
{
private static final String UNSUPPORTED_WITH_WEBSOCKET_UPGRADE = "Feature unsupported with a Upgraded to WebSocket HttpServletRequest";

private final Request baseRequest;
private final ServletContext context;
private final DispatcherType dispatcher;
private final String method;
Expand Down Expand Up @@ -110,8 +112,9 @@ public UpgradeHttpServletRequest(HttpServletRequest httpRequest)

remoteUser = httpRequest.getRemoteUser();
principal = httpRequest.getUserPrincipal();
authentication = Request.getBaseRequest(httpRequest).getAuthentication();
scope = Request.getBaseRequest(httpRequest).getUserIdentityScope();
baseRequest = Objects.requireNonNull(Request.getBaseRequest(httpRequest));
authentication = baseRequest.getAuthentication();
scope = baseRequest.getUserIdentityScope();

Enumeration<String> headerNames = httpRequest.getHeaderNames();
while (headerNames.hasMoreElements())
Expand Down Expand Up @@ -278,6 +281,11 @@ public HttpSession getSession()
return session;
}

public Request getBaseRequest()
{
return baseRequest;
}

@Override
public String getRequestedSessionId()
{
Expand Down

0 comments on commit dc4ada1

Please sign in to comment.