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.sentilo.platform.server.test.parser.OrderParserTest.java

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

    final List<Order> ordersList = Collections.emptyList();
    final SentiloResponse response = SentiloResponse
            .build(new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_0, 200, "")));
    parser.writeResponse(sentiloRequest, response, ordersList);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ((ByteArrayEntity) response.getHttpResponse().getEntity()).writeTo(baos);
    final String expected = "{\"sensors\":[]}";
    assertEquals(expected, baos.toString());
}

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

@Test
public void testResponseStatusCodeOf400ReturnsBodyErrorValues() throws Exception {
    when(response.getStatusLine())//from  w w  w .ja  va2 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\": 70401, \"message\": \"Expected Message\"}".getBytes("UTF-8")))
                    .build());
    expectedException.expect(InvalidRequestException.class);
    expectedException.expectMessage("Expected Message");
    transport.poll(new PollRequest(null, 0L, null, null));
}

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

@Test
public void testResponseStatusCodeOf400ReturnsBodyErrorValues() throws Exception {
    when(response.getStatusLine())//  www.ja v  a 2s. 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\": 70401, \"message\": \"Expected Message\"}".getBytes("UTF-8")))
                    .build());
    expectedException.expect(InvalidRequestException.class);
    expectedException.expectMessage("Expected Message");
    transport.users(new UsersRequest(null, 0L, null));
}

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

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

    when(response.getStatusLine())//from  w w w  .ja  va2  s  . c o  m
            .thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 300, "Expected Message"));
    transport.auths(new AuthsRequest(null, 0L, null, null, 0, 0));
}

From source file:com.telefonica.iot.cygnus.backends.http.HttpBackendTest.java

/**
 * [HttpBackend.createJsonResponse] -------- A JsonResponse object is
 * created if the content-type header contains 'application/json' but no
 * location header.//  w w  w .ja  va  2s .  c  om
 */
@Test
public void testCreateJsonResponseNoLocationHeader() {
    System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]")
            + "-------- A JsonResponse object is created if the content-type header contains 'application/json' "
            + "but no location header");
    HttpResponseFactory factory = new DefaultHttpResponseFactory();
    HttpResponse response = factory
            .newHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null), null);
    String responseStr = "{\"somefield1\":\"somevalue1\",\"somefield2\":\"somevalue2\","
            + "\"somefield3\":\"somevalue3\"}";

    try {
        response.setEntity(new StringEntity(responseStr));
    } catch (UnsupportedEncodingException e) {
        System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]")
                + "- FAIL - There was some problem when creating the HttpResponse object");
        throw new AssertionError(e.getMessage());
    } // try catch

    response.addHeader("Content-Type", "application/json");
    HttpBackend httpBackend = new HttpBackendImpl(host, port, false, false, null, null, null, null, maxConns,
            maxConnsPerRoute);

    try {
        JsonResponse jsonRes = httpBackend.createJsonResponse(response);

        try {
            assertEquals(null, jsonRes.getLocationHeader());
            System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]")
                    + "-  OK  - The JsonResponse object was created with null location header");
        } catch (AssertionError e) {
            System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]")
                    + "- FAIL - The JsonResponse object was not created with null location header");
            throw e;
        } // try catch
    } catch (Exception e) {
        System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]")
                + "- FAIL - There was some problem when creating the JsonResponse object");
        throw new AssertionError(e.getMessage());
    } // try catch
}

From source file:org.wildfly.camel.test.hipchat.HipchatConsumerIntegrationTest.java

@Test
public void sendInOnlyNoResponse() throws Exception {
    CamelContext camelctx = createCamelContext();

    MockEndpoint result = camelctx.getEndpoint("mock:result", MockEndpoint.class);
    result.expectedMessageCount(0);/*  ww w  .ja  v a  2s.  co  m*/

    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        HttpEntity mockHttpEntity = mock(HttpEntity.class);
        when(mockHttpEntity.getContent()).thenReturn(null);
        when(closeableHttpResponse.getEntity()).thenReturn(mockHttpEntity);
        when(closeableHttpResponse.getStatusLine())
                .thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, ""));

        result.assertIsSatisfied();
    } finally {
        camelctx.stop();
    }
}

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

private HttpResponse handleResponse(final HttpHost httpHost, final HttpRequest request,
        final HttpResponse originalResponse, final HttpContext context) throws IOException {
    final GoodDataChallengeType challenge = identifyGoodDataChallenge(originalResponse);
    if (challenge == GoodDataChallengeType.UNKNOWN) {
        return originalResponse;
    }/*  www  .ja va  2  s  . c  om*/
    EntityUtils.consume(originalResponse.getEntity());

    try {
        if (authLock.tryLock()) {
            //only one thread requiring authentication will get here.
            final Lock writeLock = rwLock.writeLock();
            writeLock.lock();
            boolean doSST = true;
            try {
                if (challenge == GoodDataChallengeType.TT && sst != null) {
                    if (refreshTt()) {
                        doSST = false;
                    }
                }
                if (doSST) {
                    sst = sstStrategy.obtainSst(httpClient, authHost);
                    if (!refreshTt()) {
                        throw new GoodDataAuthException("Unable to obtain TT after successfully obtained SST");
                    }
                }
            } catch (GoodDataAuthException e) {
                return new BasicHttpResponse(new BasicStatusLine(originalResponse.getProtocolVersion(),
                        HttpStatus.SC_UNAUTHORIZED, e.getMessage()));
            } finally {
                writeLock.unlock();
            }
        } else {
            // the other thread is performing auth and thus is holding the write lock
            // lets wait until it is finished (the write lock is granted) and then continue
            authLock.lock();
        }
    } finally {
        authLock.unlock();
    }
    return this.execute(httpHost, request, context);
}

From source file:com.comcast.video.dawg.house.DawgHouseClientTest.java

@Test
public void testDeleteMetaStb() throws HttpException {
    RestClient mockClient = EasyMock.createMock(RestClient.class);

    DawgHouseClient client = new DawgHouseClient();
    client.setClient(mockClient);//from   www.jav  a2s  .  c om

    RestResponse response = new RestResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 0, 0), 0, null));
    EasyMock.expect(mockClient.delete((String) EasyMock.anyObject())).andReturn(true);
    EasyMock.replay(mockClient);

    String id = "";
    MetaStb stb = EasyMock.createMock(MetaStb.class);
    EasyMock.expect(stb.getId()).andReturn(id);
    EasyMock.expect(stb.getData()).andReturn(new HashMap<String, Object>());
    EasyMock.replay(stb);
    try {
        client.delete(stb);
    } catch (Exception e) {
        Assert.fail("delete stb  threw " + e);
    }
}

From source file:eionet.webq.web.interceptor.CdrAuthorizationInterceptorTest.java

private void assertUrlIsExtractedFromCookieRequest(MockHttpServletRequest request, String expectedValue)
        throws Exception {

    interceptor = spy(interceptor);//from   w w w . j av a2  s .  c  om
    CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    when(response.getStatusLine())
            .thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 2, 0), 200, "OK"));
    doReturn(response).when(interceptor).fetchUrlWithoutRedirection(anyString(), (HttpHeaders) anyObject());
    interceptor.preHandle(request, new MockHttpServletResponse(), null);

    ArgumentCaptor<String> urlCaptor = ArgumentCaptor.forClass(String.class);
    verify(interceptor).fetchUrlWithoutRedirection(urlCaptor.capture(), any(HttpHeaders.class));

    assertThat(urlCaptor.getValue(), containsString(expectedValue));
}

From source file:org.sentilo.platform.server.test.parser.DataParserTest.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, getObservationsFromProvider());

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