Search in sources :

Example 61 with BufferedSource

use of okio.BufferedSource in project azure-sdk-for-java by Azure.

the class MockIntegrationTestBase method extractResponseData.

private void extractResponseData(Map<String, String> responseData, Response response) throws Exception {
    Map<String, List<String>> headers = response.headers().toMultimap();
    boolean addedRetryAfter = false;
    for (Entry<String, List<String>> header : headers.entrySet()) {
        String headerValueToStore = header.getValue().get(0);
        if (header.getKey().equalsIgnoreCase("location") || header.getKey().equalsIgnoreCase("azure-asyncoperation")) {
            headerValueToStore = applyRegex(headerValueToStore);
        }
        if (header.getKey().equalsIgnoreCase("retry-after")) {
            headerValueToStore = "0";
            addedRetryAfter = true;
        }
        responseData.put(header.getKey().toLowerCase(), headerValueToStore);
    }
    if (!addedRetryAfter) {
        responseData.put("retry-after", "0");
    }
    BufferedSource bufferedSource = response.body().source();
    bufferedSource.request(9223372036854775807L);
    Buffer buffer = bufferedSource.buffer().clone();
    String content = null;
    if (response.header("Content-Encoding") == null) {
        content = new String(buffer.readString(Util.UTF_8));
    } else if (response.header("Content-Encoding").equalsIgnoreCase("gzip")) {
        GZIPInputStream gis = new GZIPInputStream(buffer.inputStream());
        content = IOUtils.toString(gis);
        responseData.remove("Content-Encoding".toLowerCase());
        responseData.put("Content-Length".toLowerCase(), Integer.toString(content.length()));
    }
    if (content != null) {
        content = applyRegex(content);
        responseData.put("Body", content);
    }
}
Also used : Buffer(okio.Buffer) GZIPInputStream(java.util.zip.GZIPInputStream) List(java.util.List) BufferedSource(okio.BufferedSource)

Example 62 with BufferedSource

use of okio.BufferedSource in project azure-sdk-for-java by Azure.

the class ProviderRegistrationInterceptor method errorBody.

private String errorBody(ResponseBody responseBody) throws IOException {
    if (responseBody == null) {
        return null;
    }
    BufferedSource source = responseBody.source();
    // Buffer the entire body.
    source.request(Long.MAX_VALUE);
    Buffer buffer = source.buffer();
    return buffer.clone().readUtf8();
}
Also used : Buffer(okio.Buffer) BufferedSource(okio.BufferedSource)

Example 63 with BufferedSource

use of okio.BufferedSource in project okhttp by square.

the class OkUrlFactoryTest method assertResponseBody.

private void assertResponseBody(HttpURLConnection connection, String expected) throws Exception {
    BufferedSource source = buffer(source(connection.getInputStream()));
    String actual = source.readString(US_ASCII);
    source.close();
    assertEquals(expected, actual);
}
Also used : BufferedSource(okio.BufferedSource)

Example 64 with BufferedSource

use of okio.BufferedSource in project retrofit by square.

the class CallTest method conversionProblemIncomingMaskedByConverterIsUnwrapped.

@Test
public void conversionProblemIncomingMaskedByConverterIsUnwrapped() throws IOException {
    // MWS has no way to trigger IOExceptions during the response body so use an interceptor.
    OkHttpClient client = //
    new OkHttpClient.Builder().addInterceptor(new Interceptor() {

        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            okhttp3.Response response = chain.proceed(chain.request());
            ResponseBody body = response.body();
            BufferedSource source = Okio.buffer(new ForwardingSource(body.source()) {

                @Override
                public long read(Buffer sink, long byteCount) throws IOException {
                    throw new IOException("cause");
                }
            });
            body = ResponseBody.create(body.contentType(), body.contentLength(), source);
            return response.newBuilder().body(body).build();
        }
    }).build();
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).client(client).addConverterFactory(new ToStringConverterFactory() {

        @Override
        public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
            return new Converter<ResponseBody, String>() {

                @Override
                public String convert(ResponseBody value) throws IOException {
                    try {
                        return value.string();
                    } catch (IOException e) {
                        // Some serialization libraries mask transport problems in runtime exceptions. Bad!
                        throw new RuntimeException("wrapper", e);
                    }
                }
            };
        }
    }).build();
    Service example = retrofit.create(Service.class);
    server.enqueue(new MockResponse().setBody("Hi"));
    Call<String> call = example.getString();
    try {
        call.execute();
        fail();
    } catch (IOException e) {
        assertThat(e).hasMessage("cause");
    }
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) OkHttpClient(okhttp3.OkHttpClient) ForwardingSource(okio.ForwardingSource) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Type(java.lang.reflect.Type) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Interceptor(okhttp3.Interceptor) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 65 with BufferedSource

use of okio.BufferedSource in project tape by square.

the class QueueFileTest method removingElementZeroesData.

@Test
public void removingElementZeroesData() throws IOException {
    QueueFile queueFile = newQueueFile(true);
    queueFile.add(values[4]);
    queueFile.remove();
    queueFile.close();
    BufferedSource source = Okio.buffer(Okio.source(file));
    source.skip(headerLength);
    source.skip(Element.HEADER_LENGTH);
    assertThat(source.readByteString(4).hex()).isEqualTo("00000000");
}
Also used : BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Aggregations

BufferedSource (okio.BufferedSource)97 Test (org.junit.Test)51 IOException (java.io.IOException)23 Buffer (okio.Buffer)23 Request (okhttp3.Request)10 Response (okhttp3.Response)10 Gson (com.google.gson.Gson)8 EOFException (java.io.EOFException)8 ResponseBody (okhttp3.ResponseBody)8 BarCode (com.nytimes.android.external.store.base.impl.BarCode)7 BarCode (com.nytimes.android.external.store3.base.impl.BarCode)7 Charset (java.nio.charset.Charset)7 MediaType (okhttp3.MediaType)7 MockResponse (okhttp3.mockwebserver.MockResponse)7 File (java.io.File)6 InputStream (java.io.InputStream)6 Headers (okhttp3.Headers)6 RequestBody (okhttp3.RequestBody)6 BufferedSink (okio.BufferedSink)6 List (java.util.List)4