Search in sources :

Example 51 with WebClient

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

the class TracingTest method testTracingNoServerSuccess.

@Test
void testTracingNoServerSuccess() throws ExecutionException, InterruptedException {
    MockTracer mockTracer = new MockTracer();
    String uri = "http://localhost:" + webServer.port() + "/greet";
    Context context = Context.builder().id("tracing-unit-test").build();
    context.register(mockTracer);
    WebClient client = WebClient.builder().baseUri(uri).context(context).addMediaSupport(JsonpSupport.create()).config(CONFIG.get("client")).build();
    WebClientResponse response = client.get().request().toCompletableFuture().get();
    // we must fully read entity for tracing to finish
    response.content().as(JsonObject.class).toCompletableFuture().get();
    List<MockSpan> mockSpans = mockTracer.finishedSpans();
    assertThat(mockSpans, iterableWithSize(1));
    MockSpan theSpan = mockSpans.get(0);
    assertThat(theSpan.operationName(), is("GET-" + uri));
    List<MockSpan.LogEntry> logEntries = theSpan.logEntries();
    assertThat(logEntries, empty());
    Map<String, Object> tags = theSpan.tags();
    assertThat(tags.get(Tags.HTTP_STATUS.getKey()), is(200));
    assertThat(tags.get(Tags.HTTP_METHOD.getKey()), is("GET"));
    assertThat(tags.get(Tags.HTTP_URL.getKey()), is(uri));
    assertThat(tags.get(Tags.COMPONENT.getKey()), is("helidon-webclient"));
}
Also used : Context(io.helidon.common.context.Context) WebClientResponse(io.helidon.webclient.WebClientResponse) MockTracer(io.opentracing.mock.MockTracer) JsonObject(jakarta.json.JsonObject) WebClient(io.helidon.webclient.WebClient) MockSpan(io.opentracing.mock.MockSpan) Test(org.junit.jupiter.api.Test)

Example 52 with WebClient

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

the class TracingTest method testTracingNoServerFailure.

@Test
void testTracingNoServerFailure() throws ExecutionException, InterruptedException {
    MockTracer mockTracer = new MockTracer();
    Context context = Context.builder().id("tracing-unit-test").build();
    context.register(mockTracer);
    WebClient client = WebClient.builder().baseUri("http://localhost:" + webServer.port() + "/greet").context(context).addMediaSupport(JsonpSupport.create()).config(CONFIG.get("client")).build();
    WebClientResponse response = client.get().path("/error").request().toCompletableFuture().get();
    // we must fully read entity, as otherwise tracing does not finish
    response.content().as(String.class).toCompletableFuture().get();
    List<MockSpan> mockSpans = mockTracer.finishedSpans();
    assertThat(mockSpans, iterableWithSize(1));
    MockSpan theSpan = mockSpans.get(0);
    assertThat(theSpan.operationName(), is("GET-http://localhost:" + webServer.port() + "/greet/error"));
    List<MockSpan.LogEntry> logEntries = theSpan.logEntries();
    assertThat(logEntries, iterableWithSize(1));
    MockSpan.LogEntry logEntry = logEntries.get(0);
    Map<String, ?> fields = logEntry.fields();
    assertThat(fields.get("event"), is("error"));
    assertThat(fields.get("message"), is("Response HTTP status: 404"));
    assertThat(fields.get("error.kind"), is("ClientError"));
    Map<String, Object> tags = theSpan.tags();
    assertThat(tags.get(Tags.HTTP_STATUS.getKey()), is(404));
}
Also used : Context(io.helidon.common.context.Context) MockTracer(io.opentracing.mock.MockTracer) WebClient(io.helidon.webclient.WebClient) WebClientResponse(io.helidon.webclient.WebClientResponse) JsonObject(jakarta.json.JsonObject) MockSpan(io.opentracing.mock.MockSpan) Test(org.junit.jupiter.api.Test)

Example 53 with WebClient

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

the class UriPartTest method testQueryNotDecoded.

@Test
public void testQueryNotDecoded() {
    WebClient webClient = createNewClient(request -> {
        assertThat(request.query(), is("first&second%26=val&ue%26"));
        return Single.just(request);
    });
    String response = webClient.get().queryParam("first&second%26", "val&ue%26").skipUriEncoding().request(String.class).await();
    assertThat(response.trim(), is("{\"message\":\"Hello World!\"}"));
}
Also used : WebClient(io.helidon.webclient.WebClient) Test(org.junit.jupiter.api.Test)

Example 54 with WebClient

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

the class UriPartTest method testPathNotDecoded.

@Test
public void testPathNotDecoded() {
    WebClient webClient = createNewClient(request -> {
        assertThat(request.path().toRawString(), is("/greet/path%26"));
        return Single.just(request);
    });
    assertThrows(CompletionException.class, () -> webClient.get().path("path%26").skipUriEncoding().request(String.class).await());
}
Also used : WebClient(io.helidon.webclient.WebClient) Test(org.junit.jupiter.api.Test)

Example 55 with WebClient

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

the class WebclientServiceValuePropagationTest method testUriPartsPropagation.

@Test
public void testUriPartsPropagation() {
    WebClient webClient = WebClient.builder().baseUri("http://invalid").addService(new UriChangingService()).build();
    String response = webClient.get().path("replace/me").request(String.class).await();
    assertThat(response, is("Hi Test"));
}
Also used : 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