use of io.helidon.webclient.WebClientResponse 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.WebClientResponse 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.WebClientResponse in project helidon by oracle.
the class ConfigTest method runWithConfig.
private JsonObject runWithConfig(String configKey, int expectedStatus) throws InterruptedException, ExecutionException, TimeoutException {
HealthSupport healthSupport = HealthSupport.builder().addLiveness(HealthChecks.healthChecks(testConfig.get(configKey + ".helidon.health"))).build();
WebServer webServer = null;
try {
webServer = startServer(healthSupport);
WebClientResponse response = webClientBuilder(webServer).build().get().accept(MediaType.APPLICATION_JSON).path("health/live").submit().await();
assertThat("Normal health URL HTTP response", response.status().code(), is(expectedStatus));
return response.content().as(JsonObject.class).await();
} finally {
if (webServer != null) {
shutdownServer(webServer);
}
}
}
use of io.helidon.webclient.WebClientResponse 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);
}
};
}
use of io.helidon.webclient.WebClientResponse in project helidon by oracle.
the class RestApiBase method requestBytesPayload.
/**
* Create a supplier for a response with publisher request.
* Defaults to "{@code () -> clientRequest.submit(publisher)}".
* 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 publisher publisher to be used as request entity
*
* @return supplier of a web client response
*/
protected Supplier<Single<WebClientResponse>> requestBytesPayload(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientRequestBuilder requestBuilder, Flow.Publisher<DataChunk> publisher) {
requestBuilder.accept(request.responseMediaType().orElse(MediaType.APPLICATION_JSON));
requestBuilder.contentType(request.requestMediaType().orElse(MediaType.APPLICATION_OCTET_STREAM));
AtomicBoolean updated = new AtomicBoolean();
return () -> {
if (updated.compareAndSet(false, true)) {
return updateRequestBuilderBytesPayload(requestBuilder, path, request, method, requestId).flatMapSingle(it -> it.submit(publisher));
} else {
return requestBuilder.submit(publisher);
}
};
}
Aggregations