Search in sources :

Example 16 with WebClient

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

the class WebclientServiceValuePropagationTest method testInvalidSchema.

@Test
public void testInvalidSchema() {
    WebClient webClient = WebClient.builder().baseUri("http://localhost:80").addService(new InvalidSchemaService()).build();
    CompletionException exception = assertThrows(CompletionException.class, () -> webClient.get().request(String.class).await());
    assertThat(exception.getCause().getMessage(), is("invalid transport protocol is not supported!"));
}
Also used : CompletionException(java.util.concurrent.CompletionException) WebClient(io.helidon.webclient.WebClient) Test(org.junit.jupiter.api.Test)

Example 17 with WebClient

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

the class TestDefaultCorsSupport method testOptionsWithCors.

@Test
void testOptionsWithCors() throws ExecutionException, InterruptedException {
    WebServer server = null;
    WebClient client;
    try {
        server = WebServer.create(prepRouting(true)).start().toCompletableFuture().get();
        client = WebClient.builder().baseUri("http://localhost:" + server.port()).get();
        WebClientRequestBuilder reqBuilder = client.options().path("/greet");
        Headers h = reqBuilder.headers();
        h.add("Origin", "http://foo.com");
        h.add("Host", "bar.com");
        WebClientResponse response = reqBuilder.submit().toCompletableFuture().get();
        WebClientResponseHeaders headers = response.headers();
        List<String> allowOrigins = headers.values(CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN);
        assertThat(allowOrigins, contains("*"));
    } finally {
        if (server != null) {
            server.shutdown();
        }
    }
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) WebServer(io.helidon.webserver.WebServer) WebClientResponseHeaders(io.helidon.webclient.WebClientResponseHeaders) Headers(io.helidon.common.http.Headers) WebClient(io.helidon.webclient.WebClient) WebClientResponseHeaders(io.helidon.webclient.WebClientResponseHeaders) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) Test(org.junit.jupiter.api.Test)

Example 18 with WebClient

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

the class TestDefaultCorsSupport method testOptionsWithoutCors.

@Test
void testOptionsWithoutCors() throws ExecutionException, InterruptedException {
    WebServer server = null;
    WebClient client;
    try {
        server = WebServer.create(prepRouting(false)).start().toCompletableFuture().get();
        client = WebClient.builder().baseUri("http://localhost:" + server.port()).get();
        WebClientRequestBuilder reqBuilder = client.options().path("/greet");
        Headers h = reqBuilder.headers();
        h.add("Origin", "http://foo.com");
        h.add("Host", "bar.com");
        WebClientResponse response = reqBuilder.submit().toCompletableFuture().get();
        WebClientResponseHeaders headers = response.headers();
        List<String> allowOrigins = headers.values(CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN);
        assertThat(allowOrigins.size(), is(0));
    } finally {
        if (server != null) {
            server.shutdown();
        }
    }
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) WebServer(io.helidon.webserver.WebServer) WebClientResponseHeaders(io.helidon.webclient.WebClientResponseHeaders) Headers(io.helidon.common.http.Headers) WebClient(io.helidon.webclient.WebClient) WebClientResponseHeaders(io.helidon.webclient.WebClientResponseHeaders) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) Test(org.junit.jupiter.api.Test)

Example 19 with WebClient

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

the class TestDefaultCorsSupport method testGetWithoutCors.

@Test
void testGetWithoutCors() throws ExecutionException, InterruptedException {
    WebServer server = null;
    WebClient client;
    try {
        server = WebServer.create(prepRouting(false)).start().toCompletableFuture().get();
        client = WebClient.builder().baseUri("http://localhost:" + server.port()).get();
        WebClientResponse response = client.get().path("/greet").submit().toCompletableFuture().get();
        String greeting = response.content().as(String.class).toCompletableFuture().get();
        assertThat(greeting, is("Hello World!"));
    } finally {
        if (server != null) {
            server.shutdown();
        }
    }
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) WebServer(io.helidon.webserver.WebServer) WebClient(io.helidon.webclient.WebClient) Test(org.junit.jupiter.api.Test)

Example 20 with WebClient

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

the class MultiPortTest method compositeRedirectWebServer.

@Test
public void compositeRedirectWebServer() throws Exception {
    // start all of the servers
    webServer = WebServer.builder(Routing.builder().get("/foo", commonHandler)).tls(webServerTls).addSocket(SocketConfiguration.create("redirect")).addNamedRouting("redirect", Routing.builder().any((req, res) -> {
        res.status(Http.Status.MOVED_PERMANENTLY_301).headers().add(Http.Header.LOCATION, String.format("https://%s:%d%s", req.headers().first(Http.Header.HOST).map(s -> s.contains(":") ? s.subSequence(0, s.indexOf(":")) : s).orElseThrow(() -> new IllegalStateException("Header 'Host' not found!")), req.webServer().port(), req.path()));
        res.send();
    })).build();
    webServer.start().toCompletableFuture().join();
    WebClient webClient = WebClient.builder().tls(WebClientTls.builder().trustAll(true).build()).build();
    // Response response = client.target("http://localhost:" + webServer.port("redirect")).path("/foo").request()
    // .get();
    // assertThat("Unexpected response: " + response, response.getHeaderString("Location"),
    // AllOf.allOf(StringContains.containsString("https://localhost:"), StringContains.containsString
    // ("/foo")));
    // assertThat("Unexpected response: " + response, response.getStatus(), is(Http.Status.MOVED_PERMANENTLY_301
    // .code()));
    // 
    // assertResponse("https", webServer.port(), "/foo", is("Root! 1"));
    // 
    // Response responseRedirected = client.target(response.getHeaderString("Location")).request().get();
    // assertThat("Unexpected response: " + responseRedirected, responseRedirected.readEntity(String.class), is
    // ("Root! 2"));
    assertResponse("https", webServer.port(), "/foo", is("Root! 1"));
    webClient.get().uri("http://localhost:" + webServer.port("redirect")).path("/foo").request().thenApply(it -> {
        assertThat("Unexpected response: " + it, it.headers().first(Http.Header.LOCATION).get(), AllOf.allOf(StringContains.containsString("https://localhost:"), StringContains.containsString("/foo")));
        assertThat("Unexpected response: " + it, it.status(), is(Http.Status.MOVED_PERMANENTLY_301));
        return it;
    }).thenCompose(it -> webClient.get().uri(it.headers().first(Http.Header.LOCATION).get()).request(String.class)).thenAccept(it -> assertThat("Unexpected response: " + it, it, is("Root! 2"))).toCompletableFuture().get();
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) BeforeEach(org.junit.jupiter.api.BeforeEach) WebClientTls(io.helidon.webclient.WebClientTls) WebClient(io.helidon.webclient.WebClient) Config(io.helidon.config.Config) Resource(io.helidon.common.configurable.Resource) AllOf(org.hamcrest.core.AllOf) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) StringContains(org.hamcrest.core.StringContains) BeforeAll(org.junit.jupiter.api.BeforeAll) KeyConfig(io.helidon.common.pki.KeyConfig) Matcher(org.hamcrest.Matcher) Is.is(org.hamcrest.core.Is.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Http(io.helidon.common.http.Http) ConfigSources(io.helidon.config.ConfigSources) 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