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();
}
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;
}
});
}
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;
}
});
}
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;
}
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);
}
Aggregations