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.launchkey.sdk.transport.v1.ApacheHttpClientTransportUsersTest.java

@Test
public void testResponseStatusCodeOf300ThrowsExpectedException() throws Exception {
    expectedException.expect(CommunicationErrorException.class);
    expectedException.expectMessage("Expected Message");

    when(response.getStatusLine())/*from   w ww. ja  va  2  s .  c  o  m*/
            .thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 300, "Expected Message"));
    transport.users(new UsersRequest(null, 0L, null));
}

From source file:org.sentilo.platform.server.test.parser.OrderParserTest.java

@Test
public void parseProviderWriteResponse() throws Exception {
    final String[] parts = { "prov1" };
    when(resource.getParts()).thenReturn(parts);

    final SentiloResponse response = SentiloResponse
            .build(new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_0, 200, "")));
    parser.writeResponse(sentiloRequest, response, getProviderOrders());

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ((ByteArrayEntity) response.getHttpResponse().getEntity()).writeTo(baos);
    final String expected = "{\"sensors\":[{\"sensor\":\"sensor1\",\"orders\":[{\"order\":\"stop\",\"timestamp\":\"21/02/2013T17:49:24\",\"sender\":\"sender1\"}]},{\"sensor\":\"sensor2\",\"orders\":[{\"order\":\"stop\",\"timestamp\":\"21/02/2013T17:49:30\",\"sender\":\"sender1\"},{\"order\":\"start\",\"timestamp\":\"21/02/2013T17:49:30\",\"sender\":\"sender1\"}]}]}";
    assertEquals(expected, baos.toString());
}

From source file:com.launchkey.sdk.transport.v1.ApacheHttpClientTransportPingTest.java

@Test
public void testResponseStatusCodeOf400ReturnsBodyErrorValues() throws Exception {
    when(response.getStatusLine())//from   w ww. j a  v a  2  s . c  o  m
            .thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 400, "Status Message"));
    when(response.getEntity())
            .thenReturn(EntityBuilder.create()
                    .setStream(new ByteArrayInputStream(
                            "{\"message_code\": 60401, \"message\": \"Expected Message\"}".getBytes("UTF-8")))
                    .build());
    expectedException.expect(InvalidRequestException.class);
    expectedException.expectMessage("Expected Message");
    transport.ping(new PingRequest());
}

From source file:com.launchkey.sdk.transport.v1.ApacheHttpClientTransportLogsTest.java

@Test
public void testResponseStatusCodeOf100ThrowsExpectedException() throws Exception {
    expectedException.expect(CommunicationErrorException.class);
    expectedException.expectMessage("Expected Message");

    when(response.getStatusLine())/*from  ww  w  . j  ava 2  s. c om*/
            .thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 100, "Expected Message"));
    transport.logs(new LogsRequest("Authenticate", true, null, 0L, null, null));
}

From source file:org.apache.nifi.processors.aws.wag.TestInvokeAmazonGatewayApiMock.java

@Test
public void testSendQueryParams() throws Exception {

    HttpResponse resp = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK"));
    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(new ByteArrayInputStream("test payload".getBytes()));
    resp.setEntity(entity);// ww  w . ja  v  a 2s.  c o  m
    Mockito.doReturn(resp).when(mockSdkClient).execute(any(HttpUriRequest.class), any(HttpContext.class));

    // add dynamic property
    runner.setProperty("dynamicHeader", "yes!");
    runner.setProperty(InvokeAWSGatewayApi.PROP_QUERY_PARAMS, "apples=oranges&dogs=cats");

    // set the regex
    runner.setProperty(InvokeAWSGatewayApi.PROP_ATTRIBUTES_TO_SEND, "F.*");

    final Map<String, String> attributes = new HashMap<>();
    attributes.put(CoreAttributes.MIME_TYPE.key(), "application/plain-text");
    attributes.put("Foo", "Bar");
    runner.enqueue("Hello".getBytes("UTF-8"), attributes);
    // execute
    runner.assertValid();
    runner.run(1);

    Mockito.verify(mockSdkClient, times(1)).execute(argThat(new RequestMatcher<HttpUriRequest>(x -> {
        return x.getMethod().equals("GET") && x.getFirstHeader("x-api-key").getValue().equals("abcd")
                && x.getFirstHeader("Authorization").getValue().startsWith("AWS4")
                && x.getFirstHeader("dynamicHeader").getValue().equals("yes!")
                && x.getFirstHeader("Foo").getValue().equals("Bar") && x.getURI().toString().equals(
                        "https://foobar.execute-api.us-east-1.amazonaws.com/TEST?dogs=cats&apples=oranges");
    })), any(HttpContext.class));
    // check
    runner.assertTransferCount(InvokeAWSGatewayApi.REL_SUCCESS_REQ, 1);
    runner.assertTransferCount(InvokeAWSGatewayApi.REL_RESPONSE, 1);
    runner.assertTransferCount(InvokeAWSGatewayApi.REL_RETRY, 0);
    runner.assertTransferCount(InvokeAWSGatewayApi.REL_NO_RETRY, 0);
    runner.assertTransferCount(InvokeAWSGatewayApi.REL_FAILURE, 0);

    final List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(InvokeAWSGatewayApi.REL_RESPONSE);
    final MockFlowFile ff0 = flowFiles.get(0);

    ff0.assertAttributeEquals(InvokeAWSGatewayApi.STATUS_CODE, "200");
    ff0.assertContentEquals("test payload");
    ff0.assertAttributeExists(InvokeAWSGatewayApi.TRANSACTION_ID);
    ff0.assertAttributeEquals(InvokeAWSGatewayApi.RESOURCE_NAME_ATTR, "/TEST");
}

From source file:com.seleritycorp.context.RequestResponseHandlerTest.java

private HttpResponse mockResponse(int statusCode, String content, String contentType) throws Exception {
    ProtocolVersion proto = new ProtocolVersion("protoFoo", 1, 2);
    StatusLine statusLine = new BasicStatusLine(proto, statusCode, "reasonFoo");

    byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8);
    InputStream contentStream = new ByteArrayInputStream(contentBytes);

    Header contentTypeHeader = new BasicHeader("Content-Type", contentType);

    HttpEntity httpEntity = createMock(HttpEntity.class);
    expect(httpEntity.getContent()).andReturn(contentStream);
    expect(httpEntity.getContentLength()).andReturn((long) content.length()).anyTimes();
    expect(httpEntity.getContentType()).andReturn(contentTypeHeader).anyTimes();

    HttpResponse response = createMock(HttpResponse.class);
    expect(response.getStatusLine()).andReturn(statusLine);
    expect(response.getEntity()).andReturn(httpEntity);

    return response;
}

From source file:org.sentilo.platform.server.test.parser.DataParserTest.java

@Test
public void parseSensorWriteResponse() throws Exception {
    final String[] parts = { "prov1", "sensor1" };
    when(resource.getParts()).thenReturn(parts);

    final SentiloResponse response = SentiloResponse
            .build(new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_0, 200, "")));
    parser.writeResponse(sentiloRequest, response, getObservationsFromSensor());

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ((ByteArrayEntity) response.getHttpResponse().getEntity()).writeTo(baos);
    final String expected = "{\"observations\":[{\"value\":\"1\",\"timestamp\":\"21/02/2013T17:49:24\"},{\"value\":\"10\",\"timestamp\":\"21/02/2013T17:49:30\"}]}";
    assertEquals(expected, baos.toString());
}

From source file:com.oplay.nohelper.volley.toolbox.HurlStack.java

@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws IOException, AuthFailureError, NetworkError {
    if (request.getHasHttpResponse()) {
        throw new NetworkError("Slow Network leads to more HttpResponse");
    }//from  ww w . j  a v a2  s  .c om
    String url = request.getUrl();
    request.addMarker(url);
    HashMap<String, String> map = new HashMap<String, String>();
    map.putAll(request.getHeaders());
    map.putAll(additionalHeaders);
    if (mUrlRewriter != null) {
        String rewritten = mUrlRewriter.rewriteUrl(url);
        if (rewritten == null) {
            throw new IOException("URL blocked by rewriter: " + url);
        }
        url = rewritten;
    }
    URL parsedUrl = new URL(url);
    HttpURLConnection connection = openConnection(parsedUrl, request);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    setConnectionParametersForRequest(connection, request);
    // Initialize HttpResponse with data from the HttpURLConnection.
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    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(protocolVersion, connection.getResponseCode(),
            connection.getResponseMessage());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromConnection(connection));
    request.addMarker("network-http-complete");
    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.joyent.manta.client.multipart.ServerSideMultipartManagerTest.java

public void canAbortMpu() throws IOException {
    StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_NO_CONTENT, "NO_CONTENT");
    ServerSideMultipartManager manager = buildMockManager(statusLine, null);

    UUID id = new UUID(0L, 0L);
    String partsDirectory = manager.uuidPrefixedPath(id);
    ServerSideMultipartUpload upload = new ServerSideMultipartUpload(id, null, partsDirectory);
    manager.abort(upload);/*from  ww w  . j  av  a  2s.  c o  m*/
}

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

private void prepareLoginFailureResponse() throws IOException, ClientProtocolException {
    statusLine = new BasicStatusLine(new ProtocolVersion("https", 1, 1), HttpStatus.SC_UNAUTHORIZED,
            "Unauthorized");
    final HttpResponse response = new BasicHttpResponse(statusLine);
    response.setHeader("X-GDC-Request", REQUEST_ID);
    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(new ByteArrayInputStream(FAILURE_REASON.getBytes()));
    response.setEntity(entity);/* w  ww . java  2  s .  c  om*/
    when(httpClient.execute(any(HttpHost.class), any(HttpPost.class))).thenReturn(response);
    sstStrategy.setLogger(logger);
}