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:com.vsct.dt.strowgr.admin.repository.consul.ConsulReaderTest.java

@Test
public void should_throw_exception_with_no_entity_when_status_out_of_range_200_299() {
    for (int status = 100; status < 600; status++) {
        if (status >= 200 && status < 300)
            continue; // skip
        // given/*from   w ww .  ja  v a2 s. c  o  m*/
        HttpResponse httpResponse = mock(HttpResponse.class);
        when(httpResponse.getStatusLine())
                .thenReturn(new BasicStatusLine(new ProtocolVersion("http1.1", 1, 1), status, ""));

        // test
        try {
            new ConsulReader(null).parseHttpResponse(httpResponse, this::getHttpEntity);
            // check
            fail("can't reach this point for status " + status);
        } catch (ClientProtocolException e) {
            // check
            assertThat(e.getMessage()).contains(String.valueOf(status));
            assertThat(e.getMessage()).contains("no content");
        }
    }
}

From source file:com.android.volley.stack.HurlStack.java

@Override
public HttpResponse performRequest(Request<?> request) throws IOException, AuthFailureError {
    HashMap<String, String> map = new HashMap<String, String>();
    if (!TextUtils.isEmpty(mUserAgent)) {
        map.put(HTTP.USER_AGENT, mUserAgent);
    }/*from   w  w w  . jav  a 2  s  . com*/
    map.putAll(request.getHeaders());

    URL parsedUrl = new URL(request.getUrl());
    HttpURLConnection connection = openConnection(parsedUrl, request);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }

    setConnectionParametersForRequest(connection, request);

    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // -1 is returned by getResponseCode() if the response code could not be retrieved.
        // Signal to the caller that something was wrong with the connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }

    StatusLine responseStatus = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1),
            connection.getResponseCode(), connection.getResponseMessage());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromConnection(connection));

    for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
        if (header.getKey() != null) {
            Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
            response.addHeader(h);
        }
    }

    return response;
}

From source file:com.solution.backend.LoginServiceTest.java

private HttpResponse makeHttpResponse(int status, String body) {
    HttpResponse response = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, status, null));
    BasicHttpEntity e = new BasicHttpEntity();
    e.setContent(new ByteArrayInputStream(body.getBytes()));
    response.setEntity(e);//ww w.ja  v a  2  s. c  o  m
    return response;
}

From source file:net.sf.jsog.client.DefaultJsogClientImplTest.java

private StatusLine createStatusLine(int code, String reason) {
    ProtocolVersion version = new ProtocolVersion("HTTP", 1, 1);
    return new BasicStatusLine(version, code, reason);
}

From source file:com.vincestyling.netroid.stack.HurlStack.java

@Override
public HttpResponse performRequest(Request<?> request) throws IOException, AuthFailureError {
    HashMap<String, String> map = new HashMap<String, String>();
    if (!TextUtils.isEmpty(mUserAgent)) {
        map.put(HTTP.USER_AGENT, mUserAgent);
    }//from w  ww  .  jav a  2 s . c  o m
    map.putAll(request.getHeaders());

    URL parsedUrl = new URL(request.getUrl());
    HttpURLConnection connection = openConnection(parsedUrl, request);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }

    setConnectionParametersForRequest(connection, request);

    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // -1 is returned by getResponseCode() if the response code could not be retrieved.
        // Signal to the caller that something was wrong with the connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }

    StatusLine responseStatus = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1),
            connection.getResponseCode(), connection.getResponseMessage());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) {
        response.setEntity(entityFromConnection(connection));
    }

    for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
        if (header.getKey() != null) {
            Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
            response.addHeader(h);
        }
    }

    return response;
}

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

@Test
public void shouldPostArtifactsAlongWithMD5() throws IOException, URISyntaxException {
    File uploadingFile = mock(File.class);
    java.util.Properties checksums = new java.util.Properties();

    String uploadUrl = "http://url";

    HttpPost mockPostMethod = mock(HttpPost.class);
    CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    when(response.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK"));
    when(httpClient.execute(mockPostMethod)).thenReturn(response);

    when(uploadingFile.exists()).thenReturn(true);
    when(httpClientFactory.createPost(uploadUrl)).thenReturn(mockPostMethod);
    when(mockPostMethod.getURI()).thenReturn(new URI(uploadUrl));

    service.upload(uploadUrl, 100L, uploadingFile, checksums);

    verify(mockPostMethod).setHeader(GO_ARTIFACT_PAYLOAD_SIZE, "100");
    verify(mockPostMethod).setHeader("Confirm", "true");
    verify(mockPostMethod).setHeader("X-Agent-GUID", "some-guid");
    verify(mockPostMethod).setHeader("Authorization", "some-token");
    verify(httpClientFactory).createMultipartRequestEntity(uploadingFile, checksums);
    verify(httpClient).execute(mockPostMethod);
}

From source file:co.cask.cdap.client.rest.RestClientTest.java

@Test
public void testUnauthorizedResponseCodeAnalysis() {

    StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_UNAUTHORIZED,
            "Unauthorized");
    when(response.getStatusLine()).thenReturn(statusLine);
    TestUtils.verifyResponse(HttpStatus.SC_UNAUTHORIZED, response);
    verify(response).getStatusLine();//from  ww w . ja  va  2  s . co m
}

From source file:com.nexmo.client.voice.endpoints.ListCallsMethodTest.java

@Test
public void parseResponse() throws Exception {
    HttpResponse stubResponse = new BasicHttpResponse(
            new BasicStatusLine(new ProtocolVersion("1.1", 1, 1), 200, "OK"));

    String json = "{\n" + "  \"page_size\": 10,\n" + "  \"record_index\": 0,\n" + "  \"count\": 2,\n"
            + "  \"_embedded\": {\n" + "    \"calls\": [\n" + "      {\n"
            + "        \"uuid\": \"93137ee3-580e-45f7-a61a-e0b5716000ef\",\n"
            + "        \"status\": \"completed\",\n" + "        \"direction\": \"outbound\",\n"
            + "        \"rate\": \"0.02400000\",\n" + "        \"price\": \"0.00280000\",\n"
            + "        \"duration\": \"7\",\n" + "        \"network\": \"23410\",\n"
            + "        \"conversation_uuid\": \"aa17bd11-c895-4225-840d-30dc38c31e50\",\n"
            + "        \"start_time\": \"2017-01-13T13:55:02.000Z\",\n"
            + "        \"end_time\": \"2017-01-13T13:55:09.000Z\",\n" + "        \"to\": {\n"
            + "          \"type\": \"phone\",\n" + "          \"number\": \"447700900104\"\n" + "        },\n"
            + "        \"from\": {\n" + "          \"type\": \"phone\",\n"
            + "          \"number\": \"447700900105\"\n" + "        },\n" + "        \"_links\": {\n"
            + "          \"self\": {\n"
            + "            \"href\": \"/v1/calls/93137ee3-580e-45f7-a61a-e0b5716000ef\"\n" + "          }\n"
            + "        }\n" + "      },\n" + "      {\n"
            + "        \"uuid\": \"105e02df-940a-466c-b28b-51ae015a9166\",\n"
            + "        \"status\": \"completed\",\n" + "        \"direction\": \"outbound\",\n"
            + "        \"rate\": \"0.02400000\",\n" + "        \"price\": \"0.00280000\",\n"
            + "        \"duration\": \"7\",\n" + "        \"network\": \"23410\",\n"
            + "        \"conversation_uuid\": \"1467b438-f5a8-4937-9a65-e1f946a2f664\",\n"
            + "        \"start_time\": \"2017-01-11T15:03:46.000Z\",\n"
            + "        \"end_time\": \"2017-01-11T15:03:53.000Z\",\n" + "        \"to\": {\n"
            + "          \"type\": \"phone\",\n" + "          \"number\": \"447700900104\"\n" + "        },\n"
            + "        \"from\": {\n" + "          \"type\": \"phone\",\n"
            + "          \"number\": \"447700900105\"\n" + "        },\n" + "        \"_links\": {\n"
            + "          \"self\": {\n"
            + "            \"href\": \"/v1/calls/105e02df-940a-466c-b28b-51ae015a9166\"\n" + "          }\n"
            + "        }\n" + "      }\n" + "    ]\n" + "  },\n" + "  \"_links\": {\n" + "    \"self\": {\n"
            + "      \"href\": \"/v1/calls?page_size=10&record_index=0\"\n" + "    },\n" + "    \"first\": {\n"
            + "      \"href\": \"/v1/calls?page_size=10\"\n" + "    },\n" + "    \"last\": {\n"
            + "      \"href\": \"/v1/calls?page_size=10\"\n" + "    }\n" + "  }\n" + "}\n";
    InputStream jsonStream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(jsonStream);/*from w  ww.jav  a 2  s . c  o  m*/
    stubResponse.setEntity(entity);

    CallInfoPage page = method.parseResponse(stubResponse);
    assertEquals(2, page.getCount());
    assertEquals(2, page.getEmbedded().getCallInfos().length);
    assertEquals("/v1/calls?page_size=10", page.getLinks().getFirst().getHref());
    assertEquals("/v1/calls?page_size=10", page.getLinks().getLast().getHref());
}

From source file:no.digipost.api.useragreements.client.response.ResponseUtilsTest.java

private static HttpResponse tooManyRequestsResponseWithRetryAfter(String retryAfterValue) {
    HttpResponse tooManyRequestsErrorResponse = new BasicHttpResponse(
            new BasicStatusLine(new ProtocolVersion("http", 1, 1), 429, "Too Many Requests"));
    tooManyRequestsErrorResponse.addHeader(Headers.Retry_After, retryAfterValue);
    return tooManyRequestsErrorResponse;
}