Search in sources :

Example 11 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method wrongMagicHeader.

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

Example 12 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method throwingOnOpenFailsImmediately.

@Test
public void throwingOnOpenFailsImmediately() {
    webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
    final RuntimeException e = new RuntimeException();
    clientListener.setNextEventDelegate(new WebSocketListener() {

        @Override
        public void onOpen(WebSocket webSocket, Response response) {
            throw e;
        }
    });
    newWebSocket();
    serverListener.assertOpen();
    serverListener.assertExhausted();
    clientListener.assertFailure(e);
}
Also used : Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) WebSocketListener(okhttp3.WebSocketListener) WebSocket(okhttp3.WebSocket) Test(org.junit.Test)

Example 13 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method webSocketAndApplicationInterceptors.

@Test
public void webSocketAndApplicationInterceptors() throws IOException {
    final AtomicInteger interceptedCount = new AtomicInteger();
    client = client.newBuilder().addInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            assertNull(chain.request().body());
            Response response = chain.proceed(chain.request());
            assertEquals("Upgrade", response.header("Connection"));
            assertTrue("", response.body().source().exhausted());
            interceptedCount.incrementAndGet();
            return response;
        }
    }).build();
    webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
    WebSocket webSocket = newWebSocket();
    clientListener.assertOpen();
    assertEquals(1, interceptedCount.get());
    webSocket.close(1000, null);
    WebSocket server = serverListener.assertOpen();
    server.close(1000, null);
}
Also used : Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IOException(java.io.IOException) Interceptor(okhttp3.Interceptor) WebSocket(okhttp3.WebSocket) Test(org.junit.Test)

Example 14 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method nullStringThrows.

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

Example 15 with WebSocket

use of okhttp3.WebSocket in project okhttp by square.

the class WebSocketHttpTest method throwingOnClosingClosesImmediatelyAndFails.

@Test
public void throwingOnClosingClosesImmediatelyAndFails() 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 onClosing(WebSocket webSocket, int code, String reason) {
            throw e;
        }
    });
    server.close(1000, "bye");
    clientListener.assertFailure(e);
    serverListener.assertExhausted();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) WebSocketListener(okhttp3.WebSocketListener) ByteString(okio.ByteString) 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