Search in sources :

Example 96 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class WebSocketHttpTest method overflowOutgoingQueue.

@Test
public void overflowOutgoingQueue() {
    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]);
    long messageCount = 0;
    while (true) {
        boolean success = webSocket.send(message);
        if (!success)
            break;
        messageCount++;
        long queueSize = webSocket.queueSize();
        assertThat(queueSize).isBetween(0L, messageCount * message.size());
        // Expect to fail before enqueueing 32 MiB.
        assertThat(messageCount).isLessThan(32L);
    }
    // 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(mockwebserver3.MockResponse) ByteString(okio.ByteString) WebSocket(okhttp3.WebSocket) Test(org.junit.jupiter.api.Test)

Example 97 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class InterceptorTest method networkInterceptorsCannotChangeServerAddress.

@Test
public void networkInterceptorsCannotChangeServerAddress() throws Exception {
    server.enqueue(new MockResponse().setResponseCode(500));
    Interceptor interceptor = chain -> {
        Address address = chain.connection().route().address();
        String sameHost = address.url().host();
        int differentPort = address.url().port() + 1;
        return chain.proceed(chain.request().newBuilder().url("http://" + sameHost + ":" + differentPort + "/").build());
    };
    client = client.newBuilder().addNetworkInterceptor(interceptor).build();
    Request request = new Request.Builder().url(server.url("/")).build();
    try {
        client.newCall(request).execute();
        fail();
    } catch (IllegalStateException expected) {
        assertThat(expected.getMessage()).isEqualTo(("network interceptor " + interceptor + " must retain the same host and port"));
    }
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) Okio(okio.Okio) BeforeEach(org.junit.jupiter.api.BeforeEach) Socket(java.net.Socket) Source(okio.Source) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicReference(java.util.concurrent.atomic.AtomicReference) Sink(okio.Sink) MockWebServer(mockwebserver3.MockWebServer) InetAddress(java.net.InetAddress) ServerSocket(java.net.ServerSocket) SocketPolicy(mockwebserver3.SocketPolicy) BufferedSink(okio.BufferedSink) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Locale(java.util.Locale) SocketTimeoutException(java.net.SocketTimeoutException) Duration(java.time.Duration) ForwardingSource(okio.ForwardingSource) Tag(org.junit.jupiter.api.Tag) Buffer(okio.Buffer) SynchronousQueue(java.util.concurrent.SynchronousQueue) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Unit(kotlin.Unit) GzipSink(okio.GzipSink) ForwardingSink(okio.ForwardingSink) MockResponse(mockwebserver3.MockResponse) TestUtil.assertSuppressed(okhttp3.TestUtil.assertSuppressed) RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) InetAddress(java.net.InetAddress) RecordedRequest(mockwebserver3.RecordedRequest) Test(org.junit.jupiter.api.Test)

Example 98 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class InterceptorTest method asyncInterceptors.

private void asyncInterceptors(boolean network) throws Exception {
    server.enqueue(new MockResponse());
    addInterceptor(network, chain -> {
        Response originalResponse = chain.proceed(chain.request());
        return originalResponse.newBuilder().addHeader("OkHttp-Intercepted", "yep").build();
    });
    Request request = new Request.Builder().url(server.url("/")).build();
    client.newCall(request).enqueue(callback);
    callback.await(request.url()).assertCode(200).assertHeader("OkHttp-Intercepted", "yep");
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) RecordedRequest(mockwebserver3.RecordedRequest)

Example 99 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class InterceptorTest method multipleInterceptors.

private void multipleInterceptors(boolean network) throws Exception {
    server.enqueue(new MockResponse());
    addInterceptor(network, chain -> {
        Request originalRequest = chain.request();
        Response originalResponse = chain.proceed(originalRequest.newBuilder().addHeader("Request-Interceptor", // 1. Added first.
        "Android").build());
        return originalResponse.newBuilder().addHeader("Response-Interceptor", // 4. Added last.
        "Donut").build();
    });
    addInterceptor(network, chain -> {
        Request originalRequest = chain.request();
        Response originalResponse = chain.proceed(originalRequest.newBuilder().addHeader("Request-Interceptor", // 2. Added second.
        "Bob").build());
        return originalResponse.newBuilder().addHeader("Response-Interceptor", // 3. Added third.
        "Cupcake").build();
    });
    Request request = new Request.Builder().url(server.url("/")).build();
    Response response = client.newCall(request).execute();
    assertThat(response.headers("Response-Interceptor")).containsExactly("Cupcake", "Donut");
    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest.getHeaders().values("Request-Interceptor")).containsExactly("Android", "Bob");
}
Also used : MockResponse(mockwebserver3.MockResponse) RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) RecordedRequest(mockwebserver3.RecordedRequest)

Example 100 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class InterceptorTest method applicationInterceptorReturnsNull.

@Test
public void applicationInterceptorReturnsNull() throws Exception {
    server.enqueue(new MockResponse());
    Interceptor interceptor = chain -> {
        chain.proceed(chain.request());
        return null;
    };
    client = client.newBuilder().addInterceptor(interceptor).build();
    ExceptionCatchingExecutor executor = new ExceptionCatchingExecutor();
    client = client.newBuilder().dispatcher(new Dispatcher(executor)).build();
    Request request = new Request.Builder().url(server.url("/")).build();
    try {
        client.newCall(request).execute();
        fail();
    } catch (NullPointerException expected) {
        assertThat(expected.getMessage()).isEqualTo(("interceptor " + interceptor + " returned null"));
    }
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) Okio(okio.Okio) BeforeEach(org.junit.jupiter.api.BeforeEach) Socket(java.net.Socket) Source(okio.Source) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicReference(java.util.concurrent.atomic.AtomicReference) Sink(okio.Sink) MockWebServer(mockwebserver3.MockWebServer) InetAddress(java.net.InetAddress) ServerSocket(java.net.ServerSocket) SocketPolicy(mockwebserver3.SocketPolicy) BufferedSink(okio.BufferedSink) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Locale(java.util.Locale) SocketTimeoutException(java.net.SocketTimeoutException) Duration(java.time.Duration) ForwardingSource(okio.ForwardingSource) Tag(org.junit.jupiter.api.Tag) Buffer(okio.Buffer) SynchronousQueue(java.util.concurrent.SynchronousQueue) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Unit(kotlin.Unit) GzipSink(okio.GzipSink) ForwardingSink(okio.ForwardingSink) MockResponse(mockwebserver3.MockResponse) TestUtil.assertSuppressed(okhttp3.TestUtil.assertSuppressed) RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) RecordedRequest(mockwebserver3.RecordedRequest) Test(org.junit.jupiter.api.Test)

Aggregations

MockResponse (mockwebserver3.MockResponse)283 Test (org.junit.jupiter.api.Test)261 RecordedRequest (mockwebserver3.RecordedRequest)79 IOException (java.io.IOException)41 Response (okhttp3.Response)39 BufferedSink (okio.BufferedSink)28 WebSocket (okhttp3.WebSocket)27 AtomicReference (java.util.concurrent.atomic.AtomicReference)22 MockWebServer (mockwebserver3.MockWebServer)22 Request (okhttp3.Request)21 Buffer (okio.Buffer)21 InetAddress (java.net.InetAddress)20 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)17 Assertions.fail (org.junit.jupiter.api.Assertions.fail)17 BeforeEach (org.junit.jupiter.api.BeforeEach)17 Tag (org.junit.jupiter.api.Tag)17 RegisterExtension (org.junit.jupiter.api.extension.RegisterExtension)17 SocketTimeoutException (java.net.SocketTimeoutException)15 Duration (java.time.Duration)15 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)15