Search in sources :

Example 36 with ByteString

use of okio.ByteString in project zipkin by openzipkin.

the class AWSSignatureVersion4 method sign.

Request sign(Request input) throws IOException {
    AWSCredentials credentials = checkNotNull(this.credentials.get(), "awsCredentials");
    String timestamp = iso8601.get().format(new Date());
    String yyyyMMdd = timestamp.substring(0, 8);
    String credentialScope = format("%s/%s/%s/%s", yyyyMMdd, region, service, "aws4_request");
    Request.Builder builder = input.newBuilder();
    builder.header(HOST, input.url().host());
    builder.header(X_AMZ_DATE, timestamp);
    if (credentials.sessionToken != null) {
        builder.header(X_AMZ_SECURITY_TOKEN, credentials.sessionToken);
    }
    Buffer canonicalString = canonicalString(builder.build());
    String signedHeaders = credentials.sessionToken == null ? HOST_DATE : HOST_DATE_TOKEN;
    Buffer toSign = toSign(timestamp, credentialScope, canonicalString);
    // TODO: this key is invalid when the secret key or the date change. both are very infrequent
    ByteString signatureKey = signatureKey(credentials.secretKey, yyyyMMdd);
    String signature = toSign.readByteString().hmacSha256(signatureKey).hex();
    String authorization = new StringBuilder().append("AWS4-HMAC-SHA256 Credential=").append(credentials.accessKey).append('/').append(credentialScope).append(", SignedHeaders=").append(signedHeaders).append(", Signature=").append(signature).toString();
    return builder.header("authorization", authorization).build();
}
Also used : Buffer(okio.Buffer) ByteString(okio.ByteString) Request(okhttp3.Request) ByteString(okio.ByteString) Date(java.util.Date)

Example 37 with ByteString

use of okio.ByteString in project okhttp by square.

the class WebSocketWriterTest method assertData.

private void assertData(String hex) throws EOFException {
    ByteString expected = ByteString.decodeHex(hex);
    ByteString actual = data.readByteString(expected.size());
    assertEquals(expected, actual);
}
Also used : ByteString(okio.ByteString)

Example 38 with ByteString

use of okio.ByteString in project retrofit by square.

the class ProtoConverterFactoryTest method deserializeWrongType.

@Test
public void deserializeWrongType() throws IOException {
    ByteString encoded = ByteString.decodeBase64("Cg4oNTE5KSA4NjctNTMwOQ==");
    server.enqueue(new MockResponse().setBody(new Buffer().write(encoded)));
    try {
        service.wrongType();
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessage("" + "Unable to create converter for java.util.List<java.lang.String>\n" + "    for method Service.wrongType");
        assertThat(e.getCause()).hasMessage("" + "Could not locate ResponseBody converter for java.util.List<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 39 with ByteString

use of okio.ByteString 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)

Example 40 with ByteString

use of okio.ByteString in project retrofit by square.

the class ProtoConverterFactoryTest 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 (RuntimeException e) {
        assertThat(e.getCause()).isInstanceOf(InvalidProtocolBufferException.class).hasMessageContaining("input ended unexpectedly");
    }
}
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