Search in sources :

Example 21 with Request

use of com.tonyodev.fetch2.Request in project zipkin by openzipkin.

the class HttpCallTest method propagatesOnDispatcherThreadWhenFatal.

@Test
public void propagatesOnDispatcherThreadWhenFatal() throws Exception {
    mws.enqueue(new MockResponse());
    http.newCall(request, b -> {
        throw new LinkageError();
    }).submit(callback);
    SimpleTimeLimiter timeLimiter = new SimpleTimeLimiter();
    try {
        timeLimiter.callWithTimeout(callback::get, 100, TimeUnit.MILLISECONDS, true);
        failBecauseExceptionWasNotThrown(UncheckedTimeoutException.class);
    } catch (UncheckedTimeoutException expected) {
    }
}
Also used : Request(okhttp3.Request) SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) OkHttpClient(okhttp3.OkHttpClient) Rule(org.junit.Rule) After(org.junit.After) MockWebServer(okhttp3.mockwebserver.MockWebServer) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException) MockResponse(okhttp3.mockwebserver.MockResponse) CallbackCaptor(zipkin.internal.CallbackCaptor) Assertions.failBecauseExceptionWasNotThrown(org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown) MockResponse(okhttp3.mockwebserver.MockResponse) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException) SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter) Test(org.junit.Test)

Example 22 with Request

use of com.tonyodev.fetch2.Request in project zipkin by openzipkin.

the class HttpCallTest method executionException_conversionException.

@Test
public void executionException_conversionException() throws Exception {
    mws.enqueue(new MockResponse());
    http.newCall(request, b -> {
        throw new IllegalArgumentException("eeek");
    }).submit(callback);
    try {
        callback.get();
        failBecauseExceptionWasNotThrown(IllegalArgumentException.class);
    } catch (IllegalArgumentException expected) {
        assertThat(expected).isInstanceOf(IllegalArgumentException.class);
    }
}
Also used : Request(okhttp3.Request) SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) OkHttpClient(okhttp3.OkHttpClient) Rule(org.junit.Rule) After(org.junit.After) MockWebServer(okhttp3.mockwebserver.MockWebServer) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException) MockResponse(okhttp3.mockwebserver.MockResponse) CallbackCaptor(zipkin.internal.CallbackCaptor) Assertions.failBecauseExceptionWasNotThrown(org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 23 with Request

use of com.tonyodev.fetch2.Request in project zipkin by openzipkin.

the class ElasticsearchHttpStorage method flush.

/** This is a blocking call, only used in tests. */
static void flush(HttpCall.Factory factory, String index) throws IOException {
    Request flushRequest = new Request.Builder().url(factory.baseUrl.newBuilder().addPathSegment(index).addPathSegment("_flush").build()).post(RequestBody.create(APPLICATION_JSON, "")).tag("flush-index").build();
    factory.execute(flushRequest, b -> null);
}
Also used : Request(okhttp3.Request)

Example 24 with Request

use of com.tonyodev.fetch2.Request in project Pokemap by omkarmoghe.

the class NetworkRequestLoggingInterceptor method convertRequestBodyToString.

private String convertRequestBodyToString(final Request request) {
    try {
        final Request copy = request.newBuilder().build();
        final Buffer buffer = new Buffer();
        copy.body().writeTo(buffer);
        return buffer.readUtf8();
    } catch (final IOException e) {
        e.printStackTrace();
        Log.e(TAG, "Failed to convert request body to string via convertRequestBodyToString(). Raised: " + e.getMessage());
        return null;
    }
}
Also used : Buffer(okio.Buffer) Request(okhttp3.Request) IOException(java.io.IOException)

Example 25 with Request

use of com.tonyodev.fetch2.Request in project Pokemap by omkarmoghe.

the class NetworkRequestLoggingInterceptor method intercept.

@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
    final Request request = chain.request();
    // Log request
    Log.d(TAG, MessageFormat.format(REQUEST_SEND_LOG, request.method(), sanitize(request.url()), chain.connection(), request.headers()));
    if (request.method().compareToIgnoreCase("post") == 0)
        Log.d(TAG, MessageFormat.format(REQUEST_BODY_LOG, convertRequestBodyToString(request)));
    final long requestStart = System.currentTimeMillis();
    final Response response = chain.proceed(request);
    final long requestEnd = System.currentTimeMillis();
    final long responseTime = requestEnd - requestStart;
    // Log response
    Log.d(TAG, MessageFormat.format(RESPONSE_RECEIVE_LOG, responseTime, sanitize(response.request().url()), response.headers()));
    final String responseBodyString = response.body().string();
    if (responseBodyString.length() > 0)
        Log.d(TAG, MessageFormat.format(RESPONSE_BODY_LOG, responseBodyString.trim()));
    return response.newBuilder().body(ResponseBody.create(response.body().contentType(), responseBodyString)).build();
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request)

Aggregations

Request (okhttp3.Request)1601 Response (okhttp3.Response)1009 IOException (java.io.IOException)519 Test (org.junit.Test)406 OkHttpClient (okhttp3.OkHttpClient)330 RequestBody (okhttp3.RequestBody)255 Call (okhttp3.Call)239 ResponseBody (okhttp3.ResponseBody)187 HttpUrl (okhttp3.HttpUrl)139 Callback (okhttp3.Callback)109 Map (java.util.Map)85 File (java.io.File)77 InputStream (java.io.InputStream)77 JSONObject (org.json.JSONObject)76 MediaType (okhttp3.MediaType)75 Buffer (okio.Buffer)73 List (java.util.List)71 Headers (okhttp3.Headers)71 FormBody (okhttp3.FormBody)64 HashMap (java.util.HashMap)63