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.auth.xsrf.XsrfServletFilterIntegrationTest.java

License:Apache License

@Test
public void xsrfNotRequiredForLogout() 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()).isNotEqualTo(401);
}

From source file:keywhiz.AuthHelper.java

License:Apache License

/**
 * Builds a Keywhiz login request.//from   w  ww  . ja v a 2s. c  o  m
 *
 * @param username login username
 * @param password login password
 * @return valid login request, given a valid username and password
 */
public static Request buildLoginPost(@Nullable String username, @Nullable String password) {
    Map<String, String> login = Maps.newHashMap();
    if (username != null) {
        login.put("username", username);
    }

    if (password != null) {
        login.put("password", password);
    }

    RequestBody body;
    try {
        body = RequestBody.create(KeywhizClient.JSON, MAPPER.writeValueAsString(login));
    } catch (JsonProcessingException e) {
        throw new AssertionError(e);
    }

    return new Request.Builder().url(HttpClients.testUrl("/admin/login")).post(body)
            .addHeader("Content-Type", MediaType.APPLICATION_JSON).build();
}

From source file:keywhiz.client.KeywhizClient.java

License:Apache License

private String httpPost(HttpUrl url, Object content) throws IOException {
    RequestBody body = RequestBody.create(JSON, mapper.writeValueAsString(content));
    Request request = new Request.Builder().url(url).post(body)
            .addHeader(HttpHeaders.CONTENT_TYPE, JSON.toString()).build();

    Response response = client.newCall(request).execute();
    throwOnCommonError(response);/*from www .  ja  va2s.co  m*/
    return response.body().string();
}

From source file:keywhiz.client.KeywhizClient.java

License:Apache License

private String httpPut(HttpUrl url) throws IOException {
    Request request = new Request.Builder().url(url).put(RequestBody.create(MediaType.parse("text/plain"), ""))
            .build();//from  w  w w  .j  a v  a  2  s . c  o  m

    Response response = client.newCall(request).execute();
    throwOnCommonError(response);
    return response.body().string();
}

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

License:Apache License

@Test
public void addClients() throws Exception {
    CreateClientRequest request = new CreateClientRequest("User1");
    String requestJSON = mapper.writeValueAsString(request);
    RequestBody body = RequestBody.create(KeywhizClient.JSON, requestJSON);
    Request post = new Request.Builder().post(body).url(testUrl("/automation/clients"))
            .addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON)
            .addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).build();

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

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

License:Apache License

@Test
public void addClientRedundant() throws Exception {
    CreateClientRequest request = new CreateClientRequest("CN=User1");
    String json = mapper.writeValueAsString(request);
    Request post = new Request.Builder().post(RequestBody.create(KeywhizClient.JSON, json))
            .url(testUrl("/automation/clients")).addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON)
            .addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).build();

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

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

License:Apache License

@Test
public void deleteClient() throws Exception {
    String json = mapper.writeValueAsString(new CreateClientRequest("ShortLived"));
    Request post = new Request.Builder().post(RequestBody.create(KeywhizClient.JSON, json))
            .url(testUrl("/automation/clients")).addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON)
            .addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).build();
    Response response = mutualSslClient.newCall(post).execute();
    assertThat(response.code()).isEqualTo(200);

    long clientId = mapper.readValue(response.body().string(), ClientDetailResponse.class).id;
    Request delete = new Request.Builder().delete().url(testUrl("/automation/clients/" + clientId)).build();
    response = mutualSslClient.newCall(delete).execute();
    assertThat(response.code()).isEqualTo(200);

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

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

License:Apache License

@Test
public void enrollClient() throws Exception {
    //Enroll "CN=User4" in "Blackops"
    Request post = new Request.Builder().put(RequestBody.create(MediaType.parse("text/plain"), ""))
            .url(testUrl("/automation/clients/772/groups/916")).build();

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

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

License:Apache License

@Test
public void createGroup() throws Exception {
    CreateGroupRequest request = new CreateGroupRequest("newgroup", "group-description");
    String body = mapper.writeValueAsString(request);
    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);
}

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

License:Apache License

@Test
public void createGroupRedundant() throws Exception {
    CreateGroupRequest request = new CreateGroupRequest("Web", "group-description");
    String body = mapper.writeValueAsString(request);
    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(409);
}