Search in sources :

Example 76 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project blueocean-plugin by jenkinsci.

the class HttpResponseTest method testWithoutErrorDetails.

@Test
public void testWithoutErrorDetails() {
    org.apache.http.HttpResponse rawHttpResponse = Mockito.mock(org.apache.http.HttpResponse.class);
    when(rawHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new HttpVersion(1, 1), 400, ""));
    when(rawHttpResponse.getEntity()).thenReturn(new BasicHttpEntity() {

        @Override
        public InputStream getContent() throws IllegalStateException {
            return new ByteArrayInputStream("{\"errors\": [{\"message\": \"msg\", \"exceptionName\": \"name\"}]}".getBytes());
        }
    });
    HttpResponse httpResponse = new HttpResponse(rawHttpResponse);
    assertThrows(ServiceException.class, () -> {
        try {
            httpResponse.getContent();
        } catch (ServiceException e) {
            assertTrue(e.toJson().contains("no error details"));
            throw e;
        }
    });
}
Also used : ServiceException(io.jenkins.blueocean.commons.ServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) HttpVersion(org.apache.http.HttpVersion) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 77 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project blueocean-plugin by jenkinsci.

the class HttpResponseTest method testWithErrorDetails.

@Test
public void testWithErrorDetails() {
    org.apache.http.HttpResponse rawHttpResponse = Mockito.mock(org.apache.http.HttpResponse.class);
    when(rawHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new HttpVersion(1, 1), 400, ""));
    when(rawHttpResponse.getEntity()).thenReturn(new BasicHttpEntity() {

        @Override
        public InputStream getContent() throws IllegalStateException {
            return new ByteArrayInputStream("{\"errors\": [{\"message\": \"msg\", \"exceptionName\": \"name\", \"details\":[\"fake-detail\"]}]}".getBytes());
        }
    });
    HttpResponse httpResponse = new HttpResponse(rawHttpResponse);
    assertThrows(ServiceException.class, () -> {
        try {
            httpResponse.getContent();
        } catch (ServiceException e) {
            assertTrue(e.toJson().contains("fake-detail"));
            throw e;
        }
    });
}
Also used : ServiceException(io.jenkins.blueocean.commons.ServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) HttpVersion(org.apache.http.HttpVersion) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 78 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project spring-boot by spring-projects.

the class ElasticsearchRestHealthIndicatorTests method elasticsearchWithYellowStatusIsUp.

@Test
void elasticsearchWithYellowStatusIsUp() throws IOException {
    BasicHttpEntity httpEntity = new BasicHttpEntity();
    httpEntity.setContent(new ByteArrayInputStream(createJsonResult(200, "yellow").getBytes()));
    Response response = mock(Response.class);
    StatusLine statusLine = mock(StatusLine.class);
    given(statusLine.getStatusCode()).willReturn(200);
    given(response.getStatusLine()).willReturn(statusLine);
    given(response.getEntity()).willReturn(httpEntity);
    given(this.restClient.performRequest(any(Request.class))).willReturn(response);
    Health health = this.elasticsearchRestHealthIndicator.health();
    assertThat(health.getStatus()).isEqualTo(Status.UP);
    assertHealthDetailsWithStatus(health.getDetails(), "yellow");
}
Also used : Response(org.elasticsearch.client.Response) StatusLine(org.apache.http.StatusLine) ByteArrayInputStream(java.io.ByteArrayInputStream) Health(org.springframework.boot.actuate.health.Health) Request(org.elasticsearch.client.Request) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) Test(org.junit.jupiter.api.Test)

Example 79 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project spring-boot by spring-projects.

the class ElasticsearchRestHealthIndicatorTests method elasticsearchIsUp.

@Test
void elasticsearchIsUp() throws IOException {
    BasicHttpEntity httpEntity = new BasicHttpEntity();
    httpEntity.setContent(new ByteArrayInputStream(createJsonResult(200, "green").getBytes()));
    Response response = mock(Response.class);
    StatusLine statusLine = mock(StatusLine.class);
    given(statusLine.getStatusCode()).willReturn(200);
    given(response.getStatusLine()).willReturn(statusLine);
    given(response.getEntity()).willReturn(httpEntity);
    given(this.restClient.performRequest(any(Request.class))).willReturn(response);
    Health health = this.elasticsearchRestHealthIndicator.health();
    assertThat(health.getStatus()).isEqualTo(Status.UP);
    assertHealthDetailsWithStatus(health.getDetails(), "green");
}
Also used : Response(org.elasticsearch.client.Response) StatusLine(org.apache.http.StatusLine) ByteArrayInputStream(java.io.ByteArrayInputStream) Health(org.springframework.boot.actuate.health.Health) Request(org.elasticsearch.client.Request) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) Test(org.junit.jupiter.api.Test)

Example 80 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project android-volley by mcxiaoke.

the class HurlStack method entityFromConnection.

/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
Also used : InputStream(java.io.InputStream) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) IOException(java.io.IOException)

Aggregations

BasicHttpEntity (org.apache.http.entity.BasicHttpEntity)139 ByteArrayInputStream (java.io.ByteArrayInputStream)104 Test (org.junit.Test)89 InputStream (java.io.InputStream)60 IOException (java.io.IOException)24 HttpResponse (org.apache.http.HttpResponse)15 StatusLine (org.apache.http.StatusLine)12 BasicStatusLine (org.apache.http.message.BasicStatusLine)11 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)9 HttpException (org.apache.http.HttpException)8 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)8 HttpContext (org.apache.http.protocol.HttpContext)8 URISyntaxException (java.net.URISyntaxException)7 Map (java.util.Map)7 URI (java.net.URI)6 Header (org.apache.http.Header)6 ProtocolVersion (org.apache.http.ProtocolVersion)6 HttpGet (org.apache.http.client.methods.HttpGet)6 ChunkedInputStream (org.apache.http.impl.io.ChunkedInputStream)6 ContentLengthInputStream (org.apache.http.impl.io.ContentLengthInputStream)6