Search in sources :

Example 11 with Interceptor

use of okhttp3.Interceptor in project okhttp by square.

the class InterceptorTest method networkInterceptorReturnsConnectionOnEmptyBody.

@Test
public void networkInterceptorReturnsConnectionOnEmptyBody() throws Exception {
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AT_END).addHeader("Connection", "Close"));
    Interceptor interceptor = new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Response response = chain.proceed(chain.request());
            assertNotNull(chain.connection());
            return response;
        }
    };
    client = client.newBuilder().addNetworkInterceptor(interceptor).build();
    Request request = new Request.Builder().url(server.url("/")).build();
    Response response = client.newCall(request).execute();
    response.body().close();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 12 with Interceptor

use of okhttp3.Interceptor in project okhttp by square.

the class InterceptorTest method rewriteRequestToServer.

private void rewriteRequestToServer(boolean network) throws Exception {
    server.enqueue(new MockResponse());
    addInterceptor(network, new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request originalRequest = chain.request();
            return chain.proceed(originalRequest.newBuilder().method("POST", uppercase(originalRequest.body())).addHeader("OkHttp-Intercepted", "yep").build());
        }
    });
    Request request = new Request.Builder().url(server.url("/")).addHeader("Original-Header", "foo").method("PUT", RequestBody.create(MediaType.parse("text/plain"), "abc")).build();
    client.newCall(request).execute();
    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals("ABC", recordedRequest.getBody().readUtf8());
    assertEquals("foo", recordedRequest.getHeader("Original-Header"));
    assertEquals("yep", recordedRequest.getHeader("OkHttp-Intercepted"));
    assertEquals("POST", recordedRequest.getMethod());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) IOException(java.io.IOException)

Example 13 with Interceptor

use of okhttp3.Interceptor in project okhttp by square.

the class InterceptorTest method asyncInterceptors.

private void asyncInterceptors(boolean network) throws Exception {
    server.enqueue(new MockResponse());
    addInterceptor(network, new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            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(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) IOException(java.io.IOException)

Example 14 with Interceptor

use of okhttp3.Interceptor in project okhttp by square.

the class InterceptorTest method networkInterceptorsCannotShortCircuitResponses.

@Test
public void networkInterceptorsCannotShortCircuitResponses() throws Exception {
    server.enqueue(new MockResponse().setResponseCode(500));
    Interceptor interceptor = new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            return new Response.Builder().request(chain.request()).protocol(Protocol.HTTP_1_1).code(200).message("Intercepted!").body(ResponseBody.create(MediaType.parse("text/plain; charset=utf-8"), "abc")).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) {
        assertEquals("network interceptor " + interceptor + " must call proceed() exactly once", expected.getMessage());
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 15 with Interceptor

use of okhttp3.Interceptor in project okhttp by square.

the class InterceptorTest method multipleInterceptors.

private void multipleInterceptors(boolean network) throws Exception {
    server.enqueue(new MockResponse());
    addInterceptor(network, new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            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, new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            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();
    assertEquals(Arrays.asList("Cupcake", "Donut"), response.headers("Response-Interceptor"));
    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals(Arrays.asList("Android", "Bob"), recordedRequest.getHeaders().values("Request-Interceptor"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)35 Interceptor (okhttp3.Interceptor)33 MockResponse (okhttp3.mockwebserver.MockResponse)29 Request (okhttp3.Request)26 Test (org.junit.Test)25 Response (okhttp3.Response)24 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)24 OkHttpClient (okhttp3.OkHttpClient)20 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)10 File (java.io.File)8 Provides (dagger.Provides)6 Singleton (javax.inject.Singleton)6 Cache (okhttp3.Cache)4 Retrofit (retrofit2.Retrofit)4 InterruptedIOException (java.io.InterruptedIOException)3 Call (okhttp3.Call)3 HttpUrl (okhttp3.HttpUrl)3 NonNull (android.support.annotation.NonNull)2 ANResponse (com.androidnetworking.common.ANResponse)2 ResponseProgressBody (com.androidnetworking.internal.ResponseProgressBody)2