org.apache.tomcat.websocket.TesterEchoServer Java Examples

The following examples show how to use org.apache.tomcat.websocket.TesterEchoServer. 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: TestWsServerContainer.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
    super.contextInitialized(sce);

    ServerContainer sc =
            (ServerContainer) sce.getServletContext().getAttribute(
                    Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE);

    ServerEndpointConfig sec = ServerEndpointConfig.Builder.create(
            TesterEchoServer.Basic.class, "/{param}").build();

    try {
        sc.addEndpoint(sec);
    } catch (DeploymentException e) {
        throw new RuntimeException(e);
    }
}
 
Example #2
Source File: TestWsServerContainer.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
    super.contextInitialized(sce);

    ServerContainer sc =
            (ServerContainer) sce.getServletContext().getAttribute(
                    Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE);

    ServerEndpointConfig sec = ServerEndpointConfig.Builder.create(
            TesterEchoServer.Basic.class, "/{param}").build();

    try {
        sc.addEndpoint(sec);
    } catch (DeploymentException e) {
        throw new RuntimeException(e);
    }
}
 
Example #3
Source File: TestWebSocket.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoConnection() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(new ApplicationListener(
            TesterEchoServer.Config.class.getName(), false));

    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    tomcat.start();

    WebSocketClient client= new WebSocketClient(getPort());

    // Send the WebSocket handshake
    client.writer.write("GET " + TesterEchoServer.Config.PATH_BASIC + " HTTP/1.1" + CRLF);
    client.writer.write("Host: foo" + CRLF);
    client.writer.write("Upgrade: websocket" + CRLF);
    client.writer.write("Sec-WebSocket-Version: 13" + CRLF);
    client.writer.write("Sec-WebSocket-Key: TODO" + CRLF);
    client.writer.write(CRLF);
    client.writer.flush();

    // Make sure we got an error response
    String responseLine = client.reader.readLine();
    assertTrue(responseLine.startsWith("HTTP/1.1 400"));

    // Finished with the socket
    client.close();
}
 
Example #4
Source File: TestWebSocket.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoUpgrade() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(new ApplicationListener(
            TesterEchoServer.Config.class.getName(), false));

    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    tomcat.start();

    WebSocketClient client= new WebSocketClient(getPort());

    // Send the WebSocket handshake
    client.writer.write("GET " + TesterEchoServer.Config.PATH_BASIC + " HTTP/1.1" + CRLF);
    client.writer.write("Host: foo" + CRLF);
    client.writer.write("Connection: upgrade" + CRLF);
    client.writer.write("Sec-WebSocket-Version: 13" + CRLF);
    client.writer.write("Sec-WebSocket-Key: TODO" + CRLF);
    client.writer.write(CRLF);
    client.writer.flush();

    // Make sure we got an error response
    // No upgrade means it is not treated an as upgrade request so a 404 is
    // generated when the request reaches the Default Servlet.s
    String responseLine = client.reader.readLine();
    assertTrue(responseLine.startsWith("HTTP/1.1 404"));

    // Finished with the socket
    client.close();
}
 
Example #5
Source File: TestWebSocket.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoConnection() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(new ApplicationListener(
            TesterEchoServer.Config.class.getName(), false));

    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    tomcat.start();

    WebSocketClient client= new WebSocketClient(getPort());

    // Send the WebSocket handshake
    client.writer.write("GET " + TesterEchoServer.Config.PATH_BASIC + " HTTP/1.1" + CRLF);
    client.writer.write("Host: foo" + CRLF);
    client.writer.write("Upgrade: websocket" + CRLF);
    client.writer.write("Sec-WebSocket-Version: 13" + CRLF);
    client.writer.write("Sec-WebSocket-Key: TODO" + CRLF);
    client.writer.write(CRLF);
    client.writer.flush();

    // Make sure we got an error response
    String responseLine = client.reader.readLine();
    assertTrue(responseLine.startsWith("HTTP/1.1 400"));

    // Finished with the socket
    client.close();
}
 
Example #6
Source File: TestWebSocket.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoUpgrade() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(new ApplicationListener(
            TesterEchoServer.Config.class.getName(), false));

    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    tomcat.start();

    WebSocketClient client= new WebSocketClient(getPort());

    // Send the WebSocket handshake
    client.writer.write("GET " + TesterEchoServer.Config.PATH_BASIC + " HTTP/1.1" + CRLF);
    client.writer.write("Host: foo" + CRLF);
    client.writer.write("Connection: upgrade" + CRLF);
    client.writer.write("Sec-WebSocket-Version: 13" + CRLF);
    client.writer.write("Sec-WebSocket-Key: TODO" + CRLF);
    client.writer.write(CRLF);
    client.writer.flush();

    // Make sure we got an error response
    // No upgrade means it is not treated an as upgrade request so a 404 is
    // generated when the request reaches the Default Servlet.s
    String responseLine = client.reader.readLine();
    assertTrue(responseLine.startsWith("HTTP/1.1 404"));

    // Finished with the socket
    client.close();
}
 
Example #7
Source File: TestWsServerContainer.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
protected ServerEndpointConfig getServerEndpointConfig() {
    return ServerEndpointConfig.Builder.create(
            TesterEchoServer.Basic.class, "/{param}").build();
}
 
Example #8
Source File: TestWebSocket.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Test
public void testSimple() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(new ApplicationListener(
            TesterEchoServer.Config.class.getName(), false));

    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    tomcat.start();

    WebSocketClient client = new WebSocketClient(getPort());

    // Send the WebSocket handshake
    client.writer.write("GET " + TesterEchoServer.Config.PATH_BASIC + " HTTP/1.1" + CRLF);
    client.writer.write("Host: foo" + CRLF);
    client.writer.write("Upgrade: websocket" + CRLF);
    client.writer.write("Connection: keep-alive, upgrade" + CRLF);
    client.writer.write("Sec-WebSocket-Version: 13" + CRLF);
    client.writer.write("Sec-WebSocket-Key: TODO" + CRLF);
    client.writer.write(CRLF);
    client.writer.flush();

    // Make sure we got an upgrade response
    String responseLine = client.reader.readLine();
    assertTrue(responseLine.startsWith("HTTP/1.1 101"));

    // Swallow the headers
    String responseHeaderLine = client.reader.readLine();
    while (!responseHeaderLine.equals("")) {
        responseHeaderLine = client.reader.readLine();
    }

    // Now we can do WebSocket
    client.sendMessage("foo", false);
    client.sendMessage("foo", true);

    assertEquals("foofoo", client.readMessage());

    // Finished with the socket
    client.close();
}
 
Example #9
Source File: TestWebSocket.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Test
public void testDetectWrongVersion() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(new ApplicationListener(
            TesterEchoServer.Config.class.getName(), false));

    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    tomcat.start();

    WebSocketClient client= new WebSocketClient(getPort());

    // Send the WebSocket handshake
    client.writer.write("GET " + TesterEchoServer.Config.PATH_BASIC + " HTTP/1.1" + CRLF);
    client.writer.write("Host: foo" + CRLF);
    client.writer.write("Upgrade: websocket" + CRLF);
    client.writer.write("Connection: upgrade" + CRLF);
    client.writer.write("Sec-WebSocket-Version: 8" + CRLF);
    client.writer.write("Sec-WebSocket-Key: TODO" + CRLF);
    client.writer.write(CRLF);
    client.writer.flush();

    // Make sure we got an upgrade response
    String responseLine = client.reader.readLine();
    assertTrue(responseLine.startsWith("HTTP/1.1 426"));

    List<String> headerlines = new ArrayList<String>();

    String responseHeaderLine = client.reader.readLine();
    while (!responseHeaderLine.equals("")) {
        headerlines.add(responseHeaderLine);
        responseHeaderLine = client.reader.readLine();
    }

    assertTrue(headerlines.contains("Sec-WebSocket-Version: 13"));
    // Finished with the socket
    client.close();
}
 
Example #10
Source File: TestWebSocket.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Test
public void testKey() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(new ApplicationListener(
            TesterEchoServer.Config.class.getName(), false));

    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    tomcat.start();

    WebSocketClient client= new WebSocketClient(getPort());

    // Send the WebSocket handshake
    client.writer.write("GET " + TesterEchoServer.Config.PATH_BASIC + " HTTP/1.1" + CRLF);
    client.writer.write("Host: foo" + CRLF);
    client.writer.write("Upgrade: websocket" + CRLF);
    client.writer.write("Connection: upgrade" + CRLF);
    client.writer.write("Sec-WebSocket-Version: 13" + CRLF);
    client.writer.write("Sec-WebSocket-Key: TODO" + CRLF);
    client.writer.write(CRLF);
    client.writer.flush();

    // Make sure we got an upgrade response
    String responseLine = client.reader.readLine();
    assertTrue(responseLine.startsWith("HTTP/1.1 101"));

    String accept = null;
    String responseHeaderLine = client.reader.readLine();
    while (!responseHeaderLine.equals("")) {
        if(responseHeaderLine.startsWith("Sec-WebSocket-Accept: ")) {
            accept = responseHeaderLine.substring(responseHeaderLine.indexOf(':')+2);
            break;
        }
        responseHeaderLine = client.reader.readLine();
    }
    assertTrue(accept != null);
    MessageDigest sha1Helper = MessageDigest.getInstance("SHA1");
    sha1Helper.reset();
    sha1Helper.update("TODO".getBytes(B2CConverter.ISO_8859_1));
    String source = Base64.encode(sha1Helper.digest(WS_ACCEPT));
    assertEquals(source,accept);

    sha1Helper.reset();
    sha1Helper.update("TOD".getBytes(B2CConverter.ISO_8859_1));
    source = Base64.encode(sha1Helper.digest(WS_ACCEPT));
    assertFalse(source.equals(accept));
    // Finished with the socket
    client.close();
}
 
Example #11
Source File: TestWebSocket.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Test
public void testSimple() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(new ApplicationListener(
            TesterEchoServer.Config.class.getName(), false));

    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    tomcat.start();

    WebSocketClient client = new WebSocketClient(getPort());

    // Send the WebSocket handshake
    client.writer.write("GET " + TesterEchoServer.Config.PATH_BASIC + " HTTP/1.1" + CRLF);
    client.writer.write("Host: foo" + CRLF);
    client.writer.write("Upgrade: websocket" + CRLF);
    client.writer.write("Connection: keep-alive, upgrade" + CRLF);
    client.writer.write("Sec-WebSocket-Version: 13" + CRLF);
    client.writer.write("Sec-WebSocket-Key: TODO" + CRLF);
    client.writer.write(CRLF);
    client.writer.flush();

    // Make sure we got an upgrade response
    String responseLine = client.reader.readLine();
    assertTrue(responseLine.startsWith("HTTP/1.1 101"));

    // Swallow the headers
    String responseHeaderLine = client.reader.readLine();
    while (!responseHeaderLine.equals("")) {
        responseHeaderLine = client.reader.readLine();
    }

    // Now we can do WebSocket
    client.sendMessage("foo", false);
    client.sendMessage("foo", true);

    assertEquals("foofoo", client.readMessage());

    // Finished with the socket
    client.close();
}
 
Example #12
Source File: TestWebSocket.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Test
public void testDetectWrongVersion() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(new ApplicationListener(
            TesterEchoServer.Config.class.getName(), false));

    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    tomcat.start();

    WebSocketClient client= new WebSocketClient(getPort());

    // Send the WebSocket handshake
    client.writer.write("GET " + TesterEchoServer.Config.PATH_BASIC + " HTTP/1.1" + CRLF);
    client.writer.write("Host: foo" + CRLF);
    client.writer.write("Upgrade: websocket" + CRLF);
    client.writer.write("Connection: upgrade" + CRLF);
    client.writer.write("Sec-WebSocket-Version: 8" + CRLF);
    client.writer.write("Sec-WebSocket-Key: TODO" + CRLF);
    client.writer.write(CRLF);
    client.writer.flush();

    // Make sure we got an upgrade response
    String responseLine = client.reader.readLine();
    assertTrue(responseLine.startsWith("HTTP/1.1 426"));

    List<String> headerlines = new ArrayList<String>();

    String responseHeaderLine = client.reader.readLine();
    while (!responseHeaderLine.equals("")) {
        headerlines.add(responseHeaderLine);
        responseHeaderLine = client.reader.readLine();
    }

    assertTrue(headerlines.contains("Sec-WebSocket-Version: 13"));
    // Finished with the socket
    client.close();
}
 
Example #13
Source File: TestWebSocket.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Test
public void testKey() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(new ApplicationListener(
            TesterEchoServer.Config.class.getName(), false));

    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    tomcat.start();

    WebSocketClient client= new WebSocketClient(getPort());

    // Send the WebSocket handshake
    client.writer.write("GET " + TesterEchoServer.Config.PATH_BASIC + " HTTP/1.1" + CRLF);
    client.writer.write("Host: foo" + CRLF);
    client.writer.write("Upgrade: websocket" + CRLF);
    client.writer.write("Connection: upgrade" + CRLF);
    client.writer.write("Sec-WebSocket-Version: 13" + CRLF);
    client.writer.write("Sec-WebSocket-Key: TODO" + CRLF);
    client.writer.write(CRLF);
    client.writer.flush();

    // Make sure we got an upgrade response
    String responseLine = client.reader.readLine();
    assertTrue(responseLine.startsWith("HTTP/1.1 101"));

    String accept = null;
    String responseHeaderLine = client.reader.readLine();
    while (!responseHeaderLine.equals("")) {
        if(responseHeaderLine.startsWith("Sec-WebSocket-Accept: ")) {
            accept = responseHeaderLine.substring(responseHeaderLine.indexOf(':')+2);
            break;
        }
        responseHeaderLine = client.reader.readLine();
    }
    assertTrue(accept != null);
    MessageDigest sha1Helper = MessageDigest.getInstance("SHA1");
    sha1Helper.reset();
    sha1Helper.update("TODO".getBytes(B2CConverter.ISO_8859_1));
    String source = Base64.encode(sha1Helper.digest(WS_ACCEPT));
    assertEquals(source,accept);

    sha1Helper.reset();
    sha1Helper.update("TOD".getBytes(B2CConverter.ISO_8859_1));
    source = Base64.encode(sha1Helper.digest(WS_ACCEPT));
    assertFalse(source.equals(accept));
    // Finished with the socket
    client.close();
}