Search in sources :

Example 31 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method throwingOnMessageClosesImmediatelyAndFails.

@Test
public void throwingOnMessageClosesImmediatelyAndFails() throws IOException {
    webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
    newWebSocket();
    clientListener.assertOpen();
    WebSocket server = serverListener.assertOpen();
    final RuntimeException e = new RuntimeException();
    clientListener.setNextEventDelegate(new WebSocketListener() {

        @Override
        public void onMessage(WebSocket webSocket, String text) {
            throw e;
        }
    });
    server.send("Hello, WebSockets!");
    clientListener.assertFailure(e);
    serverListener.assertExhausted();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) WebSocketListener(okhttp3.WebSocketListener) ByteString(okio.ByteString) WebSocket(okhttp3.WebSocket) Test(org.junit.Test)

Example 32 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method closeReasonMaximumLength.

@Test
public void closeReasonMaximumLength() throws IOException {
    webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
    String clientReason = repeat('C', 123);
    String serverReason = repeat('S', 123);
    WebSocket webSocket = newWebSocket();
    WebSocket server = serverListener.assertOpen();
    clientListener.assertOpen();
    webSocket.close(1000, clientReason);
    serverListener.assertClosing(1000, clientReason);
    server.close(1000, serverReason);
    clientListener.assertClosing(1000, serverReason);
    clientListener.assertClosed(1000, serverReason);
    serverListener.assertClosed(1000, clientReason);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ByteString(okio.ByteString) WebSocket(okhttp3.WebSocket) Test(org.junit.Test)

Example 33 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method nullByteStringThrows.

@Test
public void nullByteStringThrows() throws IOException {
    webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
    WebSocket webSocket = newWebSocket();
    clientListener.assertOpen();
    try {
        webSocket.send((ByteString) null);
        fail();
    } catch (NullPointerException e) {
        assertEquals("bytes == null", e.getMessage());
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) WebSocket(okhttp3.WebSocket) Test(org.junit.Test)

Example 34 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method wrongConnectionHeader.

@Test
public void wrongConnectionHeader() throws IOException {
    webServer.enqueue(new MockResponse().setResponseCode(101).setHeader("Upgrade", "websocket").setHeader("Connection", "Downgrade").setHeader("Sec-WebSocket-Accept", "ujmZX4KXZqjwy6vi1aQFH5p4Ygk="));
    newWebSocket();
    clientListener.assertFailure(101, null, ProtocolException.class, "Expected 'Connection' header value 'Upgrade' but was 'Downgrade'");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) 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