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("*"));
}
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("*"));
}
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"));
}
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));
}
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);
}
};
}
Aggregations