Search in sources :

Example 51 with Headers

use of com.amazonaws.services.s3.Headers in project MVPFrames by RockyQu.

the class LoggingInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    Level level = this.level;
    Request request = chain.request();
    if (level == Level.NONE) {
        return chain.proceed(request);
    }
    RequestBody requestBody = request.body();
    boolean hasRequestBody = requestBody != null;
    // 请求地址
    Connection connection = chain.connection();
    Protocol protocol = connection != null ? connection.protocol() : Protocol.HTTP_1_1;
    String requestStartMessage = "--> " + request.method() + ' ' + request.url() + ' ' + protocol;
    if (hasRequestBody) {
        requestStartMessage += " (" + requestBody.contentLength() + "-byte body)";
    }
    log(requestStartMessage);
    // Content-Type
    if (hasRequestBody) {
        if (requestBody.contentType() != null) {
            log("Content-Type: " + requestBody.contentType());
        }
        if (requestBody.contentLength() != -1) {
            log("Content-Length: " + requestBody.contentLength());
        }
    }
    // 拼装请求参数
    Headers headers = request.headers();
    for (int i = 0, count = headers.size(); i < count; i++) {
        String name = headers.name(i);
        if (!"Content-Type".equalsIgnoreCase(name) && !"Content-Length".equalsIgnoreCase(name)) {
            log(name + ": " + headers.value(i));
        }
    }
    // Request结束
    if (!hasRequestBody) {
        log("--> END " + request.method());
    } else if (bodyEncoded(request.headers())) {
        log("--> END " + request.method() + " (encoded body omitted)");
    } else {
        Buffer buffer = new Buffer();
        requestBody.writeTo(buffer);
        Charset charset = UTF8;
        MediaType contentType = requestBody.contentType();
        if (contentType != null) {
            charset = contentType.charset(UTF8);
        }
        if (isPlaintext(buffer)) {
            log(buffer.readString(charset));
            log("--> END " + request.method() + " (" + requestBody.contentLength() + "-byte body)");
        } else {
            log("--> END " + request.method() + " (binary " + requestBody.contentLength() + "-byte body omitted)");
        }
    }
    // Response开始
    long startNs = System.nanoTime();
    Response response;
    try {
        response = chain.proceed(request);
    } catch (Exception e) {
        log("<-- HTTP FAILED: " + e);
        throw e;
    }
    long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
    ResponseBody responseBody = response.body();
    long contentLength = responseBody.contentLength();
    String bodySize = contentLength != -1 ? contentLength + "-byte" : "unknown-length";
    log("<-- " + response.code() + ' ' + response.message() + ' ' + response.request().url() + " (" + tookMs + "ms" + (", " + bodySize + " body") + ')');
    headers = response.headers();
    for (int i = 0, count = headers.size(); i < count; i++) {
        log(headers.name(i) + ": " + headers.value(i));
    }
    if (!HttpHeaders.hasBody(response)) {
        log("<-- END HTTP");
    } else if (bodyEncoded(response.headers())) {
        log("<-- END HTTP (encoded body omitted)");
    } else {
        BufferedSource source = responseBody.source();
        // Buffer the entire body.
        source.request(Long.MAX_VALUE);
        Buffer buffer = source.buffer();
        Charset charset = UTF8;
        MediaType contentType = responseBody.contentType();
        if (contentType != null) {
            try {
                charset = contentType.charset(UTF8);
            } catch (UnsupportedCharsetException e) {
                log("Couldn't decode the response body; charset is likely malformed.");
                log("<-- END HTTP");
                return response;
            }
        }
        if (!isPlaintext(buffer)) {
            log("<-- END HTTP (binary " + buffer.size() + "-byte body omitted)");
            return response;
        }
        if (contentLength != 0) {
            log(buffer.clone().readString(charset));
        }
        log("<-- END HTTP (" + buffer.size() + "-byte body)");
    }
    return response;
}
Also used : Buffer(okio.Buffer) HttpHeaders(okhttp3.internal.http.HttpHeaders) Headers(okhttp3.Headers) Request(okhttp3.Request) Connection(okhttp3.Connection) Charset(java.nio.charset.Charset) IOException(java.io.IOException) EOFException(java.io.EOFException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) MediaType(okhttp3.MediaType) Protocol(okhttp3.Protocol) RequestBody(okhttp3.RequestBody) BufferedSource(okio.BufferedSource)

Example 52 with Headers

use of com.amazonaws.services.s3.Headers in project ProxerLibJava by proxer.

the class HeaderInterceptorTest method testCorrectHeadersForStreamServer.

@Test
public void testCorrectHeadersForStreamServer() throws IOException, InterruptedException {
    server.enqueue(new MockResponse());
    api.client().newCall(new Request.Builder().url("https://stream.proxer.me/test").build()).execute();
    final Headers headers = server.takeRequest().getHeaders();
    assertThat(headers.get("proxer-api-key")).isNull();
    assertThat(headers.get("User-Agent")).startsWith("ProxerLibJava/");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Headers(okhttp3.Headers) ProxerTest(me.proxer.library.ProxerTest) Test(org.junit.Test)

Example 53 with Headers

use of com.amazonaws.services.s3.Headers in project ProxerLibJava by proxer.

the class HeaderInterceptorTest method testCorrectHeadersForCdn.

@Test
public void testCorrectHeadersForCdn() throws IOException, InterruptedException {
    server.enqueue(new MockResponse());
    api.client().newCall(new Request.Builder().url(ProxerUrls.cdnBase().newBuilder().addPathSegment("test").build()).build()).execute();
    final Headers headers = server.takeRequest().getHeaders();
    assertThat(headers.get("proxer-api-key")).isNull();
    assertThat(headers.get("User-Agent")).startsWith("ProxerLibJava/");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Headers(okhttp3.Headers) ProxerTest(me.proxer.library.ProxerTest) Test(org.junit.Test)

Example 54 with Headers

use of com.amazonaws.services.s3.Headers in project dhis2-android-sdk by dhis2.

the class TrackedEntityCallUnitShould method invoke_insert_if_request_is_successful.

@Test
@SuppressWarnings("unchecked")
public void invoke_insert_if_request_is_successful() throws Exception {
    Headers headers = new Headers.Builder().add("Date", lastUpdated.toString()).build();
    when(payload.items()).thenReturn(Collections.singletonList(trackedEntity));
    Response<Payload<TrackedEntity>> response = Response.success(payload, headers);
    when(retrofitCall.execute()).thenReturn(response);
    call.call();
    verify(database, times(1)).beginNewTransaction();
    verify(transaction, times(1)).setSuccessful();
    verify(transaction, times(1)).end();
    verify(trackedEntityStore, times(1)).insert(anyString(), anyString(), anyString(), anyString(), any(Date.class), any(Date.class), anyString(), anyString(), anyString(), anyString());
    // TODO: after implementing the SystemInfoCall, tests..etc modify this to actually check the date:
    // Right now it only checks if: (Date) null is an instance of Date.class, not a terribly useful:
    verify(resourceStore, times(1)).insert(anyString(), any(Date.class));
}
Also used : Headers(okhttp3.Headers) Payload(org.hisp.dhis.android.core.common.Payload) Date(java.util.Date) Test(org.junit.Test)

Example 55 with Headers

use of com.amazonaws.services.s3.Headers in project autorest.java by Azure.

the class HeaderOperationsTests method responseDate.

@Test
public void responseDate() throws Exception {
    lock = new CountDownLatch(1);
    client.headers().responseDateWithServiceResponseAsync("valid").subscribe(new Action1<ServiceResponseWithHeaders<Void, HeaderResponseDateHeaders>>() {

        @Override
        public void call(ServiceResponseWithHeaders<Void, HeaderResponseDateHeaders> response) {
            Headers headers = response.response().headers();
            if (headers.get("value") != null) {
                Assert.assertEquals("2010-01-01", headers.get("value"));
                lock.countDown();
            }
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            fail();
        }
    });
    Assert.assertTrue(lock.await(1000, TimeUnit.MILLISECONDS));
    lock = new CountDownLatch(1);
    client.headers().responseDateWithServiceResponseAsync("min").subscribe(new Action1<ServiceResponseWithHeaders<Void, HeaderResponseDateHeaders>>() {

        @Override
        public void call(ServiceResponseWithHeaders<Void, HeaderResponseDateHeaders> response) {
            Headers headers = response.response().headers();
            if (headers.get("value") != null) {
                Assert.assertEquals("0001-01-01", headers.get("value"));
                lock.countDown();
            }
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            fail();
        }
    });
    Assert.assertTrue(lock.await(1000, TimeUnit.MILLISECONDS));
}
Also used : HeaderResponseDatetimeHeaders(fixtures.header.models.HeaderResponseDatetimeHeaders) HeaderResponseEnumHeaders(fixtures.header.models.HeaderResponseEnumHeaders) HeaderResponseLongHeaders(fixtures.header.models.HeaderResponseLongHeaders) HeaderResponseProtectedKeyHeaders(fixtures.header.models.HeaderResponseProtectedKeyHeaders) Headers(okhttp3.Headers) HeaderResponseDurationHeaders(fixtures.header.models.HeaderResponseDurationHeaders) HeaderResponseDatetimeRfc1123Headers(fixtures.header.models.HeaderResponseDatetimeRfc1123Headers) ServiceResponseWithHeaders(com.microsoft.rest.ServiceResponseWithHeaders) HeaderResponseDateHeaders(fixtures.header.models.HeaderResponseDateHeaders) HeaderResponseByteHeaders(fixtures.header.models.HeaderResponseByteHeaders) HeaderResponseDoubleHeaders(fixtures.header.models.HeaderResponseDoubleHeaders) HeaderResponseBoolHeaders(fixtures.header.models.HeaderResponseBoolHeaders) HeaderResponseStringHeaders(fixtures.header.models.HeaderResponseStringHeaders) HeaderResponseExistingKeyHeaders(fixtures.header.models.HeaderResponseExistingKeyHeaders) HeaderResponseFloatHeaders(fixtures.header.models.HeaderResponseFloatHeaders) HeaderResponseIntegerHeaders(fixtures.header.models.HeaderResponseIntegerHeaders) CountDownLatch(java.util.concurrent.CountDownLatch) ServiceResponseWithHeaders(com.microsoft.rest.ServiceResponseWithHeaders) HeaderResponseDateHeaders(fixtures.header.models.HeaderResponseDateHeaders) Test(org.junit.Test)

Aggregations

Headers (okhttp3.Headers)128 Request (okhttp3.Request)61 Response (okhttp3.Response)54 Test (org.junit.Test)40 IOException (java.io.IOException)32 Call (okhttp3.Call)30 RequestBody (okhttp3.RequestBody)25 CancelledException (com.hippo.ehviewer.client.exception.CancelledException)20 EhException (com.hippo.ehviewer.client.exception.EhException)20 NoHAtHClientException (com.hippo.ehviewer.client.exception.NoHAtHClientException)20 ParseException (com.hippo.ehviewer.client.exception.ParseException)20 StatusCodeException (com.hippo.network.StatusCodeException)20 ResponseBody (okhttp3.ResponseBody)18 HttpHeaders (okhttp3.internal.http.HttpHeaders)18 MediaType (okhttp3.MediaType)15 ServiceResponseWithHeaders (com.microsoft.rest.ServiceResponseWithHeaders)14 HeaderResponseBoolHeaders (fixtures.header.models.HeaderResponseBoolHeaders)14 HeaderResponseByteHeaders (fixtures.header.models.HeaderResponseByteHeaders)14 HeaderResponseDateHeaders (fixtures.header.models.HeaderResponseDateHeaders)14 HeaderResponseDatetimeHeaders (fixtures.header.models.HeaderResponseDatetimeHeaders)14