Search in sources :

Example 36 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method readTimeoutAppliesWithinFrames.

/**
 * There's no read timeout when reading the first byte of a new frame. But as soon as we start
 * reading a frame we enable the read timeout. In this test we have the server returning the first
 * byte of a frame but no more frames.
 */
@Test
public void readTimeoutAppliesWithinFrames() {
    webServer.setDispatcher(new Dispatcher() {

        @Override
        public MockResponse dispatch(RecordedRequest request) {
            return upgradeResponse(request).setBody(// Truncated frame.
            new Buffer().write(ByteString.decodeHex("81"))).removeHeader("Content-Length").setSocketPolicy(SocketPolicy.KEEP_OPEN);
        }
    });
    WebSocket webSocket = newWebSocket();
    clientListener.assertOpen();
    clientListener.assertFailure(SocketTimeoutException.class, "timeout", "Read timed out");
    assertThat(webSocket.close(1000, null)).isFalse();
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) Buffer(okio.Buffer) MockResponse(mockwebserver3.MockResponse) Dispatcher(mockwebserver3.Dispatcher) WebSocket(okhttp3.WebSocket) Test(org.junit.jupiter.api.Test)

Example 37 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method textMessage.

@Test
public void textMessage() {
    webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
    WebSocket webSocket = newWebSocket();
    clientListener.assertOpen();
    WebSocket server = serverListener.assertOpen();
    webSocket.send("Hello, WebSockets!");
    serverListener.assertTextMessage("Hello, WebSockets!");
    closeWebSockets(webSocket, server);
}
Also used : MockResponse(mockwebserver3.MockResponse) WebSocket(okhttp3.WebSocket) Test(org.junit.jupiter.api.Test)

Example 38 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method closeWithoutSuccessfulConnect.

/**
 * https://github.com/square/okhttp/issues/5705
 */
@Test
public void closeWithoutSuccessfulConnect() {
    Request request = new Request.Builder().url(webServer.url("/")).build();
    WebSocket webSocket = client.newWebSocket(request, clientListener);
    webSocket.send("hello");
    webSocket.close(1000, null);
}
Also used : Request(okhttp3.Request) RecordedRequest(mockwebserver3.RecordedRequest) WebSocket(okhttp3.WebSocket) Test(org.junit.jupiter.api.Test)

Example 39 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method closeReasonTooLong.

@Test
public void closeReasonTooLong() {
    webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
    WebSocket webSocket = newWebSocket();
    WebSocket server = serverListener.assertOpen();
    clientListener.assertOpen();
    String reason = repeat('X', 124);
    try {
        webSocket.close(1000, reason);
        fail();
    } catch (IllegalArgumentException expected) {
        assertThat(expected.getMessage()).isEqualTo(("reason.size() > 123: " + reason));
    }
    webSocket.close(1000, null);
    serverListener.assertClosing(1000, "");
    server.close(1000, null);
    clientListener.assertClosing(1000, "");
    clientListener.assertClosed(1000, "");
    serverListener.assertClosed(1000, "");
}
Also used : MockResponse(mockwebserver3.MockResponse) ByteString(okio.ByteString) WebSocket(okhttp3.WebSocket) Test(org.junit.jupiter.api.Test)

Example 40 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class OkHttpClient method newWebSocket.

/**
   * Uses {@code request} to connect a new web socket.
   */
@Override
public WebSocket newWebSocket(Request request, WebSocketListener listener) {
    RealWebSocket webSocket = new RealWebSocket(request, listener, new SecureRandom());
    webSocket.connect(this);
    return webSocket;
}
Also used : RealWebSocket(okhttp3.internal.ws.RealWebSocket) SecureRandom(java.security.SecureRandom)

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