Example usage for com.squareup.okhttp RequestBody create

List of usage examples for com.squareup.okhttp RequestBody create

Introduction

In this page you can find the example usage for com.squareup.okhttp RequestBody create.

Prototype

public static RequestBody create(final MediaType contentType, final File file) 

Source Link

Document

Returns a new request body that transmits the content of file .

Usage

From source file:keywhiz.service.resources.AutomationGroupResourceIntegrationTest.java

License:Apache License

@Test
public void deleteGroup() throws Exception {
    String body = mapper.writeValueAsString(new CreateGroupRequest("short-lived", "group-description"));
    Request post = new Request.Builder().post(RequestBody.create(KeywhizClient.JSON, body))
            .url(testUrl("/automation/groups")).addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
            .addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON).build();
    Response response = mutualSslClient.newCall(post).execute();
    assertThat(response.code()).isEqualTo(200);
    long groupId = mapper.readValue(response.body().string(), Group.class).getId();

    Request delete = new Request.Builder().delete().url(testUrl("/automation/groups/" + groupId)).build();
    response = mutualSslClient.newCall(delete).execute();
    assertThat(response.code()).isEqualTo(200);

    Request lookup = new Request.Builder().get().url(testUrl("/automation/groups/" + groupId))
            .addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON).build();
    response = mutualSslClient.newCall(lookup).execute();
    assertThat(response.code()).isEqualTo(404);
}

From source file:keywhiz.service.resources.AutomationSecretAccessResourceIntegrationTest.java

License:Apache License

@Test
public void allowAccess() throws Exception {
    //Allow "Web" to access "Hacking_Password"
    Request put = new Request.Builder().put(RequestBody.create(MediaType.parse("text/plain"), ""))
            .url(testUrl("/automation/secrets/738/groups/918")).build();

    Response response = mutualSslClient.newCall(put).execute();
    assertThat(response.code()).isEqualTo(200);
}

From source file:keywhiz.service.resources.AutomationSecretResourceIntegrationTest.java

License:Apache License

@Test
public void addSecrets() throws Exception {
    CreateSecretRequest request = new CreateSecretRequest("new_secret", "desc", "superSecret", true,
            ImmutableMap.of());//  w  w  w .ja  v a2  s  .c  o  m
    String body = mapper.writeValueAsString(request);
    Request post = new Request.Builder().post(RequestBody.create(KeywhizClient.JSON, body))
            .url(testUrl("/automation/secrets")).addHeader("Content-Type", MediaType.APPLICATION_JSON).build();

    Response response = mutualSslClient.newCall(post).execute();
    assertThat(response.code()).isEqualTo(200);
}

From source file:keywhiz.service.resources.AutomationSecretResourceIntegrationTest.java

License:Apache License

@Test
public void addInvalidSecrets() throws Exception {
    CreateSecretRequest request = new CreateSecretRequest("empty_secret", "desc", "", true, null);
    String body = mapper.writeValueAsString(request);
    Request post = new Request.Builder().post(RequestBody.create(KeywhizClient.JSON, body))
            .url(testUrl("/automation/secrets")).addHeader("Content-Type", MediaType.APPLICATION_JSON).build();

    Response response = mutualSslClient.newCall(post).execute();
    assertThat(response.code()).isEqualTo(422);
}

From source file:keywhiz.service.resources.AutomationSecretResourceIntegrationTest.java

License:Apache License

@Test
public void addConflictingSecrets() throws Exception {
    CreateSecretRequest request = new CreateSecretRequest("dup_secret", "desc", "content", false,
            ImmutableMap.of());/*  ww w  . j ava 2s . c o m*/
    String body = mapper.writeValueAsString(request);
    Request post = new Request.Builder().post(RequestBody.create(KeywhizClient.JSON, body))
            .url(testUrl("/automation/secrets")).addHeader("Content-Type", MediaType.APPLICATION_JSON).build();

    Response response = mutualSslClient.newCall(post).execute();
    assertThat(response.code()).isEqualTo(200);
    response = mutualSslClient.newCall(post).execute();
    assertThat(response.code()).isEqualTo(409);
}

From source file:keywhiz.service.resources.AutomationSecretResourceIntegrationTest.java

License:Apache License

@Test
public void readValidSecret() throws Exception {
    CreateSecretRequest request = new CreateSecretRequest("readable", "desc", "c3VwZXJTZWNyZXQK", false,
            ImmutableMap.of());/*ww w .j  a  v  a2s.  c  o m*/
    String body = mapper.writeValueAsString(request);
    Request post = new Request.Builder().post(RequestBody.create(KeywhizClient.JSON, body))
            .url(testUrl("/automation/secrets")).addHeader("Content-Type", MediaType.APPLICATION_JSON).build();

    Response response = mutualSslClient.newCall(post).execute();
    assertThat(response.code()).isEqualTo(200);

    Request get = new Request.Builder().get().url(testUrl("/automation/secrets?name=readable")).build();
    response = mutualSslClient.newCall(get).execute();
    assertThat(response.code()).isEqualTo(200);
}

From source file:keywhiz.service.resources.AutomationSecretResourceIntegrationTest.java

License:Apache License

@Test
public void deleteSecrets() throws Exception {
    CreateSecretRequest request = new CreateSecretRequest("deletable", "desc", "c3VwZXJTZWNyZXQK", true,
            ImmutableMap.of());/*from ww  w . j a va  2 s . c  om*/
    String body = mapper.writeValueAsString(request);
    Request post = new Request.Builder().post(RequestBody.create(KeywhizClient.JSON, body))
            .url(testUrl("/automation/secrets")).addHeader("Content-Type", MediaType.APPLICATION_JSON).build();

    Response response = mutualSslClient.newCall(post).execute();
    assertThat(response.code()).isEqualTo(200);

    Request delete = new Request.Builder().delete().url(testUrl("/automation/secrets/deletable")).build();

    response = mutualSslClient.newCall(delete).execute();
    assertThat(response.code()).isEqualTo(200);
}

From source file:keywhiz.service.resources.SessionLoginResourceIntegrationTest.java

License:Apache License

@Test
public void respondsToLogin() throws Exception {
    Request post = new Request.Builder().post(RequestBody.create(MediaType.parse("text/plain"), ""))
            .url(testUrl("/admin/login")).build();

    Response response = client.newCall(post).execute();
    assertThat(response.code()).isNotEqualTo(404);
}

From source file:keywhiz.service.resources.SessionLogoutResourceIntegrationTest.java

License:Apache License

@Test
public void sendsExpiredCookie() throws Exception {
    Request request = new Request.Builder().post(RequestBody.create(MediaType.parse("text/plain"), ""))
            .url(testUrl("/admin/logout")).build();

    Response response = client.newCall(request).execute();
    assertThat(response.code()).isEqualTo(303);

    List<String> cookies = response.headers(HttpHeaders.SET_COOKIE);
    assertThat(cookies).hasSize(1);/*from ww w.  ja  v a 2s  . com*/

    NewCookie cookie = NewCookie.valueOf(cookies.get(0));
    assertThat(cookie.getName()).isEqualTo("session");
    assertThat(cookie.getValue()).isEqualTo("expired");
    assertThat(cookie.getVersion()).isEqualTo(1);
    assertThat(cookie.getPath()).isEqualTo("/admin");
    assertThat(cookie.isSecure()).isTrue();
    assertThat(cookie.isHttpOnly()).isTrue();
    assertThat(cookie.getExpiry()).isEqualTo(new Date(0));
}

From source file:library.util.OkHttpStack.java

License:Apache License

@SuppressWarnings("deprecation")
private static void setConnectionParametersForRequest(Builder builder, Request<?> request)
        throws IOException, AuthFailureError {
    switch (request.getMethod()) {
    case Request.Method.DEPRECATED_GET_OR_POST:
        // Ensure backwards compatibility.  Volley assumes a request with a null body is a GET.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            builder.post(RequestBody.create(MediaType.parse(request.getPostBodyContentType()), postBody));
        }/*from   w  w w .  j  a  v  a2  s  .c  o m*/
        break;
    case Request.Method.GET:
        builder.get();
        break;
    case Request.Method.DELETE:
        builder.delete();
        break;
    case Request.Method.POST:
        builder.post(createRequestBody(request));
        break;
    case Request.Method.PUT:
        builder.put(createRequestBody(request));
        break;
    case Request.Method.HEAD:
        builder.head();
        break;
    case Request.Method.OPTIONS:
        builder.method("OPTIONS", null);
        break;
    case Request.Method.TRACE:
        builder.method("TRACE", null);
        break;
    case Request.Method.PATCH:
        builder.patch(createRequestBody(request));
        break;
    default:
        throw new IllegalStateException("Unknown method type.");
    }
}