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:com.gsma.mobileconnect.impl.RequestTokenTest.java

@Test
public void requestToken_withAllOptionsSet_shouldCreateExpectedRequest()
        throws OIDCException, DiscoveryResponseExpiredException, IOException, RestException {
    // GIVEN/*w w w  .j a va  2 s.  co  m*/
    RestClient mockedRestClient = mock(RestClient.class);
    IOIDC ioidc = Factory.getOIDC(mockedRestClient);
    CaptureRequestTokenResponse captureRequestTokenResponse = new CaptureRequestTokenResponse();
    DiscoveryResponse discoveryResponse = new DiscoveryResponse(true, new Date(Long.MAX_VALUE), 0, null,
            parseJson(OPERATOR_JSON_STRING));

    TokenOptions options = new TokenOptions();
    options.setTimeout(333);

    final CaptureHttpRequestBase captureHttpRequestBase = new CaptureHttpRequestBase();
    final RestResponse restResponse = new RestResponse(null, 0, null, "{}");

    doAnswer(new Answer() {
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            HttpRequestBase requestBase = (HttpRequestBase) args[0];
            captureHttpRequestBase.setValue(requestBase);
            return restResponse;
        }
    }).when(mockedRestClient).callRestEndPoint(any(HttpRequestBase.class), any(HttpClientContext.class),
            eq(options.getTimeout()), Matchers.<List<KeyValuePair>>any());

    String expectedRedirectURI = "expected-redirect-uri";
    String expectedCode = "expected-code";

    // WHEN
    ioidc.requestToken(discoveryResponse, expectedRedirectURI, expectedCode, options,
            captureRequestTokenResponse);

    // THEN
    HttpRequestBase request = captureHttpRequestBase.getValue();

    assertEquals(TOKEN_HREF, request.getURI().toString());
    assertEquals("POST", request.getMethod());
    assertEquals("application/x-www-form-urlencoded", request.getFirstHeader("Content-Type").getValue());
    assertEquals("application/json", request.getFirstHeader("accept").getValue());

    assertTrue(request instanceof HttpPost);

    HttpPost postRequest = (HttpPost) request;
    List<NameValuePair> contents = URLEncodedUtils.parse(postRequest.getEntity());

    assertEquals(expectedCode, findValueOfName(contents, "code"));
    assertEquals("authorization_code", findValueOfName(contents, "grant_type"));
    assertEquals(expectedRedirectURI, findValueOfName(contents, "redirect_uri"));
}

From source file:com.smartling.cms.gateway.client.CmsGatewayClientTest.java

@Test
public void postsFileContentsToUploadChannel() throws Exception {
    FileUpload response = makeFileUploadResponse("some request id", "some file uri", "some file body");

    client.send(response);/*from w  w w . jav  a2  s .  co m*/

    HttpPost httpPost = getHttpPostFromUploadChannel();
    assertThat(httpPost.getURI(), is(URI.create(STUB_UPLOAD_CHANNEL_URI + "&rid=some+request+id")));
    assertThat(IOUtils.toString(httpPost.getEntity().getContent()), is("some file body"));
}

From source file:com.twitter.hbc.SitestreamControllerTest.java

@Test
public void testAddUser() throws IOException, ControlStreamException, URISyntaxException {
    SitestreamController controlstreams = setupSimplControlStreamRequest(200, "{}");
    controlstreams.addUser("mock_stream_id", 123456789L);
    Mockito.verify(client).execute(argThat(new ArgumentValidator<HttpPost>() {
        public void validate(HttpPost post) throws Exception {
            assertEquals("application/x-www-form-urlencoded",
                    post.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue());
            assertEquals("https://host.com/1.1/site/c/mock_stream_id/add_user.json", post.getURI().toString());
            assertEquals("user_id=123456789", consumeUtf8String(post.getEntity().getContent()));
        }/*from w  w  w  . j a  va2 s.  com*/
    }));
}

From source file:com.twitter.hbc.SitestreamControllerTest.java

@Test
public void testAddUsers() throws IOException, ControlStreamException, URISyntaxException {
    SitestreamController controlstreams = setupSimplControlStreamRequest(200, "{}");
    controlstreams.addUsers("mock_stream_id", Longs.asList(1111, 2222, 3333, 4444));
    Mockito.verify(client).execute(argThat(new ArgumentValidator<HttpPost>() {
        public void validate(HttpPost post) throws Exception {
            assertEquals("application/x-www-form-urlencoded",
                    post.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue());
            assertEquals("https://host.com/1.1/site/c/mock_stream_id/add_user.json", post.getURI().toString());
            assertEquals("user_id=1111%2C2222%2C3333%2C4444", consumeUtf8String(post.getEntity().getContent()));
        }/*  w  w w.  java 2s.c om*/
    }));
}

From source file:com.twitter.hbc.SitestreamControllerTest.java

@Test
public void testRemoveUser() throws IOException, ControlStreamException, URISyntaxException {
    SitestreamController controlstreams = setupSimplControlStreamRequest(200, "{}");
    controlstreams.removeUser("mock_stream_id", 123456789L);
    Mockito.verify(client).execute(argThat(new ArgumentValidator<HttpPost>() {
        public void validate(HttpPost post) throws Exception {
            assertEquals("application/x-www-form-urlencoded",
                    post.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue());
            assertEquals("https://host.com/1.1/site/c/mock_stream_id/remove_user.json",
                    post.getURI().toString());
            assertEquals("user_id=123456789", consumeUtf8String(post.getEntity().getContent()));
        }/*w  w  w. j a v  a 2s  .  c om*/
    }));
}

From source file:com.twitter.hbc.SitestreamControllerTest.java

@Test
public void testRemoveUsers() throws IOException, ControlStreamException, URISyntaxException {
    SitestreamController controlstreams = setupSimplControlStreamRequest(200, "{}");
    controlstreams.removeUsers("mock_stream_id", Longs.asList(1111, 2222, 3333, 4444));
    Mockito.verify(client).execute(argThat(new ArgumentValidator<HttpPost>() {
        public void validate(HttpPost post) throws Exception {
            assertEquals("application/x-www-form-urlencoded",
                    post.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue());
            assertEquals("https://host.com/1.1/site/c/mock_stream_id/remove_user.json",
                    post.getURI().toString());
            assertEquals("user_id=1111%2C2222%2C3333%2C4444", consumeUtf8String(post.getEntity().getContent()));
        }// w  w w .ja v a2s .co m
    }));
}

From source file:us.the.mac.android.jni.helpers.NetworkTest.java

@Test
public void networkHttpPost() throws Exception {
    MACRequests object = createMACRequests();

    object.put("parameter", "parameterValue");
    object.setRequestType(MACRequests.HTTP_BIN);
    assertNotEquals(0, object.nPtr);//from w  w w  . ja v  a2  s. co  m

    HttpPost post = (HttpPost) object.getHttpPost();
    assertNotEquals(null, post);

    byte[] bytes = object.getBytes();
    assertNotEquals(null, bytes);
    String requestCertificate = new String(bytes);
    assertTrue(requestCertificate.contains("-----END CERTIFICATE-----"));

    String requestUrl = post.getURI().toString();
    assertEquals(TestConstants.TEST_URL, requestUrl);

    InputStream stream = post.getEntity().getContent();
    String content = new Scanner(stream).useDelimiter("\\A").next();
    assertNotEquals(null, content);
    JSONObject jsonObject = new JSONObject(content);

    String parameter = jsonObject.getString("parameter");
    assertEquals(TestConstants.TEST_PARAMETER, parameter);

    Header[] acceptHeaders = post.getHeaders("Accept");
    String acceptString = acceptHeaders[0].getValue();

    Header[] contentHeaders = post.getHeaders("Content-Type");
    String contentString = contentHeaders[0].getValue();

}

From source file:nl.nn.adapterframework.http.HttpResponseMock.java

public InputStream doPost(HttpHost host, HttpPost request, HttpContext context) throws IOException {
    assertEquals("POST", request.getMethod());
    StringBuilder response = new StringBuilder();
    String lineSeparator = System.getProperty("line.separator");
    response.append(request.toString() + lineSeparator);

    Header[] headers = request.getAllHeaders();
    for (Header header : headers) {
        response.append(header.getName() + ": " + header.getValue() + lineSeparator);
    }/*from   w  w  w  .  j  a  v a  2s  .  co  m*/
    HttpEntity entity = request.getEntity();
    if (entity instanceof MultipartEntity) {
        MultipartEntity multipartEntity = (MultipartEntity) entity;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        multipartEntity.writeTo(baos);
        String contentType = multipartEntity.getContentType().getValue();
        String boundary = getBoundary(contentType);
        contentType = contentType.replaceAll(boundary, "IGNORE");
        response.append("Content-Type: " + contentType + lineSeparator);

        response.append(lineSeparator);
        String content = new String(baos.toByteArray());
        content = content.replaceAll(boundary, "IGNORE");
        response.append(content);
    } else {
        response.append(lineSeparator);
        response.append(EntityUtils.toString(entity));
    }

    return new ByteArrayInputStream(response.toString().getBytes());
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlFileInputTest.java

/**
 * Helper that does some nasty magic./*from   w  ww . ja v  a2 s .  c o  m*/
 */
private HttpEntity post(final WebClient client, final MockWebConnection webConnection)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    final Method makeHttpMethod = HttpWebConnection.class.getDeclaredMethod("makeHttpMethod", WebRequest.class,
            HttpClientBuilder.class);
    makeHttpMethod.setAccessible(true);

    final HttpWebConnection con = new HttpWebConnection(client);

    final Method getHttpClientBuilderMethod = HttpWebConnection.class.getDeclaredMethod("getHttpClientBuilder");
    getHttpClientBuilderMethod.setAccessible(true);
    final HttpClientBuilder builder = (HttpClientBuilder) getHttpClientBuilderMethod.invoke(con);

    final HttpPost httpPost = (HttpPost) makeHttpMethod.invoke(con, webConnection.getLastWebRequest(), builder);
    final HttpEntity httpEntity = httpPost.getEntity();
    return httpEntity;
}

From source file:com.janoz.usenet.searchers.impl.NewzbinConnectorTest.java

private String getRequestContent(Capture<HttpPost> postCapture) throws IOException {
    HttpPost post = postCapture.getValue();
    String request = readStream(post.getEntity().getContent());
    return request;
}