Search in sources :

Example 86 with BasicStatusLine

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

the class DownloaderWebClientTest method testGetBusinessObjectDataDownloadCredentialAssertThrowIOExceptionWhenClosingHttpClientThrowsIOException.

/**
 * Asserts that the http client is closed and if an exception is thrown during closing of the http client, the same exception is bubbled up.
 */
@Test
public void testGetBusinessObjectDataDownloadCredentialAssertThrowIOExceptionWhenClosingHttpClientThrowsIOException() throws Exception {
    HttpClientOperations mockHttpClientOperations = mock(HttpClientOperations.class);
    HttpClientOperations originalHttpClientOperations = (HttpClientOperations) ReflectionTestUtils.getField(downloaderWebClient, "httpClientOperations");
    ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", mockHttpClientOperations);
    try {
        CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class);
        when(mockHttpClientOperations.createHttpClient()).thenReturn(closeableHttpClient);
        doThrow(IOException.class).when(closeableHttpClient).close();
        CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class);
        when(mockHttpClientOperations.execute(any(), any())).thenReturn(closeableHttpResponse);
        when(closeableHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "SUCCESS"));
        when(closeableHttpResponse.getEntity()).thenReturn(new StringEntity(xmlHelper.objectToXml(new StorageUnitDownloadCredential())));
        DownloaderInputManifestDto downloaderInputManifestDto = new DownloaderInputManifestDto();
        String storageName = "storageName";
        try {
            downloaderWebClient.getStorageUnitDownloadCredential(downloaderInputManifestDto, storageName);
            verify(closeableHttpClient).close();
            fail();
        } catch (Exception e) {
            assertEquals(IOException.class, e.getClass());
        }
    } finally {
        ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", originalHttpClientOperations);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) StringEntity(org.apache.http.entity.StringEntity) DownloaderInputManifestDto(org.finra.herd.model.dto.DownloaderInputManifestDto) StorageUnitDownloadCredential(org.finra.herd.model.api.xml.StorageUnitDownloadCredential) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) HttpClientOperations(org.finra.herd.dao.HttpClientOperations) IOException(java.io.IOException) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 87 with BasicStatusLine

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

the class OauthAsyncCompletionHandlerTest method shouldReleaseLatchOnFailure.

@Test
public void shouldReleaseLatchOnFailure() throws Exception {
    handler = new OAuthAsyncCompletionHandler<>(callback, ALL_GOOD_RESPONSE_CONVERTER);
    final HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("4", 1, 1), 200, "ok"));
    final BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(new ByteArrayInputStream(new byte[0]));
    response.setEntity(entity);
    handler.failed(new RuntimeException());
    assertNull(callback.getResponse());
    assertNotNull(callback.getThrowable());
    assertTrue(callback.getThrowable() instanceof RuntimeException);
    // verify latch is released
    try {
        handler.getResult();
        fail();
    } catch (ExecutionException expected) {
    // expected
    }
}
Also used : BasicHttpResponse(org.apache.http.message.BasicHttpResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) ProtocolVersion(org.apache.http.ProtocolVersion) ExecutionException(java.util.concurrent.ExecutionException) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 88 with BasicStatusLine

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

the class OauthAsyncCompletionHandlerTest method shouldReleaseLatchOnIOException.

@Test
public void shouldReleaseLatchOnIOException() throws Exception {
    handler = new OAuthAsyncCompletionHandler<>(callback, EXCEPTION_RESPONSE_CONVERTER);
    final HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("4", 1, 1), 200, "ok"));
    final BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(new ByteArrayInputStream(new byte[0]));
    response.setEntity(entity);
    handler.completed(response);
    assertNull(callback.getResponse());
    assertNotNull(callback.getThrowable());
    assertTrue(callback.getThrowable() instanceof IOException);
    // verify latch is released
    try {
        handler.getResult();
        fail();
    } catch (ExecutionException expected) {
    // expected
    }
}
Also used : BasicHttpResponse(org.apache.http.message.BasicHttpResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) IOException(java.io.IOException) ProtocolVersion(org.apache.http.ProtocolVersion) ExecutionException(java.util.concurrent.ExecutionException) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 89 with BasicStatusLine

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

the class JobUtilTest method testJobFromHttpResponse.

@Test
public void testJobFromHttpResponse() throws Exception {
    BasicHttpResponse response = new BasicHttpResponse(new BasicStatusLine(new HttpVersion(1, 1), HttpStatus.SC_NO_CONTENT, "No message"));
    Option<Job> job = JobUtil.jobFromHttpResponse.apply(response);
    assertFalse(job.isSome());
    JaxbJob jaxbJob = new JaxbJob(new JobImpl(32));
    response.setEntity(new StringEntity(JobParser.toXml(jaxbJob), StandardCharsets.UTF_8));
    job = JobUtil.jobFromHttpResponse.apply(response);
    assertTrue(job.isSome());
    assertEquals(jaxbJob.toJob(), job.get());
}
Also used : StringEntity(org.apache.http.entity.StringEntity) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) JobUtil.waitForJob(org.opencastproject.util.JobUtil.waitForJob) HttpVersion(org.apache.http.HttpVersion) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 90 with BasicStatusLine

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

the class TrustedHttpClientImplTest method testNotAcceptsUrlSigningService.

@Test
public void testNotAcceptsUrlSigningService() throws IOException {
    String notAcceptsUrl = "http://notaccepts.com";
    HttpGet get = new HttpGet(notAcceptsUrl);
    // Setup signing service
    UrlSigningService urlSigningService = EasyMock.createMock(UrlSigningService.class);
    EasyMock.expect(urlSigningService.accepts(notAcceptsUrl)).andReturn(false);
    EasyMock.replay(urlSigningService);
    CredentialsProvider cp = EasyMock.createNiceMock(CredentialsProvider.class);
    Capture<HttpUriRequest> request = EasyMock.newCapture();
    // Setup Http Client
    HttpClient httpClient = createMock("Request", HttpClient.class);
    HttpParams httpParams = createNiceMock(HttpParams.class);
    expect(httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000)).andReturn(httpParams);
    expect(httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000)).andReturn(httpParams);
    replay(httpParams);
    expect(httpClient.getParams()).andReturn(httpParams).anyTimes();
    expect(httpClient.getCredentialsProvider()).andReturn(cp);
    expect(httpClient.execute(EasyMock.capture(request))).andReturn(new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "ok")));
    EasyMock.replay(httpClient);
    // Setup DefaultHttpClientFactory
    HttpClientFactory httpClientFactory = createMock(HttpClientFactory.class);
    expect(httpClientFactory.makeHttpClient()).andReturn(httpClient).atLeastOnce();
    replay(httpClientFactory);
    client = new TrustedHttpClientImpl("user", "pass");
    client.setHttpClientFactory(httpClientFactory);
    client.setSecurityService(securityService);
    client.setUrlSigningService(urlSigningService);
    client.execute(get);
    assertTrue(request.hasCaptured());
    assertEquals(get.getURI().toString(), request.getValue().getURI().toString());
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) UrlSigningService(org.opencastproject.security.urlsigning.service.UrlSigningService) HttpParams(org.apache.http.params.HttpParams) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpGet(org.apache.http.client.methods.HttpGet) HttpClient(org.opencastproject.kernel.http.api.HttpClient) CredentialsProvider(org.apache.http.client.CredentialsProvider) HttpClientFactory(org.opencastproject.kernel.http.impl.HttpClientFactory) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.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