Search in sources :

Example 1 with ByteArrayProvider

use of com.canoo.platform.core.http.ByteArrayProvider in project dolphin-platform by canoo.

the class HttpClientTests method testSimpleGetWithByteContent.

@Test
public void testSimpleGetWithByteContent() throws Exception {
    // given:
    final HttpClient client = PlatformClient.getService(HttpClient.class);
    final AtomicBoolean actionCalled = new AtomicBoolean(false);
    final AtomicBoolean doneCalled = new AtomicBoolean(false);
    final AtomicBoolean errorCalled = new AtomicBoolean(false);
    // when:
    final CompletableFuture<HttpResponse<ByteArrayProvider>> future = client.get("http://localhost:" + freePort).withoutContent().readBytes().onDone(response -> {
        actionCalled.set(true);
        doneCalled.set(true);
    }).onError(e -> {
        actionCalled.set(true);
        errorCalled.set(true);
    }).execute();
    // then:
    final HttpResponse<ByteArrayProvider> response = future.get(1_000, TimeUnit.MILLISECONDS);
    assertThat("response not defined", response, notNullValue());
    assertThat("Wrong response code", response.getStatusCode(), is(200));
    assertThat("Content should not be null", response.getRawContent(), notNullValue());
    final byte[] bytes = response.getContent().get();
    assertThat("Byte content does not match", bytes, is("Spark Server for HTTP client integration tests".getBytes()));
    assertThatDoneCalledAndErrorNotCalled(actionCalled, doneCalled, errorCalled);
}
Also used : AfterClass(org.testng.annotations.AfterClass) SC_HTTP_RESOURCE_NOTFOUND(com.canoo.dp.impl.platform.core.http.HttpStatus.SC_HTTP_RESOURCE_NOTFOUND) ByteArrayProvider(com.canoo.platform.core.http.ByteArrayProvider) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) ConnectionException(com.canoo.platform.core.http.ConnectionException) HttpResponse(com.canoo.platform.core.http.HttpResponse) BeforeClass(org.testng.annotations.BeforeClass) HttpClient(com.canoo.platform.core.http.HttpClient) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.testng.annotations.Test) CompletableFuture(java.util.concurrent.CompletableFuture) HttpHeaderConstants(com.canoo.dp.impl.platform.core.http.HttpHeaderConstants) BadResponseException(com.canoo.platform.core.http.BadResponseException) SC_HTTP_UNAUTHORIZED(com.canoo.dp.impl.platform.core.http.HttpStatus.SC_HTTP_UNAUTHORIZED) ServerSocket(java.net.ServerSocket) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) PlatformClient(com.canoo.platform.client.PlatformClient) Assert(org.testng.Assert) Gson(com.google.gson.Gson) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Spark(spark.Spark) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpClient(com.canoo.platform.core.http.HttpClient) HttpResponse(com.canoo.platform.core.http.HttpResponse) ByteArrayProvider(com.canoo.platform.core.http.ByteArrayProvider) Test(org.testng.annotations.Test)

Aggregations

HttpHeaderConstants (com.canoo.dp.impl.platform.core.http.HttpHeaderConstants)1 SC_HTTP_RESOURCE_NOTFOUND (com.canoo.dp.impl.platform.core.http.HttpStatus.SC_HTTP_RESOURCE_NOTFOUND)1 SC_HTTP_UNAUTHORIZED (com.canoo.dp.impl.platform.core.http.HttpStatus.SC_HTTP_UNAUTHORIZED)1 PlatformClient (com.canoo.platform.client.PlatformClient)1 BadResponseException (com.canoo.platform.core.http.BadResponseException)1 ByteArrayProvider (com.canoo.platform.core.http.ByteArrayProvider)1 ConnectionException (com.canoo.platform.core.http.ConnectionException)1 HttpClient (com.canoo.platform.core.http.HttpClient)1 HttpResponse (com.canoo.platform.core.http.HttpResponse)1 Gson (com.google.gson.Gson)1 ServerSocket (java.net.ServerSocket)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Matchers.is (org.hamcrest.Matchers.is)1 Matchers.notNullValue (org.hamcrest.Matchers.notNullValue)1 Assert (org.testng.Assert)1 AfterClass (org.testng.annotations.AfterClass)1