Search in sources :

Example 56 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method throwingOnFailLogs.

@Disabled("AsyncCall currently lets runtime exceptions propagate.")
@Test
public void throwingOnFailLogs() throws Exception {
    webServer.enqueue(new MockResponse().setResponseCode(200).setBody("Body"));
    final RuntimeException e = new RuntimeException("boom");
    clientListener.setNextEventDelegate(new WebSocketListener() {

        @Override
        public void onFailure(WebSocket webSocket, Throwable t, Response response) {
            throw e;
        }
    });
    newWebSocket();
    assertThat(testLogHandler.take()).isEqualTo("INFO: [WS client] onFailure");
}
Also used : Response(okhttp3.Response) MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) WebSocketListener(okhttp3.WebSocketListener) WebSocket(okhttp3.WebSocket) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 57 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method unacknowledgedPingFailsConnection.

/**
 * Configure the websocket to send pings every 500 ms. Artificially prevent the server from
 * responding to pings. The client should give up when attempting to send its 2nd ping, at about
 * 1000 ms.
 */
@Test
public void unacknowledgedPingFailsConnection() {
    TestUtil.assumeNotWindows();
    client = client.newBuilder().pingInterval(Duration.ofMillis(500)).build();
    // Stall in onOpen to prevent pongs from being sent.
    final CountDownLatch latch = new CountDownLatch(1);
    webServer.enqueue(new MockResponse().withWebSocketUpgrade(new WebSocketListener() {

        @Override
        public void onOpen(WebSocket webSocket, Response response) {
            try {
                // The server can't respond to pings!
                latch.await();
            } catch (InterruptedException e) {
                throw new AssertionError(e);
            }
        }
    }));
    long openAtNanos = System.nanoTime();
    newWebSocket();
    clientListener.assertOpen();
    clientListener.assertFailure(SocketTimeoutException.class, "sent ping but didn't receive pong within 500ms (after 0 successful ping/pongs)");
    latch.countDown();
    long elapsedUntilFailure = System.nanoTime() - openAtNanos;
    assertThat(TimeUnit.NANOSECONDS.toMillis(elapsedUntilFailure)).isCloseTo(1000L, offset(250L));
}
Also used : Response(okhttp3.Response) MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) WebSocketListener(okhttp3.WebSocketListener) CountDownLatch(java.util.concurrent.CountDownLatch) WebSocket(okhttp3.WebSocket) Test(org.junit.jupiter.api.Test)

Example 58 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method binaryMessage.

@Test
public void binaryMessage() {
    webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
    WebSocket webSocket = newWebSocket();
    clientListener.assertOpen();
    WebSocket server = serverListener.assertOpen();
    webSocket.send(ByteString.encodeUtf8("Hello!"));
    serverListener.assertBinaryMessage(ByteString.of(new byte[] { 'H', 'e', 'l', 'l', 'o', '!' }));
    closeWebSockets(webSocket, server);
}
Also used : MockResponse(mockwebserver3.MockResponse) WebSocket(okhttp3.WebSocket) Test(org.junit.jupiter.api.Test)

Example 59 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method clientCancelsIfCloseIsNotAcknowledged.

/**
 * https://github.com/square/okhttp/issues/2788
 */
@Test
public void clientCancelsIfCloseIsNotAcknowledged() {
    webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
    RealWebSocket webSocket = newWebSocket();
    clientListener.assertOpen();
    WebSocket server = serverListener.assertOpen();
    // Initiate a close on the client, which will schedule a hard cancel in 500 ms.
    long closeAtNanos = System.nanoTime();
    webSocket.close(1000, "goodbye", 500L);
    serverListener.assertClosing(1000, "goodbye");
    // Confirm that the hard cancel occurred after 500 ms.
    clientListener.assertFailure();
    long elapsedUntilFailure = System.nanoTime() - closeAtNanos;
    assertThat(TimeUnit.NANOSECONDS.toMillis(elapsedUntilFailure)).isCloseTo(500L, offset(250L));
    // Close the server and confirm it saw what we expected.
    server.close(1000, null);
    serverListener.assertClosed(1000, "goodbye");
}
Also used : MockResponse(mockwebserver3.MockResponse) WebSocket(okhttp3.WebSocket) Test(org.junit.jupiter.api.Test)

Example 60 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method nullByteStringThrows.

@Test
public void nullByteStringThrows() {
    TestUtil.assumeNotWindows();
    webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
    WebSocket webSocket = newWebSocket();
    clientListener.assertOpen();
    WebSocket server = serverListener.assertOpen();
    try {
        webSocket.send((ByteString) null);
        fail();
    } catch (NullPointerException expected) {
    }
    closeWebSockets(webSocket, server);
}
Also used : MockResponse(mockwebserver3.MockResponse) WebSocket(okhttp3.WebSocket) Test(org.junit.jupiter.api.Test)

Aggregations

WebSocket (okhttp3.WebSocket)42 MockResponse (mockwebserver3.MockResponse)27 Test (org.junit.jupiter.api.Test)27 WebSocketListener (okhttp3.WebSocketListener)25 ByteString (okio.ByteString)20 Request (okhttp3.Request)16 Response (okhttp3.Response)9 OkHttpClient (okhttp3.OkHttpClient)8 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 Test (org.junit.Test)4 RecordedRequest (mockwebserver3.RecordedRequest)3 Single (io.reactivex.rxjava3.core.Single)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataInputStream (java.io.DataInputStream)2 InputStream (java.io.InputStream)2 StandardCharsets (java.nio.charset.StandardCharsets)2