Search in sources :

Example 6 with ByteString

use of okio.ByteString in project okhttp by square.

the class Http2Reader method readGoAway.

private void readGoAway(Handler handler, int length, byte flags, int streamId) throws IOException {
    if (length < 8)
        throw ioException("TYPE_GOAWAY length < 8: %s", length);
    if (streamId != 0)
        throw ioException("TYPE_GOAWAY streamId != 0");
    int lastStreamId = source.readInt();
    int errorCodeInt = source.readInt();
    int opaqueDataLength = length - 8;
    ErrorCode errorCode = ErrorCode.fromHttp2(errorCodeInt);
    if (errorCode == null) {
        throw ioException("TYPE_GOAWAY unexpected error code: %d", errorCodeInt);
    }
    ByteString debugData = EMPTY;
    if (opaqueDataLength > 0) {
        // Must read debug data in order to not corrupt the connection.
        debugData = source.readByteString(opaqueDataLength);
    }
    handler.goAway(lastStreamId, errorCode, debugData);
}
Also used : ByteString(okio.ByteString)

Example 7 with ByteString

use of okio.ByteString in project retrofit by square.

the class ProtoConverterFactoryTest method deserializeWrongClass.

@Test
public void deserializeWrongClass() throws IOException {
    ByteString encoded = ByteString.decodeBase64("Cg4oNTE5KSA4NjctNTMwOQ==");
    server.enqueue(new MockResponse().setBody(new Buffer().write(encoded)));
    try {
        service.wrongClass();
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessage("" + "Unable to create converter for class java.lang.String\n" + "    for method Service.wrongClass");
        assertThat(e.getCause()).hasMessage("" + "Could not locate ResponseBody converter for class java.lang.String.\n" + "  Tried:\n" + "   * retrofit2.BuiltInConverters\n" + "   * retrofit2.converter.protobuf.ProtoConverterFactory");
    }
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) ByteString(okio.ByteString) Test(org.junit.Test)

Example 8 with ByteString

use of okio.ByteString in project retrofit by square.

the class ProtoConverterFactoryTest method serializeAndDeserialize.

@Test
public void serializeAndDeserialize() throws IOException, InterruptedException {
    ByteString encoded = ByteString.decodeBase64("Cg4oNTE5KSA4NjctNTMwOQ==");
    server.enqueue(new MockResponse().setBody(new Buffer().write(encoded)));
    Call<Phone> call = service.post(Phone.newBuilder().setNumber("(519) 867-5309").build());
    Response<Phone> response = call.execute();
    Phone body = response.body();
    assertThat(body.getNumber()).isEqualTo("(519) 867-5309");
    RecordedRequest request = server.takeRequest();
    assertThat(request.getBody().readByteString()).isEqualTo(encoded);
    assertThat(request.getHeader("Content-Type")).isEqualTo("application/x-protobuf");
}
Also used : Buffer(okio.Buffer) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) ByteString(okio.ByteString) Phone(retrofit2.converter.protobuf.PhoneProtos.Phone) Test(org.junit.Test)

Example 9 with ByteString

use of okio.ByteString in project retrofit by square.

the class WireConverterFactoryTest method deserializeWrongValue.

@Test
public void deserializeWrongValue() throws IOException {
    ByteString encoded = ByteString.decodeBase64("////");
    server.enqueue(new MockResponse().setBody(new Buffer().write(encoded)));
    Call<?> call = service.get();
    try {
        call.execute();
        fail();
    } catch (EOFException ignored) {
    }
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) ByteString(okio.ByteString) EOFException(java.io.EOFException) Test(org.junit.Test)

Example 10 with ByteString

use of okio.ByteString in project retrofit by square.

the class WireConverterFactoryTest method deserializeWrongClass.

@Test
public void deserializeWrongClass() throws IOException {
    ByteString encoded = ByteString.decodeBase64("Cg4oNTE5KSA4NjctNTMwOQ==");
    server.enqueue(new MockResponse().setBody(new Buffer().write(encoded)));
    try {
        service.wrongClass();
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessage("" + "Unable to create converter for class java.lang.String\n" + "    for method Service.wrongClass");
        assertThat(e.getCause()).hasMessage("" + "Could not locate ResponseBody converter for class java.lang.String.\n" + "  Tried:\n" + "   * retrofit2.BuiltInConverters\n" + "   * retrofit2.converter.wire.WireConverterFactory");
    }
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) ByteString(okio.ByteString) Test(org.junit.Test)

Aggregations

ByteString (okio.ByteString)59 Test (org.junit.Test)37 Buffer (okio.Buffer)26 MockResponse (okhttp3.mockwebserver.MockResponse)11 ProtocolException (java.net.ProtocolException)5 IOException (java.io.IOException)4 OneField (com.squareup.wire.protos.edgecases.OneField)3 EOFException (java.io.EOFException)3 BufferedSink (okio.BufferedSink)3 BufferedSource (okio.BufferedSource)3 AllTypes (com.squareup.wire.protos.alltypes.AllTypes)2 Person (com.squareup.wire.protos.person.Person)2 ArrayList (java.util.ArrayList)2 Headers (okhttp3.Headers)2 Request (okhttp3.Request)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 GzipSink (okio.GzipSink)2 Ignore (org.junit.Ignore)2 Phone (retrofit2.converter.protobuf.PhoneProtos.Phone)2 GsonBuilder (com.google.gson.GsonBuilder)1