Search in sources :

Example 16 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method serverMessage.

@Test
public void serverMessage() throws IOException {
    webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
    newWebSocket();
    clientListener.assertOpen();
    WebSocket server = serverListener.assertOpen();
    server.send("Hello, WebSockets!");
    clientListener.assertTextMessage("Hello, WebSockets!");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) WebSocket(okhttp3.WebSocket) Test(org.junit.Test)

Example 17 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method wrongUpgradeHeader.

@Test
public void wrongUpgradeHeader() throws IOException {
    webServer.enqueue(new MockResponse().setResponseCode(101).setHeader("Connection", "Upgrade").setHeader("Upgrade", "Pepsi").setHeader("Sec-WebSocket-Accept", "ujmZX4KXZqjwy6vi1aQFH5p4Ygk="));
    newWebSocket();
    clientListener.assertFailure(101, null, ProtocolException.class, "Expected 'Upgrade' header value 'websocket' but was 'Pepsi'");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 18 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method readTimeoutDoesNotApplyAcrossFrames.

@Test
public void readTimeoutDoesNotApplyAcrossFrames() throws Exception {
    webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
    newWebSocket();
    clientListener.assertOpen();
    WebSocket server = serverListener.assertOpen();
    // Sleep longer than the HTTP client's read timeout.
    Thread.sleep(client.readTimeoutMillis() + 500);
    server.send("abc");
    clientListener.assertTextMessage("abc");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) WebSocket(okhttp3.WebSocket) Test(org.junit.Test)

Example 19 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method overflowOutgoingQueue.

@Test
public void overflowOutgoingQueue() throws IOException {
    webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
    WebSocket webSocket = newWebSocket();
    clientListener.assertOpen();
    // Send messages until the client's outgoing buffer overflows!
    ByteString message = ByteString.of(new byte[1024 * 1024]);
    int messageCount = 0;
    while (true) {
        boolean success = webSocket.send(message);
        if (!success)
            break;
        messageCount++;
        long queueSize = webSocket.queueSize();
        assertTrue(queueSize >= 0 && queueSize <= messageCount * message.size());
        // Expect to fail before enqueueing 32 MiB.
        assertTrue(messageCount < 32);
    }
    // Confirm all sent messages were received, followed by a client-initiated close.
    WebSocket server = serverListener.assertOpen();
    for (int i = 0; i < messageCount; i++) {
        serverListener.assertBinaryMessage(message);
    }
    serverListener.assertClosing(1001, "");
    // When the server acknowledges the close the connection shuts down gracefully.
    server.close(1000, null);
    clientListener.assertClosing(1000, "");
    clientListener.assertClosed(1000, "");
    serverListener.assertClosed(1001, "");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ByteString(okio.ByteString) WebSocket(okhttp3.WebSocket) Test(org.junit.Test)

Example 20 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() throws IOException {
    webServer.setDispatcher(new Dispatcher() {

        @Override
        public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
            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");
    assertFalse(webSocket.close(1000, null));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) Dispatcher(okhttp3.mockwebserver.Dispatcher) WebSocket(okhttp3.WebSocket) Test(org.junit.Test)

Aggregations

MockResponse (okhttp3.mockwebserver.MockResponse)23 Test (org.junit.Test)22 WebSocket (okhttp3.WebSocket)20 WebSocketListener (okhttp3.WebSocketListener)11 Response (okhttp3.Response)7 ByteString (okio.ByteString)7 Request (okhttp3.Request)5 IOException (java.io.IOException)4 SecureRandom (java.security.SecureRandom)2 Interceptor (okhttp3.Interceptor)2 OkHttpClient (okhttp3.OkHttpClient)2 RealWebSocket (okhttp3.internal.ws.RealWebSocket)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 FlowableOnSubscribe (io.reactivex.FlowableOnSubscribe)1 OnErrorNotImplementedException (io.reactivex.exceptions.OnErrorNotImplementedException)1 InterruptedIOException (java.io.InterruptedIOException)1 ProtocolException (java.net.ProtocolException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Logger (java.util.logging.Logger)1