Search in sources :

Example 6 with WebClient

use of io.helidon.webclient.WebClient in project helidon by oracle.

the class LifeCycleExtension method shutdown.

/**
 * Shutdown test application
 */
public void shutdown() {
    WebClient testClient = WebClient.builder().baseUri(String.format("http://localhost:%d", HelidonProcessRunner.HTTP_PORT)).build();
    WebClientResponse response = testClient.get().path("/Exit").submit().await(1, TimeUnit.MINUTES);
    LOGGER.info(() -> String.format("Status: %s", response.status()));
    LOGGER.info(() -> String.format("Response: %s", response.content().as(String.class).await(1, TimeUnit.MINUTES)));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) WebClient(io.helidon.webclient.WebClient)

Example 7 with WebClient

use of io.helidon.webclient.WebClient in project helidon by oracle.

the class GreetService method basicAuthOutbound.

private void basicAuthOutbound(ServerRequest serverRequest, ServerResponse response) {
    WebClient webClient = WebClient.builder().baseUri("http://localhost:" + Main.serverPort + "/greet/secure/basic").addService(WebClientSecurity.create()).build();
    webClient.get().request().thenAccept(clientResponse -> {
        response.status(clientResponse.status());
        response.send(clientResponse.content());
    }).exceptionally(throwable -> {
        response.status(Http.Status.INTERNAL_SERVER_ERROR_500);
        response.send();
        return null;
    });
}
Also used : WebClient(io.helidon.webclient.WebClient) DataChunk(io.helidon.common.http.DataChunk) Context(io.helidon.common.context.Context) JsonBuilderFactory(jakarta.json.JsonBuilderFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) Level(java.util.logging.Level) FormParams(io.helidon.common.http.FormParams) ServerResponse(io.helidon.webserver.ServerResponse) JsonObject(jakarta.json.JsonObject) Service(io.helidon.webserver.Service) JsonException(jakarta.json.JsonException) Http(io.helidon.common.http.Http) Multi(io.helidon.common.reactive.Multi) Config(io.helidon.config.Config) WebClientSecurity(io.helidon.webclient.security.WebClientSecurity) SecurityContext(io.helidon.security.SecurityContext) Logger(java.util.logging.Logger) Contexts(io.helidon.common.context.Contexts) Executors(java.util.concurrent.Executors) ServerRequest(io.helidon.webserver.ServerRequest) Json(jakarta.json.Json) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Principal(java.security.Principal) Optional(java.util.Optional) Routing(io.helidon.webserver.Routing) Collections(java.util.Collections) WebClient(io.helidon.webclient.WebClient)

Example 8 with WebClient

use of io.helidon.webclient.WebClient in project helidon by oracle.

the class ContextCheckTest method testContextCheck.

@Test
void testContextCheck() {
    WebClient webClient = createNewClient();
    WebClientResponse r = webClient.get().path("/contextCheck").property(HttpBasicAuthProvider.EP_PROPERTY_OUTBOUND_USER, "jack").property(HttpBasicAuthProvider.EP_PROPERTY_OUTBOUND_PASSWORD, "password").request().await();
    assertThat(r.status().code(), is(Http.Status.OK_200.code()));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) WebClient(io.helidon.webclient.WebClient) Test(org.junit.jupiter.api.Test)

Example 9 with WebClient

use of io.helidon.webclient.WebClient in project helidon by oracle.

the class MediaContextTest method testWriterRegisteredOnClient.

@Test
public void testWriterRegisteredOnClient() throws Exception {
    WebClient client = WebClient.builder().baseUri("http://localhost:" + webServer.port() + "/greet").addWriter(JsonpSupport.writer()).build();
    client.put().path("/greeting").submit(JSON_NEW_GREETING).thenCompose(it -> client.get().request(JsonObject.class)).thenAccept(it -> fail("JsonReader should not be registered!")).exceptionally(ex -> {
        assertThat(ex.getCause().getMessage(), is("No reader found for type: interface jakarta.json.JsonObject"));
        return null;
    }).thenCompose(// Cleanup
    it -> client.put().path("/greeting").submit(JSON_OLD_GREETING)).toCompletableFuture().get();
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) WebClient(io.helidon.webclient.WebClient) JsonBuilderFactory(jakarta.json.JsonBuilderFactory) IOException(java.io.IOException) MediaContext(io.helidon.media.common.MediaContext) Json(jakarta.json.Json) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test) JsonpSupport(io.helidon.media.jsonp.JsonpSupport) JsonObject(jakarta.json.JsonObject) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Collections(java.util.Collections) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) InputStream(java.io.InputStream) WebClient(io.helidon.webclient.WebClient) Test(org.junit.jupiter.api.Test)

Example 10 with WebClient

use of io.helidon.webclient.WebClient in project helidon by oracle.

the class MetricsTest method testErrorHandling.

@Test
public void testErrorHandling() {
    WebClientService errorAll = WebClientMetrics.counter().success(false).nameFormat("counter.all.errors.%2$s").build();
    WebClientService errorGet = WebClientMetrics.counter().methods(Http.Method.GET).success(false).nameFormat("counter.errors.%1$s.%2$s").build();
    WebClientService errorPut = WebClientMetrics.counter().methods(Http.Method.PUT).success(false).nameFormat("counter.errors.%1$s.%2$s").build();
    WebClient webClient = createNewClient(errorAll, errorGet, errorPut);
    Counter counterAll = FACTORY.counter("counter.all.errors.localhost");
    Counter counterGet = FACTORY.counter("counter.errors.GET.localhost");
    Counter counterPut = FACTORY.counter("counter.errors.PUT.localhost");
    assertThat(counterAll.getCount(), is(0L));
    assertThat(counterGet.getCount(), is(0L));
    assertThat(counterPut.getCount(), is(0L));
    try {
        webClient.get().path("/invalid").request().thenCompose(WebClientResponse::close).toCompletableFuture().get();
        assertThat(counterAll.getCount(), is(1L));
        assertThat(counterGet.getCount(), is(1L));
        assertThat(counterPut.getCount(), is(0L));
        webClient.put().path("/invalid").submit().thenCompose(WebClientResponse::close).toCompletableFuture().get();
        assertThat(counterAll.getCount(), is(2L));
        assertThat(counterGet.getCount(), is(1L));
        assertThat(counterPut.getCount(), is(1L));
    } catch (Exception e) {
        fail(e);
    }
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Counter(org.eclipse.microprofile.metrics.Counter) WebClientService(io.helidon.webclient.spi.WebClientService) WebClient(io.helidon.webclient.WebClient) Test(org.junit.jupiter.api.Test)

Aggregations

WebClient (io.helidon.webclient.WebClient)58 Test (org.junit.jupiter.api.Test)42 WebClientResponse (io.helidon.webclient.WebClientResponse)21 JsonObject (jakarta.json.JsonObject)15 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)13 Http (io.helidon.common.http.Http)11 Config (io.helidon.config.Config)11 Collections (java.util.Collections)10 WebClientRequestBuilder (io.helidon.webclient.WebClientRequestBuilder)9 WebClientService (io.helidon.webclient.spi.WebClientService)9 Json (jakarta.json.Json)9 JsonBuilderFactory (jakarta.json.JsonBuilderFactory)8 IOException (java.io.IOException)8 DataChunk (io.helidon.common.http.DataChunk)7 JsonpSupport (io.helidon.media.jsonp.JsonpSupport)7 CompletionException (java.util.concurrent.CompletionException)7 Context (io.helidon.common.context.Context)6 List (java.util.List)6 TimeUnit (java.util.concurrent.TimeUnit)6 Counter (org.eclipse.microprofile.metrics.Counter)6