Example usage for org.apache.http.client.methods HttpPost getEntity

List of usage examples for org.apache.http.client.methods HttpPost getEntity

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpPost getEntity.

Prototype

public HttpEntity getEntity() 

Source Link

Usage

From source file:org.ow2.proactive_grid_cloud_portal.rm.server.serialization.CatalogRequestBuilderTest.java

private String getMultipartEntityString(HttpPost httpPost) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream((int) httpPost.getEntity().getContentLength());
    httpPost.getEntity().writeTo(out);//from  ww  w .  j  ava 2 s.c  om
    return out.toString();
}

From source file:com.parse.ParseApacheHttpClientTest.java

@Test
public void testGetApacheRequestWithEmptyContentType() throws Exception {
    String url = "http://www.parse.com/";
    String content = "test";
    ParseHttpRequest parseRequest = new ParseHttpRequest.Builder().setUrl(url)
            .setMethod(ParseHttpRequest.Method.POST).setBody(new ParseByteArrayHttpBody(content, null)).build();

    ParseApacheHttpClient parseClient = new ParseApacheHttpClient(10000, null);
    HttpUriRequest apacheRequest = parseClient.getRequest(parseRequest);

    // Verify Content-Type
    HttpPost apachePostRequest = (HttpPost) apacheRequest;
    assertNull(apachePostRequest.getEntity().getContentType());
}

From source file:com.ksc.http.apache.client.impl.ApacheDefaultHttpRequestFactoryTest.java

@Test
public void query_parameters_moved_to_payload_for_post_request_with_no_payload()
        throws IOException, URISyntaxException {
    final Request<Object> request = newDefaultRequest(HttpMethodName.POST);
    request.withParameter("foo", "bar").withParameter("alpha", "beta");
    HttpRequestBase requestBase = requestFactory.create(request, settings);
    Assert.assertThat(requestBase, Matchers.instanceOf(HttpPost.class));
    HttpPost post = (HttpPost) requestBase;
    HttpEntity entity = post.getEntity();
    byte[] actualContents = drainInputStream(entity.getContent());
    Assert.assertTrue(actualContents.length > 0);
}

From source file:com.parse.ParseApacheHttpClientTest.java

@Test
public void testGetApacheRequest() throws IOException {
    Map<String, String> headers = new HashMap<>();
    String headerValue = "value";
    headers.put("name", headerValue);

    String url = "http://www.parse.com/";
    String content = "test";
    String contentType = "application/json";
    ParseHttpRequest parseRequest = new ParseHttpRequest.Builder().setUrl(url)
            .setMethod(ParseHttpRequest.Method.POST).setBody(new ParseByteArrayHttpBody(content, contentType))
            .setHeaders(headers).build();

    ParseApacheHttpClient parseClient = new ParseApacheHttpClient(10000, null);
    HttpUriRequest apacheRequest = parseClient.getRequest(parseRequest);

    // Verify method
    assertTrue(apacheRequest instanceof HttpPost);
    // Verify URL
    assertEquals(url, apacheRequest.getURI().toString());
    // Verify Headers
    // We add gzip header to apacheRequest which does not contain in ParseRequest
    assertEquals(2, apacheRequest.getAllHeaders().length);
    assertEquals(1, apacheRequest.getHeaders("name").length);
    assertEquals("name", apacheRequest.getHeaders("name")[0].getName());
    assertEquals(headerValue, apacheRequest.getHeaders("name")[0].getValue());
    // Verify Body
    HttpPost apachePostRequest = (HttpPost) apacheRequest;
    assertEquals(4, apachePostRequest.getEntity().getContentLength());
    assertEquals(contentType, apachePostRequest.getEntity().getContentType().getValue());
    // Can not read parseRequest body to compare since it has been read during
    // creating apacheRequest
    assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(apachePostRequest.getEntity().getContent()));
}

From source file:org.camunda.connect.httpclient.HttpConnectorTest.java

@Test
public void shouldSetPayloadOnHttpRequest() throws IOException {
    connector.createRequest().url(EXAMPLE_URL).payload(EXAMPLE_PAYLOAD).post().execute();
    HttpPost request = interceptor.getTarget();
    String content = IoUtil.inputStreamAsString(request.getEntity().getContent());
    assertThat(content).isEqualTo(EXAMPLE_PAYLOAD);
}

From source file:com.github.restdriver.serverdriver.http.ByteArrayRequestBodyTest.java

@Test
public void bodyAppliesItselfToRequest() throws Exception {
    byte[] bytes = "MY STRING OF DOOM!".getBytes();
    HttpPost request = new HttpPost();
    ByteArrayRequestBody body = new ByteArrayRequestBody(bytes, "contentType");
    body.applyTo(new ServerDriverHttpUriRequest(request));
    assertThat(IOUtils.toString(request.getEntity().getContent()), is("MY STRING OF DOOM!"));
    assertThat(request.getEntity().getContentType().getValue(), is("contentType"));
    assertThat(request.getFirstHeader("Content-type").getValue(), is("contentType"));
}

From source file:com.github.restdriver.serverdriver.http.RequestBodyTest.java

@Test
public void bodyAppliesItselfToRequest() throws Exception {
    HttpPost request = new HttpPost();
    RequestBody body = new RequestBody("content", "content/type");
    body.applyTo(new ServerDriverHttpUriRequest(request));
    assertThat(IOUtils.toString(request.getEntity().getContent()), is("content"));
    assertThat(request.getEntity().getContentType().getValue(), is("content/type; charset=UTF-8"));
    assertThat(request.getFirstHeader("Content-type").getValue(), is("content/type"));
}

From source file:com.ibm.watson.app.common.services.nlclassifier.impl.BaseNLClassifierTest.java

@SuppressWarnings({ "unchecked", "rawtypes" })
protected void verify_http_client_execute_invoked(String method, String contentType, String urlSuffix)
        throws Exception {
    ArgumentCaptor<HttpUriRequest> uriCap = ArgumentCaptor.forClass(HttpUriRequest.class);
    ArgumentCaptor<ResponseHandler> respCap = ArgumentCaptor.forClass(ResponseHandler.class);
    ArgumentCaptor<HttpContext> cxtCap = ArgumentCaptor.forClass(HttpContext.class);

    verify(httpClient, times(1)).execute(uriCap.capture(), respCap.capture(), cxtCap.capture());

    assertNotNull(uriCap.getValue());//  ww w  .  jav  a  2  s .  c  o  m
    if (method.equalsIgnoreCase("post")) {
        assertTrue(uriCap.getValue().getClass().isAssignableFrom(HttpPost.class));
        HttpPost post = (HttpPost) uriCap.getValue();

        HttpEntity e = post.getEntity();
        assertNotNull(e);
        assertEquals(contentType, e.getContentType().getValue());
        assertTrue(e.getContentLength() > 0);

        entityContent = EntityUtils.toString(e, StandardCharsets.UTF_8);
        assertNotNull(entityContent);
        assertFalse(entityContent.isEmpty());
    } else if (method.equalsIgnoreCase("get")) {
        assertTrue(uriCap.getValue().getClass().isAssignableFrom(HttpGet.class));
    } else if (method.equalsIgnoreCase("delete")) {
        assertTrue(uriCap.getValue().getClass().isAssignableFrom(HttpDelete.class));
    }
    assertEquals(DEFAULT_URL + "/v1/classifiers" + urlSuffix, uriCap.getValue().getURI().toString());

    assertNotNull(respCap.getValue());
    assertNotNull(cxtCap.getValue());
}

From source file:io.github.jonestimd.neo4j.client.http.ApacheHttpDriverTest.java

@Test
public void post() throws Exception {
    BasicHeader header = new BasicHeader("", "header-value");
    StringEntity responseEntity = new StringEntity("response entity");
    when(client.execute(any(HttpUriRequest.class), any(HttpClientContext.class))).thenReturn(httpResponse);
    when(httpResponse.getFirstHeader(anyString())).thenReturn(null, header);
    when(httpResponse.getEntity()).thenReturn(responseEntity);

    HttpResponse response = driver.post(uri, "json entity");

    verify(client).execute(postCaptor.capture(), same(context));
    HttpPost post = postCaptor.getValue();
    assertThat(post.getURI().toString()).isEqualTo(uri);
    assertThat(post.getEntity().getContentType().getValue()).isEqualTo(ContentType.APPLICATION_JSON.toString());
    assertThat(getContent(post.getEntity().getContent())).isEqualTo("json entity");
    assertThat(response.getHeader("header1")).isNull();
    assertThat(response.getHeader("header2")).isEqualTo(header.getValue());
    verify(httpResponse).getFirstHeader("header1");
    verify(httpResponse).getFirstHeader("header2");
    assertThat(getContent(response.getEntityContent())).isEqualTo("response entity");
    response.close();//ww  w  .j  a  v  a2  s .  c om
    verify(httpResponse).close();
}

From source file:com.github.restdriver.serverdriver.http.RequestBodyTest.java

@Test
public void bodyAppliesItselfToRequestWhenContentTypeIncludesCharset() throws Exception {
    HttpPost request = new HttpPost();
    RequestBody body = new RequestBody("content", "text/plain;charset=UTF-8");
    body.applyTo(new ServerDriverHttpUriRequest(request));
    assertThat(IOUtils.toString(request.getEntity().getContent()), is("content"));
    assertThat(request.getEntity().getContentType().getValue(), is("text/plain; charset=UTF-8"));
    assertThat(request.getFirstHeader("Content-type").getValue(), is("text/plain;charset=UTF-8"));
}