Search in sources :

Example 26 with WebClientResponse

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

the class BasicExampleTest method testProtectedDenied.

private void testProtectedDenied(String uri, String username, String password) {
    WebClientResponse response = callProtected(uri, username, password);
    assertThat(response.status(), is(Http.Status.FORBIDDEN_403));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse)

Example 27 with WebClientResponse

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

the class MainTest method testTimeout.

@Test
void testTimeout() {
    String response = client.get().path("/timeout/50").request(String.class).await(1, TimeUnit.SECONDS);
    assertThat(response, is("Slept for 50 ms"));
    WebClientResponse clientResponse = client.get().path("/timeout/105").request().await(1, TimeUnit.SECONDS);
    response = clientResponse.content().as(String.class).await(1, TimeUnit.SECONDS);
    // error handler specified in Main
    assertThat(clientResponse.status(), is(Http.Status.REQUEST_TIMEOUT_408));
    assertThat(response, is("timeout"));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Test(org.junit.jupiter.api.Test)

Example 28 with WebClientResponse

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

the class MainTest method testCircuitBreaker.

@Test
void testCircuitBreaker() {
    String response = client.get().path("/circuitBreaker/true").request(String.class).await(1, TimeUnit.SECONDS);
    assertThat(response, is("blocked for 100 millis"));
    // error ratio is 20% within 10 request
    client.get().path("/circuitBreaker/false").request().await(1, TimeUnit.SECONDS);
    // should work after first
    response = client.get().path("/circuitBreaker/true").request(String.class).await(1, TimeUnit.SECONDS);
    assertThat(response, is("blocked for 100 millis"));
    // should open after second
    client.get().path("/circuitBreaker/false").request().await(1, TimeUnit.SECONDS);
    WebClientResponse clientResponse = client.get().path("/circuitBreaker/true").request().await(1, TimeUnit.SECONDS);
    response = clientResponse.content().as(String.class).await(1, TimeUnit.SECONDS);
    // registered an error handler in Main
    assertThat(clientResponse.status(), is(Http.Status.SERVICE_UNAVAILABLE_503));
    assertThat(response, is("circuit breaker"));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Test(org.junit.jupiter.api.Test)

Example 29 with WebClientResponse

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

the class HelidonConnector method applyInternal.

private CompletionStage<ClientResponse> applyInternal(ClientRequest request) {
    final WebClientRequestBuilder webClientRequestBuilder = webClient.method(request.getMethod());
    webClientRequestBuilder.uri(request.getUri());
    webClientRequestBuilder.headers(HelidonStructures.createHeaders(request.getRequestHeaders()));
    for (String propertyName : request.getConfiguration().getPropertyNames()) {
        Object property = request.getConfiguration().getProperty(propertyName);
        if (!propertyName.startsWith("jersey") && String.class.isInstance(property)) {
            webClientRequestBuilder.property(propertyName, (String) property);
        }
    }
    for (String propertyName : request.getPropertyNames()) {
        Object property = request.resolveProperty(propertyName, Object.class);
        if (!propertyName.startsWith("jersey") && String.class.isInstance(property)) {
            webClientRequestBuilder.property(propertyName, (String) property);
        }
    }
    // TODO
    // HelidonStructures.createProxy(request).ifPresent(webClientRequestBuilder::proxy);
    webClientRequestBuilder.followRedirects(request.resolveProperty(ClientProperties.FOLLOW_REDIRECTS, true));
    webClientRequestBuilder.readTimeout(request.resolveProperty(ClientProperties.READ_TIMEOUT, 10000), TimeUnit.MILLISECONDS);
    CompletionStage<WebClientResponse> responseStage = null;
    if (request.hasEntity()) {
        responseStage = HelidonEntity.submit(entityType, request, webClientRequestBuilder, executorServiceKeeper.getExecutorService(request));
    } else {
        responseStage = webClientRequestBuilder.submit();
    }
    return responseStage.thenCompose((a) -> convertResponse(request, a));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder)

Example 30 with WebClientResponse

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

the class HelidonEntity method submit.

/**
 * Convert Jersey {@code OutputStream} to an entity based on the client request use case and submits to the provided
 * {@code WebClientRequestBuilder}.
 * @param type the type of the Helidon entity.
 * @param requestContext Jersey {@link ClientRequest} providing the entity {@code OutputStream}.
 * @param requestBuilder Helidon {@code WebClientRequestBuilder} which is used to submit the entity
 * @param executorService {@link ExecutorService} that fills the entity instance for Helidon with data from Jersey
 *                      {@code OutputStream}.
 * @return Helidon Client response completion stage.
 */
static CompletionStage<WebClientResponse> submit(HelidonEntityType type, ClientRequest requestContext, WebClientRequestBuilder requestBuilder, ExecutorService executorService) {
    CompletionStage<WebClientResponse> stage = null;
    if (type != null) {
        final int bufferSize = requestContext.resolveProperty(ClientProperties.OUTBOUND_CONTENT_LENGTH_BUFFER, 8192);
        switch(type) {
            case BYTE_ARRAY_OUTPUT_STREAM:
                final ByteArrayOutputStream baos = new ByteArrayOutputStream(bufferSize);
                requestContext.setStreamProvider(contentLength -> baos);
                ((ProcessingRunnable) requestContext::writeEntity).run();
                stage = requestBuilder.submit(baos);
                break;
            case READABLE_BYTE_CHANNEL:
                final OutputStreamChannel channel = new OutputStreamChannel(bufferSize);
                requestContext.setStreamProvider(contentLength -> channel);
                executorService.execute((ProcessingRunnable) requestContext::writeEntity);
                stage = requestBuilder.submit(channel);
                break;
            case OUTPUT_STREAM_MULTI:
                final OutputStreamMulti publisher = IoMulti.outputStreamMulti();
                requestContext.setStreamProvider(contentLength -> publisher);
                executorService.execute((ProcessingRunnable) () -> {
                    requestContext.writeEntity();
                    publisher.close();
                });
                stage = requestBuilder.submit(Multi.create(publisher).map(DataChunk::create));
                break;
            default:
        }
    }
    return stage;
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) OutputStreamMulti(io.helidon.common.reactive.OutputStreamMulti) DataChunk(io.helidon.common.http.DataChunk) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

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