Search in sources :

Example 46 with CloseInfo

use of org.eclipse.jetty.websocket.common.CloseInfo in project jetty.project by eclipse.

the class TestABCase4 method testCase4_1_2.

/**
     * Send opcode 4 (reserved), with payload
     * @throws Exception on test failure
     */
@Test
public void testCase4_1_2() throws Exception {
    byte[] payload = StringUtil.getUtf8Bytes("reserved payload");
    ByteBuffer buf = ByteBuffer.wrap(payload);
    List<WebSocketFrame> send = new ArrayList<>();
    // intentionally bad
    send.add(new BadFrame((byte) 4).setPayload(buf));
    List<WebSocketFrame> expect = new ArrayList<>();
    expect.add(new CloseInfo(StatusCode.PROTOCOL).asFrame());
    try (Fuzzer fuzzer = new Fuzzer(this);
        StacklessLogging logging = new StacklessLogging(Parser.class)) {
        fuzzer.connect();
        fuzzer.setSendMode(Fuzzer.SendMode.BULK);
        fuzzer.send(send);
        fuzzer.expect(expect);
    }
}
Also used : Fuzzer(org.eclipse.jetty.websocket.common.test.Fuzzer) ArrayList(java.util.ArrayList) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) ByteBuffer(java.nio.ByteBuffer) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) Test(org.junit.Test)

Example 47 with CloseInfo

use of org.eclipse.jetty.websocket.common.CloseInfo in project jetty.project by eclipse.

the class TestABCase4 method testCase4_1_5.

/**
     * Send small text, then frame with opcode 7 (reserved) w/payload, then ping
     * @throws Exception on test failure
     */
@Test
public void testCase4_1_5() throws Exception {
    ByteBuffer buf = ByteBuffer.wrap(StringUtil.getUtf8Bytes("bad"));
    List<WebSocketFrame> send = new ArrayList<>();
    send.add(new TextFrame().setPayload("hello"));
    // intentionally bad
    send.add(new BadFrame((byte) 7).setPayload(buf));
    send.add(new PingFrame());
    List<WebSocketFrame> expect = new ArrayList<>();
    // echo
    expect.add(new TextFrame().setPayload("hello"));
    expect.add(new CloseInfo(StatusCode.PROTOCOL).asFrame());
    try (Fuzzer fuzzer = new Fuzzer(this);
        StacklessLogging logging = new StacklessLogging(Parser.class)) {
        fuzzer.connect();
        fuzzer.setSendMode(Fuzzer.SendMode.BULK);
        fuzzer.send(send);
        fuzzer.expect(expect);
    }
}
Also used : Fuzzer(org.eclipse.jetty.websocket.common.test.Fuzzer) PingFrame(org.eclipse.jetty.websocket.common.frames.PingFrame) ArrayList(java.util.ArrayList) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) ByteBuffer(java.nio.ByteBuffer) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) Test(org.junit.Test)

Example 48 with CloseInfo

use of org.eclipse.jetty.websocket.common.CloseInfo in project jetty.project by eclipse.

the class OnCloseTest method testOnCloseCall.

@Test
public void testOnCloseCall() throws Exception {
    // Scan annotations
    AnnotatedClientEndpointMetadata metadata = new AnnotatedClientEndpointMetadata(container, testcase.closeClass);
    AnnotatedEndpointScanner<ClientEndpoint, ClientEndpointConfig> scanner = new AnnotatedEndpointScanner<>(metadata);
    scanner.scan();
    // Build up EventDriver
    WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();
    ClientEndpointConfig config = metadata.getConfig();
    TrackingSocket endpoint = (TrackingSocket) testcase.closeClass.newInstance();
    EndpointInstance ei = new EndpointInstance(endpoint, config, metadata);
    JsrEvents<ClientEndpoint, ClientEndpointConfig> jsrevents = new JsrEvents<>(metadata);
    EventDriver driver = new JsrAnnotatedEventDriver(policy, ei, jsrevents);
    // Execute onClose call
    driver.onClose(new CloseInfo(StatusCode.NORMAL, "normal"));
    // Test captured event
    EventQueue<String> events = endpoint.eventQueue;
    Assert.assertThat("Number of Events Captured", events.size(), is(1));
    String closeEvent = events.poll();
    Assert.assertThat("Close Event", closeEvent, is(testcase.expectedCloseEvent));
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) JsrEvents(org.eclipse.jetty.websocket.jsr356.annotations.JsrEvents) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) EventDriver(org.eclipse.jetty.websocket.common.events.EventDriver) AnnotatedEndpointScanner(org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointScanner) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ClientEndpoint(javax.websocket.ClientEndpoint) AnnotatedClientEndpointMetadata(org.eclipse.jetty.websocket.jsr356.client.AnnotatedClientEndpointMetadata) Test(org.junit.Test)

Example 49 with CloseInfo

use of org.eclipse.jetty.websocket.common.CloseInfo in project jetty.project by eclipse.

the class WebSocketCloseTest method fastClose.

@SuppressWarnings("Duplicates")
private void fastClose() throws Exception {
    try (IBlockheadClient client = new BlockheadClient(server.getServerUri())) {
        client.setProtocols("fastclose");
        client.setTimeout(1, TimeUnit.SECONDS);
        try (StacklessLogging scope = new StacklessLogging(WebSocketSession.class)) {
            client.connect();
            client.sendStandardRequest();
            client.expectUpgradeResponse();
            client.readFrames(1, 1, TimeUnit.SECONDS);
            CloseInfo close = new CloseInfo(StatusCode.NORMAL, "Normal");
            assertThat("Close Status Code", close.getStatusCode(), is(StatusCode.NORMAL));
            // Notify server of close handshake
            // respond with close
            client.write(close.asFrame());
            // ensure server socket got close event
            assertThat("Fast Close Latch", closeSocket.closeLatch.await(1, TimeUnit.SECONDS), is(true));
            assertThat("Fast Close.statusCode", closeSocket.closeStatusCode, is(StatusCode.NORMAL));
        }
    }
}
Also used : IBlockheadClient(org.eclipse.jetty.websocket.common.test.IBlockheadClient) BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) IBlockheadClient(org.eclipse.jetty.websocket.common.test.IBlockheadClient) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo)

Example 50 with CloseInfo

use of org.eclipse.jetty.websocket.common.CloseInfo in project jetty.project by eclipse.

the class WebSocketCloseTest method fastFail.

private void fastFail() throws Exception {
    try (IBlockheadClient client = new BlockheadClient(server.getServerUri())) {
        client.setProtocols("fastfail");
        client.setTimeout(1, TimeUnit.SECONDS);
        try (StacklessLogging scope = new StacklessLogging(WebSocketSession.class)) {
            client.connect();
            client.sendStandardRequest();
            client.expectUpgradeResponse();
            client.readFrames(1, 1, TimeUnit.SECONDS);
            CloseInfo close = new CloseInfo(StatusCode.NORMAL, "Normal");
            // respond with close
            client.write(close.asFrame());
            // ensure server socket got close event
            assertThat("Fast Fail Latch", closeSocket.closeLatch.await(1, TimeUnit.SECONDS), is(true));
            assertThat("Fast Fail.statusCode", closeSocket.closeStatusCode, is(StatusCode.SERVER_ERROR));
            assertThat("Fast Fail.errors", closeSocket.errors.size(), is(1));
        }
    }
}
Also used : IBlockheadClient(org.eclipse.jetty.websocket.common.test.IBlockheadClient) BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) IBlockheadClient(org.eclipse.jetty.websocket.common.test.IBlockheadClient) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo)

Aggregations

CloseInfo (org.eclipse.jetty.websocket.common.CloseInfo)151 Test (org.junit.Test)129 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)118 ArrayList (java.util.ArrayList)104 Fuzzer (org.eclipse.jetty.websocket.common.test.Fuzzer)104 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)68 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)55 ByteBuffer (java.nio.ByteBuffer)48 PingFrame (org.eclipse.jetty.websocket.common.frames.PingFrame)30 ContinuationFrame (org.eclipse.jetty.websocket.common.frames.ContinuationFrame)27 PongFrame (org.eclipse.jetty.websocket.common.frames.PongFrame)19 BinaryFrame (org.eclipse.jetty.websocket.common.frames.BinaryFrame)17 BlockheadClient (org.eclipse.jetty.websocket.common.test.BlockheadClient)14 IBlockheadClient (org.eclipse.jetty.websocket.common.test.IBlockheadClient)10 CloseableLocalWebSocketSession (org.eclipse.jetty.websocket.common.io.CloseableLocalWebSocketSession)8 LocalWebSocketSession (org.eclipse.jetty.websocket.common.io.LocalWebSocketSession)8 Stress (org.eclipse.jetty.toolchain.test.annotation.Stress)6 Slow (org.eclipse.jetty.toolchain.test.annotation.Slow)5 CloseFrame (org.eclipse.jetty.websocket.common.frames.CloseFrame)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4