Search in sources :

Example 96 with BasicStatusLine

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicStatusLine in project wikidata-query-rdf by wikimedia.

the class EventHttpSenderUnitTest method testPushReturnsFalseOnHttpErrorCode.

@Test
public void testPushReturnsFalseOnHttpErrorCode() throws IOException {
    String eventGateHost = "https://eventgate.test.local/v1/events";
    CloseableHttpClient client = mock(CloseableHttpClient.class);
    CloseableHttpResponse resp = mock(CloseableHttpResponse.class);
    when(resp.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 404, "Not found"));
    when(client.execute(any(HttpUriRequest.class))).thenReturn(resp);
    EventHttpSender sender = new EventHttpSender(client, eventGateHost, JacksonUtil.DEFAULT_OBJECT_WRITER);
    assertThat(sender.push(EventTestUtils.newQueryEvent())).isFalse();
    assertThat(sender.push(Collections.singleton(EventTestUtils.newQueryEvent()))).isEqualTo(0);
    verify(resp, times(2)).close();
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 97 with BasicStatusLine

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicStatusLine 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 98 with BasicStatusLine

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicStatusLine 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 99 with BasicStatusLine

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicStatusLine in project android-volley by mcxiaoke.

the class HurlStack method performRequest.

@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
    String url = request.getUrl();
    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) {
        // 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);
    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;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) ProtocolVersion(org.apache.http.ProtocolVersion) URL(java.net.URL) BasicStatusLine(org.apache.http.message.BasicStatusLine) BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpURLConnection(java.net.HttpURLConnection) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) List(java.util.List) BasicHeader(org.apache.http.message.BasicHeader)

Example 100 with BasicStatusLine

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicStatusLine in project gocd by gocd.

the class HttpServiceTest method shouldDownloadArtifact.

@Test
public void shouldDownloadArtifact() throws IOException, URISyntaxException {
    String url = "http://blah";
    FetchHandler fetchHandler = mock(FetchHandler.class);
    HttpGet mockGetMethod = mock(HttpGet.class);
    CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    BasicHttpEntity basicHttpEntity = new BasicHttpEntity();
    ByteArrayInputStream instream = new ByteArrayInputStream(new byte[] {});
    basicHttpEntity.setContent(instream);
    when(response.getEntity()).thenReturn(basicHttpEntity);
    when(response.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK"));
    when(httpClient.execute(mockGetMethod)).thenReturn(response);
    when(httpClientFactory.createGet(url)).thenReturn(mockGetMethod);
    when(mockGetMethod.getURI()).thenReturn(new URI(url));
    service.download(url, fetchHandler);
    verify(httpClient).execute(mockGetMethod);
    verify(fetchHandler).handle(instream);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) FetchHandler(com.thoughtworks.go.domain.FetchHandler) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) URI(java.net.URI) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.jupiter.api.Test)

Aggregations

BasicStatusLine (org.apache.http.message.BasicStatusLine)118 ProtocolVersion (org.apache.http.ProtocolVersion)67 Test (org.junit.Test)56 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)54 StatusLine (org.apache.http.StatusLine)45 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)33 StringEntity (org.apache.http.entity.StringEntity)32 ByteArrayInputStream (java.io.ByteArrayInputStream)29 HttpResponse (org.apache.http.HttpResponse)27 IOException (java.io.IOException)19 HttpEntity (org.apache.http.HttpEntity)19 BasicHeader (org.apache.http.message.BasicHeader)19 URL (java.net.URL)17 HttpURLConnection (java.net.HttpURLConnection)16 List (java.util.List)16 Header (org.apache.http.Header)16 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)16 HashMap (java.util.HashMap)14 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)13 BasicHttpEntity (org.apache.http.entity.BasicHttpEntity)12