Search in sources :

Example 36 with WebClient

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

the class HeaderTest method userAgentNotOverridden.

@Test
public void userAgentNotOverridden() {
    WebClient webClient = createNewClient(new HeaderTestService(TEST_USER));
    webClient.get().headers(headers -> {
        headers.add(Http.Header.USER_AGENT, TEST_USER);
        return headers;
    }).request(JsonObject.class).await();
}
Also used : JsonObject(jakarta.json.JsonObject) WebClient(io.helidon.webclient.WebClient) Test(org.junit.jupiter.api.Test)

Example 37 with WebClient

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

the class HeaderTest method contentLengthSet.

@Test
public void contentLengthSet() {
    WebClient webClient = createNewClient();
    String contentLength = webClient.post().path("contentLength").submit().flatMapSingle(response -> response.content().as(String.class)).await();
    assertThat(contentLength, is(Http.Header.CONTENT_LENGTH + " is 0"));
    contentLength = webClient.put().path("contentLength").submit().flatMapSingle(response -> response.content().as(String.class)).await();
    assertThat(contentLength, is(Http.Header.CONTENT_LENGTH + " is 0"));
    contentLength = webClient.get().path("contentLength").request().flatMapSingle(response -> response.content().as(String.class)).await();
    assertThat(contentLength, is("No " + Http.Header.CONTENT_LENGTH + " has been set"));
    String sampleSmallEntity = "Hi there";
    contentLength = webClient.post().path("contentLength").submit(sampleSmallEntity).flatMapSingle(response -> response.content().as(String.class)).await();
    assertThat(contentLength, is(Http.Header.CONTENT_LENGTH + " is " + sampleSmallEntity.length()));
    contentLength = webClient.post().headers(headers -> {
        headers.contentLength(0);
        return headers;
    }).path("contentLength").submit(sampleSmallEntity).flatMapSingle(response -> response.content().as(String.class)).await();
    assertThat(contentLength, is(Http.Header.CONTENT_LENGTH + " is " + sampleSmallEntity.length()));
}
Also used : WebClientServiceRequest(io.helidon.webclient.WebClientServiceRequest) WebClient(io.helidon.webclient.WebClient) WebClientService(io.helidon.webclient.spi.WebClientService) Test(org.junit.jupiter.api.Test) List(java.util.List) Matchers.contains(org.hamcrest.Matchers.contains) WebClientServiceResponse(io.helidon.webclient.WebClientServiceResponse) JsonObject(jakarta.json.JsonObject) Single(io.helidon.common.reactive.Single) Matchers.hasSize(org.hamcrest.Matchers.hasSize) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Http(io.helidon.common.http.Http) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) WebClient(io.helidon.webclient.WebClient) Test(org.junit.jupiter.api.Test)

Example 38 with WebClient

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

the class MediaContextTest method testMediaSupportWithoutDefaults.

@Test
public void testMediaSupportWithoutDefaults() throws Exception {
    WebClient client = WebClient.builder().baseUri("http://localhost:" + webServer.port() + "/greet").mediaContext(MediaContext.empty()).build();
    client.get().request(String.class).thenAccept(it -> fail("No reader for String should be registered!")).exceptionally(ex -> {
        assertThat(ex.getCause().getMessage(), is("No reader found for type: class java.lang.String"));
        return null;
    }).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 39 with WebClient

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

the class MediaContextTest method testRequestSpecificReader.

@Test
public void testRequestSpecificReader() throws Exception {
    WebClient client = WebClient.builder().baseUri("http://localhost:" + webServer.port() + "/greet").build();
    client.get().request(JsonObject.class).thenAccept(it -> fail("JsonObject should not have been handled.")).thenCompose(it -> {
        WebClientRequestBuilder requestBuilder = client.get();
        requestBuilder.readerContext().registerReader(JsonpSupport.reader());
        return requestBuilder.request(JsonObject.class);
    }).thenAccept(jsonObject -> assertThat(jsonObject.getString("message"), is(DEFAULT_GREETING + " World!"))).thenCompose(it -> client.get().request(JsonObject.class)).thenAccept(it -> fail("JsonObject should not have been handled.")).exceptionally(throwable -> {
        assertThat(throwable.getCause().getMessage(), is("No reader found for type: interface jakarta.json.JsonObject"));
        return null;
    }).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) JsonObject(jakarta.json.JsonObject) WebClient(io.helidon.webclient.WebClient) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) Test(org.junit.jupiter.api.Test)

Example 40 with WebClient

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

the class MediaContextTest method testReaderRegisteredOnClient.

@Test
public void testReaderRegisteredOnClient() throws Exception {
    WebClient client = WebClient.builder().baseUri("http://localhost:" + webServer.port() + "/greet").addReader(JsonpSupport.reader()).build();
    client.get().request(JsonObject.class).thenAccept(it -> assertThat(it, is(JSON_GREETING))).thenCompose(it -> client.put().path("/greeting").submit(JSON_NEW_GREETING)).thenAccept(it -> fail("No writer for String should be registered!")).exceptionally(ex -> {
        assertThat(ex.getCause().getMessage(), is("Transformation failed!"));
        return null;
    }).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)

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