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

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

Introduction

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

Prototype

public HttpEntity getEntity() 

Source Link

Usage

From source file:io.cloudslang.content.httpclient.build.RequestBuilderTest.java

@Test
public void testEntity() throws URISyntaxException, IOException {
    HttpEntityEnclosingRequestBase httpRequestBase = (HttpEntityEnclosingRequestBase) new RequestBuilder()
            .setUri(new URI("https://localhost:443/lalal?dd=3")).setMethod("POST")
            .setEntity(new StringEntity("my custom entity")).build();
    assertEquals(IOUtils.toString(httpRequestBase.getEntity().getContent()), "my custom entity");
}

From source file:com.yoelnunez.mobilefirst.analytics.AnalyticsAPITest.java

@Test
public void testSendingLogs() throws MissingServerContextException {
    // reset http client
    AnalyticsAPI.setHttpClient(new HttpClientMock() {
        @Override/*from  w ww  .j a va2s  . com*/
        public HttpResponse execute(HttpUriRequest request) throws IOException {

            HttpEntityEnclosingRequestBase requestBase = (HttpEntityEnclosingRequestBase) request;

            InputStream requestInputStream = requestBase.getEntity().getContent();

            BufferedReader reader = new BufferedReader(new InputStreamReader(requestInputStream));
            StringBuilder body = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                body.append(line);
            }
            reader.close();

            JSONArray requestBody = new JSONArray(body.toString());

            Assert.assertEquals("should contain only one element", requestBody.length(), 1);

            JSONObject logPayload = requestBody.getJSONObject(0);

            Assert.assertEquals("should have log type \"RawMfpAppLogs\"", logPayload.get("_type"),
                    "RawMfpAppLogs");

            Assert.assertTrue("should contain key \"worklight_data\"", logPayload.has("worklight_data"));

            Assert.assertEquals("should contain one log", logPayload.getJSONArray("client_logs").length(), 1);

            return null;
        }
    });

    AnalyticsAPI.setContext(serverContext);
    AnalyticsAPI api = AnalyticsAPI.createInstance(appContext);

    JSONObject log = new JSONObject();
    log.put("myCustomKey", "helloValueForKey");

    api.log("hello-world", log);

    AnalyticsAPI.send();
}

From source file:org.openscore.content.httpclient.build.RequestBuilderTest.java

@Test
public void testEntity() throws URISyntaxException, IOException {
    HttpEntityEnclosingRequestBase httpRequestBase = (HttpEntityEnclosingRequestBase) new org.openscore.content.httpclient.build.RequestBuilder()
            .setUri(new URI("https://localhost:443/lalal?dd=3")).setMethod("POST")
            .setEntity(new StringEntity("my custom entity")).build();
    assertEquals(IOUtils.toString(httpRequestBase.getEntity().getContent()), "my custom entity");
}

From source file:com.polydeucesys.eslogging.testutils.MockClosableHttpAsyncClient.java

public String getLastRequestContent() {
    String retval = "";
    if (savedRequest instanceof HttpEntityEnclosingRequestBase) {
        HttpEntityEnclosingRequestBase lastPost = (HttpEntityEnclosingRequestBase) savedRequest;
        try {/*from   ww w. ja  va 2s .c  om*/
            retval = EntityUtils.toString(lastPost.getEntity(), StandardCharsets.UTF_8);
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        HttpRequestBase rb = (HttpRequestBase) savedRequest;
        retval = rb.getURI().toASCIIString();
    }
    return retval;
}

From source file:com.amazonaws.http.AmazonHttpClientTest.java

private void mockFailure(final int contentLength) throws IOException {

    EasyMock.reset(httpClient);// w  ww .j  a  va 2 s . c  om

    EasyMock.expect(httpClient.getConnectionManager()).andReturn(null).anyTimes();

    for (int i = 0; i < 4; ++i) {
        EasyMock.expect(
                httpClient.execute(EasyMock.<HttpUriRequest>anyObject(), EasyMock.<HttpContext>anyObject()))
                .andAnswer(new IAnswer<org.apache.http.HttpResponse>() {

                    @Override
                    public org.apache.http.HttpResponse answer() throws Throwable {

                        HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) EasyMock
                                .getCurrentArguments()[0];

                        InputStream stream = request.getEntity().getContent();
                        int len = 0;
                        while (true) {
                            int b = stream.read(new byte[1024]);
                            if (b == -1) {
                                break;
                            }
                            len += b;
                        }

                        Assert.assertEquals(contentLength, len);

                        throw new IOException("BOOM");
                    }
                });
    }

    EasyMock.replay(httpClient);
}

From source file:com.ksc.http.KscHttpClientTest.java

@SuppressWarnings("deprecation")
private void mockFailure(final int contentLength) throws IOException {

    EasyMock.reset(httpClient);//  w  w  w  . j a  v  a 2s  .  c o  m

    EasyMock.expect(httpClient.getConnectionManager()).andReturn(null).anyTimes();

    for (int i = 0; i < 4; ++i) {
        EasyMock.expect(
                httpClient.execute(EasyMock.<HttpUriRequest>anyObject(), EasyMock.<HttpContext>anyObject()))
                .andAnswer(new IAnswer<org.apache.http.HttpResponse>() {

                    @Override
                    public org.apache.http.HttpResponse answer() throws Throwable {

                        HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) EasyMock
                                .getCurrentArguments()[0];

                        InputStream stream = request.getEntity().getContent();
                        int len = 0;
                        while (true) {
                            int b = stream.read(new byte[1024]);
                            if (b == -1) {
                                break;
                            }
                            len += b;
                        }

                        Assert.assertEquals(contentLength, len);

                        throw new IOException("BOOM");
                    }
                });
    }

    EasyMock.replay(httpClient);
}

From source file:com.seleritycorp.common.base.http.client.HttpRequestTest.java

@Test
public void testAddDataSingle() throws Exception {
    replayAll();/*from   w ww .j ava 2s.  c  o  m*/

    HttpRequest request = createHttpRequest("foo");
    HttpRequest requestAfterSetting1 = request.setMethodPost();
    HttpRequest requestAfterSetting2 = request.addData("foo=bar%");
    HttpResponse response = request.execute();

    verifyAll();

    assertThat(request).isSameAs(requestAfterSetting1);
    assertThat(request).isSameAs(requestAfterSetting2);
    assertThat(response).isEqualTo(httpResponse);

    HttpUriRequest backendRequestRaw = backendRequestCapture.getValue();
    assertThat(backendRequestRaw).isInstanceOf(HttpEntityEnclosingRequestBase.class);
    HttpEntityEnclosingRequestBase backendRequest = (HttpEntityEnclosingRequestBase) backendRequestRaw;
    assertThat(backendRequest.getMethod()).isEqualTo("POST");
    assertThat(backendRequest.getURI().toString()).isEqualTo("foo");
    HttpEntity entity = backendRequest.getEntity();
    assertThat(entity.getContentType().getValue()).isEqualTo("text/plain; charset=UTF-8");
    assertThat(IOUtils.toString(entity.getContent(), StandardCharsets.UTF_8)).isEqualTo("foo=bar%");
}

From source file:com.seleritycorp.common.base.http.client.HttpRequestTest.java

@Test
public void testAddDataSingleWithContentType() throws Exception {
    replayAll();//from  w w w .ja  va2s .  c om

    HttpRequest request = createHttpRequest("foo");
    HttpRequest requestAfterSetting1 = request.setMethodPost();
    HttpRequest requestAfterSetting2 = request.setContentType(ContentType.APPLICATION_JSON);
    HttpRequest requestAfterSetting3 = request.addData("foo=bar%");
    HttpResponse response = request.execute();

    verifyAll();

    assertThat(request).isSameAs(requestAfterSetting1);
    assertThat(request).isSameAs(requestAfterSetting2);
    assertThat(request).isSameAs(requestAfterSetting3);
    assertThat(response).isEqualTo(httpResponse);

    HttpUriRequest backendRequestRaw = backendRequestCapture.getValue();
    assertThat(backendRequestRaw).isInstanceOf(HttpEntityEnclosingRequestBase.class);
    HttpEntityEnclosingRequestBase backendRequest = (HttpEntityEnclosingRequestBase) backendRequestRaw;
    assertThat(backendRequest.getMethod()).isEqualTo("POST");
    assertThat(backendRequest.getURI().toString()).isEqualTo("foo");
    HttpEntity entity = backendRequest.getEntity();
    assertThat(entity.getContentType().getValue()).isEqualTo("application/json; charset=UTF-8");
    assertThat(IOUtils.toString(entity.getContent(), StandardCharsets.UTF_8)).isEqualTo("foo=bar%");
}

From source file:com.seleritycorp.common.base.http.client.HttpRequestTest.java

@Test
public void testAddDataAppending() throws Exception {
    replayAll();/*from  ww w .  j  ava2s.c  o m*/

    HttpRequest request = createHttpRequest("foo");
    HttpRequest requestAfterSetting1 = request.setMethodPost();
    HttpRequest requestAfterSetting2 = request.addData("foo=bar%");
    HttpRequest requestAfterSetting3 = request.addData("baz&quux");
    HttpResponse response = request.execute();

    verifyAll();

    assertThat(request).isSameAs(requestAfterSetting1);
    assertThat(request).isSameAs(requestAfterSetting2);
    assertThat(request).isSameAs(requestAfterSetting3);
    assertThat(response).isEqualTo(httpResponse);

    HttpUriRequest backendRequestRaw = backendRequestCapture.getValue();
    assertThat(backendRequestRaw).isInstanceOf(HttpEntityEnclosingRequestBase.class);
    HttpEntityEnclosingRequestBase backendRequest = (HttpEntityEnclosingRequestBase) backendRequestRaw;
    assertThat(backendRequest.getMethod()).isEqualTo("POST");
    assertThat(backendRequest.getURI().toString()).isEqualTo("foo");
    HttpEntity entity = backendRequest.getEntity();
    assertThat(entity.getContentType().getValue()).isEqualTo("text/plain; charset=UTF-8");
    assertThat(IOUtils.toString(entity.getContent(), StandardCharsets.UTF_8)).isEqualTo("foo=bar%&baz&quux");
}