Search in sources :

Example 66 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 67 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)

Example 68 with BufferedSource

use of okio.BufferedSource in project wire by square.

the class SchemaLoader method loadDescriptorProto.

/**
   * Returns Google's protobuf descriptor, which defines standard options like default, deprecated,
   * and java_package. If the user has provided their own version of the descriptor proto, that is
   * preferred.
   */
private ProtoFile loadDescriptorProto() throws IOException {
    InputStream resourceAsStream = SchemaLoader.class.getResourceAsStream("/" + DESCRIPTOR_PROTO);
    try (BufferedSource buffer = Okio.buffer(Okio.source(resourceAsStream))) {
        String data = buffer.readUtf8();
        Location location = Location.get("", DESCRIPTOR_PROTO);
        ProtoFileElement element = ProtoParser.parse(location, data);
        return ProtoFile.get(element);
    }
}
Also used : InputStream(java.io.InputStream) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement) BufferedSource(okio.BufferedSource)

Example 69 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 70 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)

Aggregations

BufferedSource (okio.BufferedSource)72 Test (org.junit.Test)33 Buffer (okio.Buffer)21 IOException (java.io.IOException)19 Request (okhttp3.Request)8 Response (okhttp3.Response)8 BarCode (com.nytimes.android.external.store.base.impl.BarCode)7 MockResponse (okhttp3.mockwebserver.MockResponse)7 EOFException (java.io.EOFException)6 InputStream (java.io.InputStream)6 ResponseBody (okhttp3.ResponseBody)6 Gson (com.google.gson.Gson)4 File (java.io.File)4 Charset (java.nio.charset.Charset)4 Headers (okhttp3.Headers)4 MediaType (okhttp3.MediaType)4 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)4 BufferedSink (okio.BufferedSink)4 Sink (okio.Sink)4 RequestBody (okhttp3.RequestBody)3