Example usage for org.apache.http.client.methods CloseableHttpResponse getStatusLine

List of usage examples for org.apache.http.client.methods CloseableHttpResponse getStatusLine

Introduction

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

Prototype

StatusLine getStatusLine();

Source Link

Usage

From source file:com.cloud.utils.rest.BasicRestClientTest.java

@Test(expected = CloudstackRESTException.class)
public void testExecuteRequestWhenClientThrowsIOException() throws Exception {
    final BasicRestClient restClient = BasicRestClient.create().host(LOCALHOST)
            .client(HttpClientHelper.createHttpClient(5)).build();

    final CloseableHttpResponse response = restClient.execute(request);
    fail("A CloudstackRESTException should have been thrown by now." + "\nWe got " + response.getStatusLine()
            + "\nMake sure you cannot reach '" + request.getURI() + "' by method " + request.getMethod());
}

From source file:com.networknt.exception.ExceptionHandlerTest.java

@Test
public void testRuntimeException() throws Exception {
    String url = "http://localhost:8080/runtime";
    CloseableHttpClient client = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet(url);
    try {// ww w .java 2 s . c  o  m
        CloseableHttpResponse response = client.execute(httpGet);
        int statusCode = response.getStatusLine().getStatusCode();
        Assert.assertEquals(500, statusCode);
        if (statusCode == 500) {
            Status status = Config.getInstance().getMapper().readValue(response.getEntity().getContent(),
                    Status.class);
            Assert.assertNotNull(status);
            Assert.assertEquals("ERR10010", status.getCode());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.networknt.swagger.SwaggerHandlerTest.java

@Test
public void testRightRequest() throws Exception {
    String url = "http://localhost:8080/v2/pet";
    CloseableHttpClient client = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);
    try {//from  w ww . j  av a 2  s  .  c  o m
        StringEntity stringEntity = new StringEntity("post");
        httpPost.setEntity(stringEntity);
        CloseableHttpResponse response = client.execute(httpPost);
        int statusCode = response.getStatusLine().getStatusCode();
        Assert.assertEquals(200, statusCode);
        if (statusCode == 200) {
            String s = IOUtils.toString(response.getEntity().getContent(), "utf8");
            Assert.assertNotNull(s);
            Assert.assertEquals("withOperation", s);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

@Test
public void shouldDownloadArtifact() throws IOException, URISyntaxException {
    String url = "http://blah";
    FetchHandler fetchHandler = mock(FetchHandler.class);

    HttpGet mockGetMethod = mock(HttpGet.class);
    CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    when(response.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK"));
    when(httpClient.execute(mockGetMethod)).thenReturn(response);
    when(httpClientFactory.createGet(url)).thenReturn(mockGetMethod);

    when(mockGetMethod.getURI()).thenReturn(new URI(url));

    service.download(url, fetchHandler);
    verify(httpClient).execute(mockGetMethod);
    verify(fetchHandler).handle(null);/*from  w w w .j a va2  s .  c  o m*/
}

From source file:com.networknt.exception.ExceptionHandlerTest.java

@Test
public void testApiException() throws Exception {
    String url = "http://localhost:8080/api";
    CloseableHttpClient client = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet(url);
    try {//from   w  ww .  java 2  s  .  com
        CloseableHttpResponse response = client.execute(httpGet);
        Assert.assertEquals(401, response.getStatusLine().getStatusCode());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.networknt.exception.ExceptionHandlerTest.java

@Test
public void testUncaughtException() throws Exception {
    String url = "http://localhost:8080/uncaught";
    CloseableHttpClient client = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet(url);
    try {/*from   w w  w . j a  va2  s  . c  o  m*/
        CloseableHttpResponse response = client.execute(httpGet);
        Assert.assertEquals(400, response.getStatusLine().getStatusCode());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.adeptnet.maven.wagon.providers.http.HttpWagon.java

private List<String> getFileList(int wait, String destinationDirectory)
        throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException {
    if (destinationDirectory.length() > 0 && !destinationDirectory.endsWith("/")) {
        destinationDirectory += "/";
    }//from  w  ww  . j  ava  2s  . c  o m

    String url = getRepository().getUrl() + "/" + destinationDirectory;

    HttpGet getMethod = new HttpGet(url);

    try {
        CloseableHttpResponse response = execute(getMethod);
        try {
            int statusCode = response.getStatusLine().getStatusCode();

            fireTransferDebug(url + " - Status code: " + statusCode);

            switch (statusCode) {
            case HttpStatus.SC_OK:
                break;

            case HttpStatus.SC_FORBIDDEN:
                throw new AuthorizationException("Access denied to: " + url);

            case HttpStatus.SC_UNAUTHORIZED:
                throw new AuthorizationException("Not authorized.");

            case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
                throw new AuthorizationException("Not authorized by proxy.");

            case HttpStatus.SC_NOT_FOUND:
                throw new ResourceDoesNotExistException("File: " + url + " does not exist");

            case SC_TOO_MANY_REQUESTS:
                return getFileList(backoff(wait, url), destinationDirectory);

            //add more entries here
            default:
                throw new TransferFailedException(
                        "Failed to transfer file: " + url + ". Return code is: " + statusCode);
            }
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                return HtmlFileListParser.parseFileList(url, entity.getContent());
            } else {
                return Collections.emptyList();
            }

        } finally {
            response.close();
        }
    } catch (IOException e) {
        throw new TransferFailedException("Could not read response body.", e);
    } catch (HttpException e) {
        throw new TransferFailedException("Could not read response body.", e);
    } catch (InterruptedException e) {
        throw new TransferFailedException("Unable to wait for resource.", e);
    }
}

From source file:io.crate.protocols.http.CrateHttpsTransportIntegrationTest.java

@Test
public void testCheckEncryptedConnection() throws Throwable {
    CloseableHttpResponse response = post("{\"stmt\": \"select 'sslWorks'\"}");
    assertThat(response, not(nullValue()));
    assertEquals(200, response.getStatusLine().getStatusCode());
    String result = EntityUtils.toString(response.getEntity());
    assertThat(result, containsString("\"rowcount\":1"));
    assertThat(result, containsString("sslWorks"));
}

From source file:data.NeustarDatabaseUpdaterTest.java

@Test
@PrepareForTest({ NeustarDatabaseUpdater.class, GZIPInputStream.class, GPDatabaseReader.Builder.class })
public void itRetrievesRemoteFileContents() throws Exception {

    StatusLine statusLine = mock(StatusLine.class);
    when(statusLine.getStatusCode()).thenReturn(200);

    InputStream remoteInputStream = mock(InputStream.class);

    HttpEntity httpEntity = mock(HttpEntity.class);
    when(httpEntity.getContent()).thenReturn(remoteInputStream);

    CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    when(response.getStatusLine()).thenReturn(statusLine);
    when(response.getEntity()).thenReturn(httpEntity);

    HttpClient httpClient = mock(HttpClient.class);
    when(httpClient.execute(any(HttpGet.class))).thenReturn(response);

    GZIPInputStream gzipInputStream = mock(GZIPInputStream.class);
    whenNew(GZIPInputStream.class).withArguments(remoteInputStream).thenReturn(gzipInputStream);

    whenNew(GPDatabaseReader.Builder.class).withArguments(any(File.class))
            .thenReturn(mock(GPDatabaseReader.Builder.class));

    neustarDatabaseUpdater.setHttpClient(httpClient);
    neustarDatabaseUpdater.setNeustarDataUrl("http://example.com/neustardata.tgz");
    neustarDatabaseUpdater.setNeustarPollingTimeout(100);

    when(filesMover.updateCurrent(eq(neustarDatabaseDirectory), any(File.class),
            eq(neustarOldDatabaseDirectory))).thenReturn(true);
    assertThat(neustarDatabaseUpdater.update(), equalTo(true));
    verify(httpClient).close();//from   w w  w.j av a  2  s  .  c  o  m
    verify(response).close();
}