Search in sources :

Example 1 with WebClientRequestBuilder

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

the class TestCORS method testNamedGreetWithCors.

// Run after CORS test changes greeting to Cheers.
@Order(12)
@Test
void testNamedGreetWithCors() {
    WebClientRequestBuilder builder = client.get();
    Headers headers = builder.headers();
    headers.add("Origin", "http://foo.com");
    headers.add("Host", "here.com");
    WebClientResponse r = getResponse("/greet/Maria", builder);
    assertThat("HTTP response", r.status().code(), is(200));
    assertThat(fromPayload(r), containsString("Cheers Maria"));
    headers = r.headers();
    Optional<String> allowOrigin = headers.value(CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN);
    assertThat("Expected CORS header " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN + " presence check", allowOrigin.isPresent(), is(true));
    assertThat(allowOrigin.get(), is("*"));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Headers(io.helidon.common.http.Headers) Matchers.containsString(org.hamcrest.Matchers.containsString) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Example 2 with WebClientRequestBuilder

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

the class TestCORS method testAnonymousGreetWithCors.

// Run after the non-CORS tests (so the greeting is Hola) but before the CORS test that changes the greeting again.
@Order(10)
@Test
void testAnonymousGreetWithCors() {
    WebClientRequestBuilder builder = client.get();
    Headers headers = builder.headers();
    headers.add("Origin", "http://foo.com");
    headers.add("Host", "here.com");
    WebClientResponse r = getResponse("/greet", builder);
    assertThat("HTTP response", r.status().code(), is(200));
    String payload = fromPayload(r);
    assertThat("HTTP response payload", payload, is("Hola World!"));
    headers = r.headers();
    Optional<String> allowOrigin = headers.value(CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN);
    assertThat("Expected CORS header " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN + " is present", allowOrigin.isPresent(), is(true));
    assertThat("CORS header " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN, allowOrigin.get(), is("*"));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Headers(io.helidon.common.http.Headers) Matchers.containsString(org.hamcrest.Matchers.containsString) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Example 3 with WebClientRequestBuilder

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

the class TestCORS method testGreetingChangeWithCors.

// Run after the non-CORS tests but before other CORS tests.
@Order(11)
@Test
void testGreetingChangeWithCors() {
    // Send the pre-flight request and check the response.
    WebClientRequestBuilder builder = client.method("OPTIONS");
    Headers headers = builder.headers();
    headers.add("Origin", "http://foo.com");
    headers.add("Host", "here.com");
    headers.add("Access-Control-Request-Method", "PUT");
    WebClientResponse r = builder.path("/greet/greeting").submit().await();
    assertThat("pre-flight status", r.status().code(), is(200));
    Headers preflightResponseHeaders = r.headers();
    List<String> allowMethods = preflightResponseHeaders.values(CrossOriginConfig.ACCESS_CONTROL_ALLOW_METHODS);
    assertThat("pre-flight response check for " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_METHODS, allowMethods, is(not(empty())));
    assertThat("Header " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_METHODS, allowMethods, contains("PUT"));
    List<String> allowOrigins = preflightResponseHeaders.values(CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN);
    assertThat("pre-flight response check for " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN, allowOrigins, is(not(empty())));
    assertThat("Header " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN, allowOrigins, contains("http://foo.com"));
    // Send the follow-up request.
    builder = client.put();
    headers = builder.headers();
    headers.add("Origin", "http://foo.com");
    headers.add("Host", "here.com");
    headers.addAll(preflightResponseHeaders);
    r = putResponse("/greet/greeting", "Cheers", builder);
    assertThat("HTTP response3", r.status().code(), is(204));
    headers = r.headers();
    allowOrigins = headers.values(CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN);
    assertThat("Expected CORS header " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN, allowOrigins, is(not(empty())));
    assertThat("Header " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN, allowOrigins, contains("http://foo.com"));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Headers(io.helidon.common.http.Headers) Matchers.containsString(org.hamcrest.Matchers.containsString) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Example 4 with WebClientRequestBuilder

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

the class TestCORS method testGreetingChangeWithCorsAndOtherOrigin.

// After all other tests so we can rely on deterministic greetings.
@Order(100)
@Test
void testGreetingChangeWithCorsAndOtherOrigin() {
    WebClientRequestBuilder builder = client.put();
    Headers headers = builder.headers();
    headers.add("Origin", "http://other.com");
    headers.add("Host", "here.com");
    WebClientResponse r = putResponse("/greet/greeting", "Ahoy", builder);
    // Result depends on whether we are using overrides or not.
    boolean isOverriding = Config.create().get("cors").exists();
    assertThat("HTTP response3", r.status().code(), is(isOverriding ? 204 : 403));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Headers(io.helidon.common.http.Headers) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Example 5 with WebClientRequestBuilder

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

the class RestApiBase method requestJsonPayload.

/**
 * Create a supplier for a response with JSON request.
 * Defaults to "{@code () -> clientRequest.submit(jsonObject)}".
 * Also configures content type and accept headers.
 *
 * @param path path requested
 * @param request API request
 * @param method HTTP method
 * @param requestId ID of this request
 * @param requestBuilder {@link io.helidon.webclient.WebClient} request builder
 * @param jsonObject JSON object that should be sent as a request entity
 *
 * @return supplier of a web client response
 */
protected Supplier<Single<WebClientResponse>> requestJsonPayload(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientRequestBuilder requestBuilder, JsonObject jsonObject) {
    requestBuilder.accept(request.responseMediaType().orElse(MediaType.APPLICATION_JSON));
    requestBuilder.contentType(request.requestMediaType().orElse(MediaType.APPLICATION_JSON));
    AtomicBoolean updated = new AtomicBoolean();
    return () -> {
        // we should only update request builder once - if a retry is done, it should not be reset
        if (updated.compareAndSet(false, true)) {
            Single<WebClientRequestBuilder> res = updateRequestBuilder(requestBuilder, path, request, method, requestId, jsonObject);
            return res.flatMapSingle(it -> it.submit(jsonObject));
        } else {
            return requestBuilder.submit(jsonObject);
        }
    };
}
Also used : JsonWriterFactory(jakarta.json.JsonWriterFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WebClient(io.helidon.webclient.WebClient) WebClientResponse(io.helidon.webclient.WebClientResponse) JsonReaderFactory(jakarta.json.JsonReaderFactory) DataChunk(io.helidon.common.http.DataChunk) JsonBuilderFactory(jakarta.json.JsonBuilderFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Supplier(java.util.function.Supplier) MediaType(io.helidon.common.http.MediaType) Flow(java.util.concurrent.Flow) JsonObject(jakarta.json.JsonObject) Single(io.helidon.common.reactive.Single) Http(io.helidon.common.http.Http) Multi(io.helidon.common.reactive.Multi) FtHandler(io.helidon.faulttolerance.FtHandler) Collector(io.helidon.common.reactive.Collector) UUID(java.util.UUID) Logger(java.util.logging.Logger) WebClientRequestHeaders(io.helidon.webclient.WebClientRequestHeaders) Contexts(io.helidon.common.context.Contexts) SpanContext(io.opentracing.SpanContext) StringReader(java.io.StringReader) Optional(java.util.Optional) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Single(io.helidon.common.reactive.Single)

Aggregations

WebClientRequestBuilder (io.helidon.webclient.WebClientRequestBuilder)50 WebClientResponse (io.helidon.webclient.WebClientResponse)37 Test (org.junit.jupiter.api.Test)34 Headers (io.helidon.common.http.Headers)24 Http (io.helidon.common.http.Http)9 WebClient (io.helidon.webclient.WebClient)9 JsonObject (jakarta.json.JsonObject)8 Order (org.junit.jupiter.api.Order)8 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)8 Single (io.helidon.common.reactive.Single)7 Logger (java.util.logging.Logger)7 Optional (java.util.Optional)6 JsonBuilderFactory (jakarta.json.JsonBuilderFactory)5 Contexts (io.helidon.common.context.Contexts)4 Config (io.helidon.config.Config)4 URI (java.net.URI)4 DataChunk (io.helidon.common.http.DataChunk)3 MediaType (io.helidon.common.http.MediaType)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Flow (java.util.concurrent.Flow)3