Search in sources :

Example 76 with BasicHttpResponse

use of org.apache.http.message.BasicHttpResponse in project docker-java-api by amihaiemil.

the class AssertRequestTestCase method returnResponseIfRequestMeetsCondition.

/**
 * Should return the given response if the request meets the given
 * condition.
 *
 * @throws Exception Unexpected.
 */
@Test
public void returnResponseIfRequestMeetsCondition() throws Exception {
    final HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_OK, "OK"));
    MatcherAssert.assertThat(new AssertRequest(response, new Condition("", // @checkstyle LineLength (1 line)
    r -> "http://some.test.com".equals(r.getRequestLine().getUri()))).execute(new HttpGet("http://some.test.com")), Matchers.is(response));
}
Also used : ProtocolVersion(org.apache.http.ProtocolVersion) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) MatcherAssert(org.hamcrest.MatcherAssert) HttpGet(org.apache.http.client.methods.HttpGet) BasicStatusLine(org.apache.http.message.BasicStatusLine) HttpResponse(org.apache.http.HttpResponse) HttpStatus(org.apache.http.HttpStatus) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpGet(org.apache.http.client.methods.HttpGet) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) ProtocolVersion(org.apache.http.ProtocolVersion) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 77 with BasicHttpResponse

use of org.apache.http.message.BasicHttpResponse 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 78 with BasicHttpResponse

use of org.apache.http.message.BasicHttpResponse 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 79 with BasicHttpResponse

use of org.apache.http.message.BasicHttpResponse 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 80 with BasicHttpResponse

use of org.apache.http.message.BasicHttpResponse 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

BasicHttpResponse (org.apache.http.message.BasicHttpResponse)107 BasicStatusLine (org.apache.http.message.BasicStatusLine)64 ProtocolVersion (org.apache.http.ProtocolVersion)58 HttpResponse (org.apache.http.HttpResponse)51 StatusLine (org.apache.http.StatusLine)39 Test (org.junit.Test)39 StringEntity (org.apache.http.entity.StringEntity)34 IOException (java.io.IOException)33 List (java.util.List)24 Header (org.apache.http.Header)22 HttpHost (org.apache.http.HttpHost)19 HashMap (java.util.HashMap)18 URL (java.net.URL)17 BasicHeader (org.apache.http.message.BasicHeader)17 HttpURLConnection (java.net.HttpURLConnection)16 HttpEntity (org.apache.http.HttpEntity)14 MainResponse (org.elasticsearch.action.main.MainResponse)14 Before (org.junit.Before)13 BasicHttpEntity (org.apache.http.entity.BasicHttpEntity)12 ElasticsearchException (org.elasticsearch.ElasticsearchException)12