Search in sources :

Example 1 with ResponseStatus

use of io.helidon.common.http.Http.ResponseStatus in project helidon by oracle.

the class ContextLifeCycleTest method testContextMediaSupport.

@Test
void testContextMediaSupport() {
    Handler handler = (req, res) -> {
        String cid = req.context().id();
        req.content().as(String.class).thenAccept(payload -> {
            if (cid.equals(contextId())) {
                res.send();
            } else {
                res.status(400).send();
            }
        });
    };
    WebServer webServer = WebServer.builder(Routing.builder().post(handler)).build().start().await(10, TimeUnit.SECONDS);
    ResponseStatus responseStatus = WebClient.builder().baseUri("http://localhost:" + webServer.port()).build().post().submit("some-payload").map(WebClientResponse::status).onTerminate(webServer::shutdown).await(10, TimeUnit.SECONDS);
    assertThat(responseStatus.code(), is(200));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) WebClient(io.helidon.webclient.WebClient) WebClientResponse(io.helidon.webclient.WebClientResponse) Subscription(java.util.concurrent.Flow.Subscription) DataChunk(io.helidon.common.http.DataChunk) Context(io.helidon.common.context.Context) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) Contexts(io.helidon.common.context.Contexts) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Subscriber(java.util.concurrent.Flow.Subscriber) ResponseStatus(io.helidon.common.http.Http.ResponseStatus) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) WebClientResponse(io.helidon.webclient.WebClientResponse) ResponseStatus(io.helidon.common.http.Http.ResponseStatus) Test(org.junit.jupiter.api.Test)

Example 2 with ResponseStatus

use of io.helidon.common.http.Http.ResponseStatus in project helidon by oracle.

the class ContextLifeCycleTest method testContextReactive.

@Test
void testContextReactive() {
    Handler handler = (req, res) -> {
        String cid = req.context().id();
        req.content().subscribe(new Subscriber<>() {

            @Override
            public void onSubscribe(Subscription subscription) {
                subscription.request(Long.MAX_VALUE);
            }

            @Override
            public void onNext(DataChunk item) {
                item.release();
                if (!cid.equals(contextId())) {
                    throw new IllegalStateException("Context invalid");
                }
            }

            @Override
            public void onError(Throwable throwable) {
                res.send(throwable);
            }

            @Override
            public void onComplete() {
                if (!cid.equals(contextId())) {
                    res.send(new IllegalStateException("Context invalid"));
                } else {
                    res.send();
                }
            }
        });
    };
    WebServer webServer = WebServer.builder(Routing.builder().post(handler)).build().start().await(10, TimeUnit.SECONDS);
    ResponseStatus responseStatus = WebClient.builder().baseUri("http://localhost:" + webServer.port()).build().post().submit("some-payload").map(WebClientResponse::status).onTerminate(webServer::shutdown).await(10, TimeUnit.SECONDS);
    assertThat(responseStatus.code(), is(200));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) WebClient(io.helidon.webclient.WebClient) WebClientResponse(io.helidon.webclient.WebClientResponse) Subscription(java.util.concurrent.Flow.Subscription) DataChunk(io.helidon.common.http.DataChunk) Context(io.helidon.common.context.Context) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) Contexts(io.helidon.common.context.Contexts) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Subscriber(java.util.concurrent.Flow.Subscriber) ResponseStatus(io.helidon.common.http.Http.ResponseStatus) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) WebClientResponse(io.helidon.webclient.WebClientResponse) Subscriber(java.util.concurrent.Flow.Subscriber) ResponseStatus(io.helidon.common.http.Http.ResponseStatus) DataChunk(io.helidon.common.http.DataChunk) Subscription(java.util.concurrent.Flow.Subscription) Test(org.junit.jupiter.api.Test)

Aggregations

Context (io.helidon.common.context.Context)2 Contexts (io.helidon.common.context.Contexts)2 DataChunk (io.helidon.common.http.DataChunk)2 ResponseStatus (io.helidon.common.http.Http.ResponseStatus)2 WebClient (io.helidon.webclient.WebClient)2 WebClientResponse (io.helidon.webclient.WebClientResponse)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutionException (java.util.concurrent.ExecutionException)2 Subscriber (java.util.concurrent.Flow.Subscriber)2 Subscription (java.util.concurrent.Flow.Subscription)2 TimeUnit (java.util.concurrent.TimeUnit)2 TimeoutException (java.util.concurrent.TimeoutException)2 CoreMatchers.is (org.hamcrest.CoreMatchers.is)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2 Test (org.junit.jupiter.api.Test)2