Search in sources :

Example 31 with Builder

use of okhttp3.Request.Builder in project Fast-Android-Networking by amitshekhariitbhu.

the class Rx2InternalNetworking method generateSimpleObservable.

public static <T> Observable<T> generateSimpleObservable(Rx2ANRequest request) {
    Request okHttpRequest;
    Request.Builder builder = new Request.Builder().url(request.getUrl());
    InternalNetworking.addHeadersToRequestBuilder(builder, request);
    RequestBody requestBody;
    switch(request.getMethod()) {
        case GET:
            {
                builder = builder.get();
                break;
            }
        case POST:
            {
                requestBody = request.getRequestBody();
                builder = builder.post(requestBody);
                break;
            }
        case PUT:
            {
                requestBody = request.getRequestBody();
                builder = builder.put(requestBody);
                break;
            }
        case DELETE:
            {
                requestBody = request.getRequestBody();
                builder = builder.delete(requestBody);
                break;
            }
        case HEAD:
            {
                builder = builder.head();
                break;
            }
        case PATCH:
            {
                requestBody = request.getRequestBody();
                builder = builder.patch(requestBody);
                break;
            }
    }
    if (request.getCacheControl() != null) {
        builder.cacheControl(request.getCacheControl());
    }
    okHttpRequest = builder.build();
    if (request.getOkHttpClient() != null) {
        request.setCall(request.getOkHttpClient().newBuilder().cache(InternalNetworking.sHttpClient.cache()).build().newCall(okHttpRequest));
    } else {
        request.setCall(InternalNetworking.sHttpClient.newCall(okHttpRequest));
    }
    return new SimpleANObservable<>(request);
}
Also used : Request(okhttp3.Request) RequestBody(okhttp3.RequestBody)

Example 32 with Builder

use of okhttp3.Request.Builder in project keywhiz by square.

the class ClientResourceTest method clientDelete_success.

@Test
public void clientDelete_success() throws Exception {
    // Sample client
    create(CreateClientRequestV2.builder().name("to-delete").build());
    // Deleting is successful
    Response httpResponse = delete("to-delete");
    assertThat(httpResponse.code()).isEqualTo(204);
    // Deleting again produces not-found
    httpResponse = delete("to-delete");
    assertThat(httpResponse.code()).isEqualTo(404);
}
Also used : Response(okhttp3.Response) Test(org.junit.Test)

Example 33 with Builder

use of okhttp3.Request.Builder in project keywhiz by square.

the class ClientResourceTest method modifyClientGroups_notFound.

@Test
public void modifyClientGroups_notFound() throws Exception {
    ModifyGroupsRequestV2 request = ModifyGroupsRequestV2.builder().build();
    RequestBody body = RequestBody.create(JSON, mapper.writeValueAsString(request));
    Request put = clientRequest("/automation/v2/clients/non-existent/groups").put(body).build();
    Response httpResponse = mutualSslClient.newCall(put).execute();
    assertThat(httpResponse.code()).isEqualTo(404);
}
Also used : Response(okhttp3.Response) ModifyGroupsRequestV2(keywhiz.api.automation.v2.ModifyGroupsRequestV2) Request(okhttp3.Request) TestClients.clientRequest(keywhiz.TestClients.clientRequest) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 34 with Builder

use of okhttp3.Request.Builder in project keywhiz by square.

the class SecretResourceTest method createSecret_successUnVersioned.

//---------------------------------------------------------------------------------------
// createSecret
//---------------------------------------------------------------------------------------
@Test
public void createSecret_successUnVersioned() throws Exception {
    CreateSecretRequestV2 request = CreateSecretRequestV2.builder().name("secret1").content(encoder.encodeToString("supa secret".getBytes(UTF_8))).description("desc").metadata(ImmutableMap.of("owner", "root", "mode", "0440")).type("password").build();
    Response httpResponse = create(request);
    assertThat(httpResponse.code()).isEqualTo(201);
    URI location = URI.create(httpResponse.header(LOCATION));
    assertThat(location.getPath()).isEqualTo("/automation/v2/secrets/secret1");
}
Also used : Response(okhttp3.Response) CreateSecretRequestV2(keywhiz.api.automation.v2.CreateSecretRequestV2) URI(java.net.URI) Test(org.junit.Test)

Example 35 with Builder

use of okhttp3.Request.Builder in project keywhiz by square.

the class SecretResourceTest method backfillExpirationTest.

//---------------------------------------------------------------------------------------
// backfillExpiration
//---------------------------------------------------------------------------------------
@Test
public void backfillExpirationTest() throws Exception {
    byte[] certs = Resources.toByteArray(Resources.getResource("fixtures/expiring-certificates.crt"));
    byte[] pubring = Resources.toByteArray(Resources.getResource("fixtures/expiring-pubring.gpg"));
    byte[] p12 = Resources.toByteArray(Resources.getResource("fixtures/expiring-keystore.p12"));
    byte[] jceks = Resources.toByteArray(Resources.getResource("fixtures/expiring-keystore.jceks"));
    create(CreateSecretRequestV2.builder().name("certificate-chain.crt").content(encoder.encodeToString(certs)).build());
    create(CreateSecretRequestV2.builder().name("public-keyring.gpg").content(encoder.encodeToString(pubring)).build());
    create(CreateSecretRequestV2.builder().name("keystore.p12").content(encoder.encodeToString(p12)).build());
    create(CreateSecretRequestV2.builder().name("keystore.jceks").content(encoder.encodeToString(jceks)).build());
    Response response = backfillExpiration("certificate-chain.crt", ImmutableList.of());
    assertThat(response.isSuccessful()).isTrue();
    response = backfillExpiration("public-keyring.gpg", ImmutableList.of());
    assertThat(response.isSuccessful()).isTrue();
    response = backfillExpiration("keystore.p12", ImmutableList.of("password"));
    assertThat(response.isSuccessful()).isTrue();
    response = backfillExpiration("keystore.jceks", ImmutableList.of("password"));
    assertThat(response.isSuccessful()).isTrue();
    SecretDetailResponseV2 details = lookup("certificate-chain.crt");
    assertThat(details.expiry()).isEqualTo(1501533950);
    details = lookup("public-keyring.gpg");
    assertThat(details.expiry()).isEqualTo(1536442365);
    details = lookup("keystore.p12");
    assertThat(details.expiry()).isEqualTo(1681596851);
    details = lookup("keystore.jceks");
    assertThat(details.expiry()).isEqualTo(1681596851);
}
Also used : Response(okhttp3.Response) SecretDetailResponseV2(keywhiz.api.automation.v2.SecretDetailResponseV2) Test(org.junit.Test)

Aggregations

Request (okhttp3.Request)163 Response (okhttp3.Response)120 OkHttpClient (okhttp3.OkHttpClient)91 IOException (java.io.IOException)83 RequestBody (okhttp3.RequestBody)67 Test (org.junit.Test)67 MultipartBody (okhttp3.MultipartBody)35 File (java.io.File)31 Map (java.util.Map)31 MockResponse (okhttp3.mockwebserver.MockResponse)31 HttpUrl (okhttp3.HttpUrl)29 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)27 Call (okhttp3.Call)25 Interceptor (okhttp3.Interceptor)24 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)22 Retrofit (retrofit2.Retrofit)20 Builder (okhttp3.Request.Builder)19 ResponseBody (okhttp3.ResponseBody)18 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)17 Builder (okhttp3.OkHttpClient.Builder)17