Search in sources :

Example 46 with WebClient

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

the class MutualTlsTest method testOptionalAuthentication.

@Test
public void testOptionalAuthentication() {
    WebClient webClient = createWebClient(CONFIG.get("no-client-cert"));
    assertThat(executeRequest(webClient, "https", webServer.port("optional")), is("Hello Unknown CN!"));
    webClient = createWebClient(CONFIG.get("success"));
    assertThat(executeRequest(webClient, "https", webServer.port("optional")), is("Hello Helidon-client!"));
}
Also used : WebClient(io.helidon.webclient.WebClient) Test(org.junit.jupiter.api.Test)

Example 47 with WebClient

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

the class MutualTlsTest method testServerCertInvalidCn.

@Test
public void testServerCertInvalidCn() {
    int port = webServer.port("invalid-server-cert");
    WebClient webClientOne = createWebClient(CONFIG.get("server-cert-invalid-cn"));
    CompletionException exception = assertThrows(CompletionException.class, () -> executeRequest(webClientOne, "https", port));
    assertThat(exception.getCause().getMessage(), is("No name matching localhost found"));
    WebClient webClientTwo = createWebClient(CONFIG.get("client-disable-hostname-verification"));
    assertThat(executeRequest(webClientTwo, "https", port), is("Hello Helidon-client!"));
}
Also used : CompletionException(java.util.concurrent.CompletionException) WebClient(io.helidon.webclient.WebClient) Test(org.junit.jupiter.api.Test)

Example 48 with WebClient

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

the class GreetService method contextCheck.

/**
 * Checks the existence of a {@code Context} object in a WebClient thread.
 *
 * @param request the request
 * @param response the response
 */
private void contextCheck(ServerRequest request, ServerResponse response) {
    WebClient webClient = WebClient.builder().baseUri("http://localhost:" + Main.serverPort + "/").build();
    Optional<Context> context = Contexts.context();
    // Verify that context was propagated with auth enabled
    if (context.isEmpty()) {
        response.status(Http.Status.INTERNAL_SERVER_ERROR_500).send();
        return;
    }
    // Register instance in context
    context.get().register(this);
    // Ensure context is available in webclient threads
    webClient.get().request().thenAccept(clientResponse -> {
        Context singleContext = Contexts.context().orElseThrow();
        Objects.requireNonNull(singleContext.get(GreetService.class));
        response.status(Http.Status.OK_200);
        response.send();
    }).exceptionally(throwable -> {
        response.status(Http.Status.INTERNAL_SERVER_ERROR_500);
        response.send();
        return null;
    });
}
Also used : Context(io.helidon.common.context.Context) SecurityContext(io.helidon.security.SecurityContext) 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 49 with WebClient

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

the class ConnectionCloseTest method testCutConnection.

@Test
void testCutConnection() throws ExecutionException, InterruptedException, TimeoutException {
    WebClient webClient = createNewClient();
    CompletableFuture<Throwable> actualErrorCf = new CompletableFuture<>();
    // Expecting WebClientException: Connection reset by the host
    webClient.get().path("/connectionClose").request().flatMap(WebClientResponse::content).map(DataChunk::bytes).map(String::new).log().onError(actualErrorCf::complete).ignoreElements();
    Throwable actual = actualErrorCf.get(10, TimeUnit.SECONDS);
    assertThat(actual, Matchers.instanceOf(WebClientException.class));
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) DataChunk(io.helidon.common.http.DataChunk) WebClient(io.helidon.webclient.WebClient) WebClientException(io.helidon.webclient.WebClientException) Test(org.junit.jupiter.api.Test)

Example 50 with WebClient

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

the class TracingPropagationTest method testTracingSuccess.

@Test
void testTracingSuccess() throws ExecutionException, InterruptedException {
    MockTracer mockTracer = new MockTracer();
    WebServer webServer = Main.startServer(mockTracer).toCompletableFuture().get();
    Context context = Context.builder().id("tracing-unit-test").build();
    context.register(mockTracer);
    String uri = "http://localhost:" + webServer.port() + "/greet";
    WebClient client = WebClient.builder().baseUri(uri).context(context).config(Config.create().get("client")).build();
    client.get().queryParam("some", "value").fragment("fragment").request().thenCompose(WebClientResponse::close).toCompletableFuture().get();
    TimeUnit.MILLISECONDS.sleep(1);
    List<MockSpan> mockSpans = mockTracer.finishedSpans();
    assertThat("At least one client and one server span expected", mockSpans.size(), greaterThanOrEqualTo(2));
    // we need the first span - parentId 0
    MockSpan clientSpan = findSpanWithParentId(mockSpans, 0);
    assertThat(clientSpan.operationName(), is("GET-" + uri));
    List<MockSpan.LogEntry> logEntries = clientSpan.logEntries();
    assertThat(logEntries, empty());
    Map<String, Object> tags = clientSpan.tags();
    assertThat(tags.get(Tags.HTTP_STATUS.getKey()), is(200));
    // now we want to test first child - first WebServer span
    MockSpan wsSpan = findSpanWithParentId(mockSpans, clientSpan.context().spanId());
    assertThat(wsSpan.operationName(), is("HTTP Request"));
    tags = wsSpan.tags();
    assertThat(tags.get(Tags.HTTP_METHOD.getKey()), is("GET"));
    assertThat(tags.get(Tags.HTTP_URL.getKey()), is("/greet?some=value#fragment"));
    assertThat(tags.get(Tags.HTTP_STATUS.getKey()), is(200));
    assertThat(tags.get(Tags.COMPONENT.getKey()), is("helidon-webserver"));
    webServer.shutdown().toCompletableFuture().get();
}
Also used : Context(io.helidon.common.context.Context) WebClientResponse(io.helidon.webclient.WebClientResponse) WebServer(io.helidon.webserver.WebServer) MockTracer(io.opentracing.mock.MockTracer) WebClient(io.helidon.webclient.WebClient) MockSpan(io.opentracing.mock.MockSpan) 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