Search in sources :

Example 1 with Phone

use of retrofit2.converter.protobuf.PhoneProtos.Phone 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 2 with Phone

use of retrofit2.converter.protobuf.PhoneProtos.Phone in project retrofit by square.

the class ProtoConverterFactoryTest method deserializeEmpty.

@Test
public void deserializeEmpty() throws IOException {
    server.enqueue(new MockResponse());
    Call<Phone> call = service.get();
    Response<Phone> response = call.execute();
    Phone body = response.body();
    assertThat(body.hasNumber()).isFalse();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Phone(retrofit2.converter.protobuf.PhoneProtos.Phone) Test(org.junit.Test)

Example 3 with Phone

use of retrofit2.converter.protobuf.PhoneProtos.Phone in project retrofit by square.

the class ProtoConverterFactoryTest method deserializeUsesRegistry.

@Test
public void deserializeUsesRegistry() throws IOException {
    ByteString encoded = ByteString.decodeBase64("Cg4oNTE5KSA4NjctNTMwORAB");
    server.enqueue(new MockResponse().setBody(new Buffer().write(encoded)));
    Call<Phone> call = serviceWithRegistry.get();
    Response<Phone> response = call.execute();
    Phone body = response.body();
    assertThat(body.getNumber()).isEqualTo("(519) 867-5309");
    assertThat(body.getExtension(PhoneProtos.voicemail)).isEqualTo(true);
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) ByteString(okio.ByteString) Phone(retrofit2.converter.protobuf.PhoneProtos.Phone) Test(org.junit.Test)

Aggregations

MockResponse (okhttp3.mockwebserver.MockResponse)3 Test (org.junit.Test)3 Phone (retrofit2.converter.protobuf.PhoneProtos.Phone)3 Buffer (okio.Buffer)2 ByteString (okio.ByteString)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1