Search in sources :

Example 46 with StatusLine

use of org.apache.http.StatusLine in project nhin-d by DirectProject.

the class UnsecureServiceRequestBase_callTest method testCall_callWithNoReturnValue.

@Test
public void testCall_callWithNoReturnValue() throws Exception {
    HttpClient mockClient = mock(HttpClient.class);
    StatusLine statLine = mock(StatusLine.class);
    when(statLine.getStatusCode()).thenReturn(204);
    HttpResponse resp = mock(HttpResponse.class);
    when(resp.getStatusLine()).thenReturn(statLine);
    when(mockClient.execute((HttpUriRequest) any())).thenReturn(resp);
    MockServiceRequest req = new MockServiceRequest(mockClient, "http://service/svc", "Test");
    assertNull(req.call());
}
Also used : StatusLine(org.apache.http.StatusLine) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Example 47 with StatusLine

use of org.apache.http.StatusLine in project nhin-d by DirectProject.

the class UnsecureServiceRequestBase_callTest method testCall_nullRequest_assertServiceException.

@Test
public void testCall_nullRequest_assertServiceException() throws Exception {
    HttpClient mockClient = mock(HttpClient.class);
    StatusLine statLine = mock(StatusLine.class);
    when(statLine.getStatusCode()).thenReturn(204);
    HttpResponse resp = mock(HttpResponse.class);
    when(resp.getStatusLine()).thenReturn(statLine);
    when(mockClient.execute((HttpUriRequest) any())).thenReturn(resp);
    MockServiceRequest req = new MockServiceRequest(mockClient, "http://service/svc", "");
    boolean exceptionOccured = false;
    try {
        req.call();
    } catch (ServiceException e) {
        exceptionOccured = true;
    }
    assertTrue(exceptionOccured);
}
Also used : StatusLine(org.apache.http.StatusLine) ServiceException(org.nhindirect.common.rest.exceptions.ServiceException) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Example 48 with StatusLine

use of org.apache.http.StatusLine in project c4sg-services by Code4SocialGood.

the class StringResponseHandler method handleResponse.

@Override
public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    final StatusLine statusLine = response.getStatusLine();
    int status = statusLine.getStatusCode();
    if (status >= 200 && status < 300) {
        HttpEntity entity = response.getEntity();
        return entity != null ? EntityUtils.toString(entity) : null;
    } else {
        throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
    }
}
Also used : StatusLine(org.apache.http.StatusLine) HttpEntity(org.apache.http.HttpEntity) HttpResponseException(org.apache.http.client.HttpResponseException)

Example 49 with StatusLine

use of org.apache.http.StatusLine in project wildfly by wildfly.

the class WebSecurityCERTTestCase method makeCall.

protected void makeCall(String alias, int expectedStatusCode) throws Exception {
    try (CloseableHttpClient httpclient = getHttpsClient(alias)) {
        HttpGet httpget = new HttpGet("https://" + mgmtClient.getMgmtAddress() + ":8380/web-secure-client-cert/secured/");
        HttpResponse response = httpclient.execute(httpget);
        StatusLine statusLine = response.getStatusLine();
        assertEquals(expectedStatusCode, statusLine.getStatusCode());
    }
}
Also used : StatusLine(org.apache.http.StatusLine) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse)

Example 50 with StatusLine

use of org.apache.http.StatusLine in project wildfly by wildfly.

the class WebSecurityExternalAuthTestCase method makeCall.

protected void makeCall(String user, int expectedStatusCode) throws Exception {
    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
        HttpGet httpget = new HttpGet(url.toExternalForm() + "secured/");
        httpget.addHeader("User", user);
        HttpResponse response = httpClient.execute(httpget);
        HttpEntity entity = response.getEntity();
        StatusLine statusLine = response.getStatusLine();
        assertEquals(expectedStatusCode, statusLine.getStatusCode());
        EntityUtils.consume(entity);
    }
}
Also used : StatusLine(org.apache.http.StatusLine) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse)

Aggregations

StatusLine (org.apache.http.StatusLine)193 IOException (java.io.IOException)83 HttpResponse (org.apache.http.HttpResponse)76 HttpEntity (org.apache.http.HttpEntity)61 BasicStatusLine (org.apache.http.message.BasicStatusLine)46 ProtocolVersion (org.apache.http.ProtocolVersion)42 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)40 HttpGet (org.apache.http.client.methods.HttpGet)38 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)37 Header (org.apache.http.Header)36 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)34 Test (org.junit.Test)31 HttpPost (org.apache.http.client.methods.HttpPost)26 HashMap (java.util.HashMap)23 HttpResponseException (org.apache.http.client.HttpResponseException)23 StringEntity (org.apache.http.entity.StringEntity)23 URL (java.net.URL)20 BasicHeader (org.apache.http.message.BasicHeader)16 HttpURLConnection (java.net.HttpURLConnection)15 List (java.util.List)15