Example usage for org.apache.http.message BasicStatusLine BasicStatusLine

List of usage examples for org.apache.http.message BasicStatusLine BasicStatusLine

Introduction

In this page you can find the example usage for org.apache.http.message BasicStatusLine BasicStatusLine.

Prototype

public BasicStatusLine(ProtocolVersion protocolVersion, int i, String str) 

Source Link

Usage

From source file:org.finra.dm.tools.common.databridge.DataBridgeWebClientTest.java

@Test
public void testGetBusinessObjectData200BadContentReturnsNull() throws Exception {
    CloseableHttpResponse httpResponse = new MockCloseableHttpResponse(
            new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "testReasonPhrase"));
    httpResponse.setEntity(new StringEntity("invalid xml"));
    String actionDescription = "testActionDescription";
    BusinessObjectData businessObjectData = dataBridgeWebClient.getBusinessObjectData(httpResponse,
            actionDescription);/*from w  w  w .  j av a 2 s .  co  m*/
    Assert.assertNull("businessObjectData", businessObjectData);
}

From source file:org.elasticsearch.client.RestClientMultipleHostsTests.java

@Before
@SuppressWarnings("unchecked")
public void createRestClient() throws IOException {
    CloseableHttpAsyncClient httpClient = mock(CloseableHttpAsyncClient.class);
    when(httpClient.<HttpResponse>execute(any(HttpAsyncRequestProducer.class),
            any(HttpAsyncResponseConsumer.class), any(HttpClientContext.class), any(FutureCallback.class)))
                    .thenAnswer(new Answer<Future<HttpResponse>>() {
                        @Override
                        public Future<HttpResponse> answer(InvocationOnMock invocationOnMock) throws Throwable {
                            HttpAsyncRequestProducer requestProducer = (HttpAsyncRequestProducer) invocationOnMock
                                    .getArguments()[0];
                            HttpUriRequest request = (HttpUriRequest) requestProducer.generateRequest();
                            HttpHost httpHost = requestProducer.getTarget();
                            HttpClientContext context = (HttpClientContext) invocationOnMock.getArguments()[2];
                            assertThat(context.getAuthCache().get(httpHost), instanceOf(BasicScheme.class));
                            FutureCallback<HttpResponse> futureCallback = (FutureCallback<HttpResponse>) invocationOnMock
                                    .getArguments()[3];
                            //return the desired status code or exception depending on the path
                            if (request.getURI().getPath().equals("/soe")) {
                                futureCallback.failed(new SocketTimeoutException(httpHost.toString()));
                            } else if (request.getURI().getPath().equals("/coe")) {
                                futureCallback.failed(new ConnectTimeoutException(httpHost.toString()));
                            } else if (request.getURI().getPath().equals("/ioe")) {
                                futureCallback.failed(new IOException(httpHost.toString()));
                            } else {
                                int statusCode = Integer.parseInt(request.getURI().getPath().substring(1));
                                StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("http", 1, 1),
                                        statusCode, "");
                                futureCallback.completed(new BasicHttpResponse(statusLine));
                            }//  w w w . j  a v a2 s  .c  o  m
                            return null;
                        }
                    });
    int numHosts = RandomNumbers.randomIntBetween(getRandom(), 2, 5);
    httpHosts = new HttpHost[numHosts];
    for (int i = 0; i < numHosts; i++) {
        httpHosts[i] = new HttpHost("localhost", 9200 + i);
    }
    failureListener = new HostsTrackingFailureListener();
    restClient = new RestClient(httpClient, 10000, new Header[0], httpHosts, null, failureListener);
}

From source file:org.piraso.client.net.HttpPirasoEntryReaderTest.java

@Test(expected = HttpPirasoException.class)
public void testStartInvalidContentType() throws Exception {
    Preferences preferences = new Preferences();

    StatusLine line = new BasicStatusLine(new ProtocolVersion("http", 1, 0), HttpStatus.SC_OK, "");
    doReturn(line).when(response).getStatusLine();
    doReturn("json/application").when(contentTypeHeader).getValue();

    reader.getStartHandler().setPreferences(preferences);

    reader.start();/*from  www  .j  ava2 s .c  o m*/
}

From source file:com.parse.ParseApacheHttpClientTest.java

@Test
public void testGetParseResponse() throws IOException {
    int statusCode = 200;
    String reasonPhrase = "test reason";
    ProtocolVersion protocol = new ProtocolVersion("HTTP", 1, 1);
    BasicStatusLine line = new BasicStatusLine(protocol, statusCode, reasonPhrase);
    BasicHttpResponse apacheResponse = new BasicHttpResponse(line);
    String content = "content";
    StringEntity entity = new StringEntity(content);
    apacheResponse.setEntity(entity);//from  w  w  w  .  j ava2 s. co  m
    apacheResponse.setHeader("Content-Length", String.valueOf(entity.getContentLength()));

    ParseApacheHttpClient parseClient = new ParseApacheHttpClient(10000, null);
    ParseHttpResponse parseResponse = parseClient.getResponse(apacheResponse);

    // Verify status code
    assertEquals(statusCode, parseResponse.getStatusCode());
    // Verify reason phrase
    assertEquals(reasonPhrase, parseResponse.getReasonPhrase());
    // Verify content length
    assertEquals(7, parseResponse.getTotalSize());
    // Verify content
    // Can not read apacheResponse entity to compare since it has been read during
    // creating parseResponse
    assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(parseResponse.getContent()));
}

From source file:com.example.chengcheng.network.httpstacks.HttpUrlConnStack.java

private Response fetchResponse(HttpURLConnection connection) throws IOException {

    // Initialize HttpResponse with data from the HttpURLConnection.
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }//from   ww w  . j a  v a  2  s  . c o  m
    // ??
    StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(),
            connection.getResponseMessage());
    // response
    Response response = new Response(responseStatus);
    // response?
    response.setEntity(entityFromURLConnwction(connection));
    addHeadersToResponse(response, connection);
    return response;
}

From source file:com.thoughtworks.go.util.HttpServiceTest.java

@Test
public void shouldSetTheAcceptHeaderWhilePostingProperties() throws Exception {
    HttpPost post = mock(HttpPost.class);
    String url = "http://url";
    when(httpClientFactory.createPost(url)).thenReturn(post);
    when(post.getURI()).thenReturn(new URI(url));
    CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    when(response.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK"));
    when(httpClient.execute(post)).thenReturn(response);

    ArgumentCaptor<UrlEncodedFormEntity> entityCaptor = ArgumentCaptor.forClass(UrlEncodedFormEntity.class);

    service.postProperty(url, "value");

    verify(post).setHeader("Confirm", "true");
    verify(post).setEntity(entityCaptor.capture());

    UrlEncodedFormEntity expected = new UrlEncodedFormEntity(
            Arrays.asList(new BasicNameValuePair("value", "value")));

    UrlEncodedFormEntity actual = entityCaptor.getValue();

    assertEquals(IOUtils.toString(expected.getContent()), IOUtils.toString(actual.getContent()));
    assertEquals(expected.getContentLength(), expected.getContentLength());
    assertEquals(expected.getContentType(), expected.getContentType());
    assertEquals(expected.getContentEncoding(), expected.getContentEncoding());
    assertEquals(expected.isChunked(), expected.isChunked());
}

From source file:org.eluder.coveralls.maven.plugin.httpclient.CoverallsClientTest.java

@Test(expected = IOException.class)
public void testParseFailingEntity() throws Exception {
    StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK");
    when(httpClientMock.execute(any(HttpUriRequest.class))).thenReturn(httpResponseMock);
    when(httpResponseMock.getStatusLine()).thenReturn(statusLine);
    when(httpResponseMock.getEntity()).thenReturn(httpEntityMock);
    when(httpEntityMock.getContent()).thenThrow(IOException.class);
    CoverallsClient client = new CoverallsClient("http://test.com/coveralls", httpClientMock,
            new ObjectMapper());
    client.submit(file);/*from w w  w.jav  a  2 s  .com*/
}

From source file:com.gooddata.http.client.LoginSSTRetrievalStrategyTest.java

@Test
public void shouldThrowOnLogoutError() throws Exception {
    statusLine = new BasicStatusLine(new ProtocolVersion("https", 1, 1), HttpStatus.SC_SERVICE_UNAVAILABLE,
            "downtime");
    final HttpResponse response = new BasicHttpResponse(statusLine);
    when(httpClient.execute(isA(HttpHost.class), isA(HttpDelete.class))).thenReturn(response);

    expectedException.expect(new GoodDataLogoutExceptionMatcher(503, "downtime"));

    sstStrategy.logout(httpClient, host, "/gdc/account/login/profileid", SST, TT);
}

From source file:mobi.jenkinsci.server.core.services.HttpClientURLDownloaderTest.java

private BasicHttpResponse newHttpResponse(final int httpStatus) {
    final BasicHttpResponse httpResponse = new BasicHttpResponse(
            new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), httpStatus, ""));
    return httpResponse;
}

From source file:org.finra.herd.tools.common.databridge.DataBridgeWebClientTest.java

@Test
public void testGetBusinessObjectDataStorageFilesCreateResponse200ValidResponse() throws Exception {
    CloseableHttpResponse httpResponse = new MockCloseableHttpResponse(
            new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "testReasonPhrase"), false);
    httpResponse.setEntity(/*  w  ww  .  j  ava  2 s. c o  m*/
            new StringEntity(xmlHelper.objectToXml(new BusinessObjectDataStorageFilesCreateResponse())));
    BusinessObjectDataStorageFilesCreateResponse businessObjectDataStorageFilesCreateResponse = dataBridgeWebClient
            .getBusinessObjectDataStorageFilesCreateResponse(httpResponse);
    assertNotNull("businessObjectDataStorageFilesCreateResponse", businessObjectDataStorageFilesCreateResponse);
}