Search in sources :

Example 41 with WebClientResponse

use of io.helidon.webclient.WebClientResponse 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)

Example 42 with WebClientResponse

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

the class RequestTest method testFollowRedirect.

@Test
public void testFollowRedirect() throws ExecutionException, InterruptedException {
    webClient.get().path("/redirect").request(JsonObject.class).thenAccept(jsonObject -> Assertions.assertEquals("Hello World!", jsonObject.getString("message"))).toCompletableFuture().get();
    WebClientResponse response = webClient.get().path("/redirect").followRedirects(false).request().toCompletableFuture().get();
    assertThat(response.status(), is(Http.Status.MOVED_PERMANENTLY_301));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) JsonObject(jakarta.json.JsonObject) Test(org.junit.jupiter.api.Test)

Example 43 with WebClientResponse

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

the class Gh2631Test method testFileNoFallbackMissing.

@Test
void testFileNoFallbackMissing() {
    WebClientResponse response = getResponse("/simpleFile/second/");
    assertThat(response.status(), is(Http.Status.NOT_FOUND_404));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Test(org.junit.jupiter.api.Test)

Example 44 with WebClientResponse

use of io.helidon.webclient.WebClientResponse 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 45 with WebClientResponse

use of io.helidon.webclient.WebClientResponse 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)

Aggregations

WebClientResponse (io.helidon.webclient.WebClientResponse)120 Test (org.junit.jupiter.api.Test)85 WebClientRequestBuilder (io.helidon.webclient.WebClientRequestBuilder)38 Headers (io.helidon.common.http.Headers)24 WebClient (io.helidon.webclient.WebClient)24 JsonObject (jakarta.json.JsonObject)22 Order (org.junit.jupiter.api.Order)16 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)16 Http (io.helidon.common.http.Http)15 DataChunk (io.helidon.common.http.DataChunk)10 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)8 Single (io.helidon.common.reactive.Single)7 WebServer (io.helidon.webserver.WebServer)7 Logger (java.util.logging.Logger)7 Context (io.helidon.common.context.Context)6 MediaType (io.helidon.common.http.MediaType)6 URI (java.net.URI)6 Optional (java.util.Optional)6 Contexts (io.helidon.common.context.Contexts)5 Multi (io.helidon.common.reactive.Multi)5