use of io.helidon.common.http.Headers in project helidon by oracle.
the class AbstractCorsTest method test2PreFlightForbiddenHeader.
@Test
void test2PreFlightForbiddenHeader() throws ExecutionException, InterruptedException {
WebClientRequestBuilder reqBuilder = client().options().path(path(SERVICE_2));
Headers headers = reqBuilder.headers();
headers.add(ORIGIN, "http://foo.bar");
headers.add(ACCESS_CONTROL_REQUEST_METHOD, "PUT");
headers.add(ACCESS_CONTROL_REQUEST_HEADERS, "X-foo, X-bar, X-oops");
WebClientResponse res = reqBuilder.request().toCompletableFuture().get();
Http.ResponseStatus status = res.status();
assertThat(status.code(), is(Http.Status.FORBIDDEN_403.code()));
assertThat(status.reasonPhrase(), is("CORS headers not in allowed list"));
}
use of io.helidon.common.http.Headers in project helidon by oracle.
the class AbstractCorsTest method test2PreFlightAllowedHeaders2.
@Test
void test2PreFlightAllowedHeaders2() throws ExecutionException, InterruptedException {
WebClientRequestBuilder reqBuilder = client().options().path(path(SERVICE_2));
Headers headers = reqBuilder.headers();
headers.add(ORIGIN, "http://foo.bar");
headers.add(ACCESS_CONTROL_REQUEST_METHOD, "PUT");
headers.add(ACCESS_CONTROL_REQUEST_HEADERS, "X-foo, X-bar");
WebClientResponse res = reqBuilder.request().toCompletableFuture().get();
assertThat(res.status(), is(Http.Status.OK_200));
assertThat(res.headers().first(ACCESS_CONTROL_ALLOW_ORIGIN), present(is("http://foo.bar")));
assertThat(res.headers().first(ACCESS_CONTROL_ALLOW_CREDENTIALS), present(is("true")));
assertThat(res.headers().first(ACCESS_CONTROL_ALLOW_METHODS), present(is("PUT")));
assertThat(res.headers().first(ACCESS_CONTROL_ALLOW_HEADERS), present(containsString("X-foo")));
assertThat(res.headers().first(ACCESS_CONTROL_ALLOW_HEADERS), present(containsString("X-bar")));
assertThat(res.headers().first(ACCESS_CONTROL_MAX_AGE), notPresent());
}
use of io.helidon.common.http.Headers in project helidon by oracle.
the class AbstractCorsTest method test1ActualAllowedOrigin.
@Test
void test1ActualAllowedOrigin() throws ExecutionException, InterruptedException {
WebClientRequestBuilder reqBuilder = client().put().path(path(SERVICE_1)).contentType(MediaType.TEXT_PLAIN);
Headers headers = reqBuilder.headers();
headers.add(ORIGIN, "http://foo.bar");
headers.add(ACCESS_CONTROL_REQUEST_METHOD, "PUT");
WebClientResponse res = reqBuilder.submit("").toCompletableFuture().get();
assertThat(res.status(), is(Http.Status.OK_200));
assertThat(res.headers().first(ACCESS_CONTROL_ALLOW_ORIGIN), present(is("*")));
}
use of io.helidon.common.http.Headers in project helidon by oracle.
the class WebClientRequestBuilderImpl method invoke.
private Single<WebClientResponse> invoke(Flow.Publisher<DataChunk> requestEntity) {
finalUri = prepareFinalURI();
if (requestId == null) {
requestId = REQUEST_NUMBER.incrementAndGet();
}
// LOGGER.finest(() -> "(client reqID: " + requestId + ") Request final URI: " + uri);
CompletableFuture<WebClientServiceRequest> sent = new CompletableFuture<>();
CompletableFuture<WebClientServiceResponse> responseReceived = new CompletableFuture<>();
CompletableFuture<WebClientServiceResponse> complete = new CompletableFuture<>();
WebClientServiceRequest completedRequest = new WebClientServiceRequestImpl(this, sent, responseReceived, complete);
CompletionStage<WebClientServiceRequest> rcs = CompletableFuture.completedFuture(completedRequest);
for (WebClientService service : services) {
rcs = rcs.thenCompose(service::request).thenApply(servReq -> {
finalUri = recreateURI(servReq);
return servReq;
});
}
Single<WebClientResponse> single = Single.create(rcs.thenCompose(serviceRequest -> {
URI requestUri = relativizeNoProxy(finalUri, proxy, configuration.relativeUris());
requestId = serviceRequest.requestId();
HttpHeaders headers = toNettyHttpHeaders();
DefaultHttpRequest request = new DefaultHttpRequest(toNettyHttpVersion(httpVersion), toNettyMethod(method), requestUri.toASCIIString(), headers);
boolean keepAlive = HttpUtil.isKeepAlive(request);
requestConfiguration = RequestConfiguration.builder(finalUri).update(configuration).followRedirects(followRedirects).clientServiceRequest(serviceRequest).readerContext(readerContext).writerContext(writerContext).connectTimeout(connectTimeout).readTimeout(readTimeout).services(services).context(context).proxy(proxy).keepAlive(keepAlive).requestId(requestId).build();
WebClientRequestImpl clientRequest = new WebClientRequestImpl(this);
CompletableFuture<WebClientResponse> result = new CompletableFuture<>();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventGroup).channel(NioSocketChannel.class).handler(new NettyClientInitializer(requestConfiguration)).option(ChannelOption.SO_KEEPALIVE, keepAlive).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, (int) connectTimeout.toMillis());
ChannelFuture channelFuture = keepAlive ? obtainChannelFuture(requestConfiguration, bootstrap) : bootstrap.connect(finalUri.getHost(), finalUri.getPort());
channelFuture.addListener((ChannelFutureListener) future -> {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.finest(() -> "(client reqID: " + requestId + ") " + "Channel hashcode -> " + channelFuture.channel().hashCode());
}
channelFuture.channel().attr(REQUEST).set(clientRequest);
channelFuture.channel().attr(RESPONSE_RECEIVED).set(false);
channelFuture.channel().attr(RECEIVED).set(responseReceived);
channelFuture.channel().attr(COMPLETED).set(complete);
channelFuture.channel().attr(WILL_CLOSE).set(!keepAlive);
channelFuture.channel().attr(RESULT).set(result);
channelFuture.channel().attr(REQUEST_ID).set(requestId);
Throwable cause = future.cause();
if (null == cause) {
RequestContentSubscriber requestContentSubscriber = new RequestContentSubscriber(request, channelFuture.channel(), result, sent, allowChunkedEncoding);
requestEntity.subscribe(requestContentSubscriber);
} else {
sent.completeExceptionally(cause);
responseReceived.completeExceptionally(cause);
complete.completeExceptionally(cause);
result.completeExceptionally(new WebClientException(finalUri.toString(), cause));
}
});
return result;
}));
return wrapWithContext(single);
}
use of io.helidon.common.http.Headers in project helidon by oracle.
the class TestHandlerRegistration method test4PreFlightAllowedHeaders2.
@Test
void test4PreFlightAllowedHeaders2() throws ExecutionException, InterruptedException {
WebClientRequestBuilder reqBuilder = client.options().path(CORS4_CONTEXT_ROOT);
Headers headers = reqBuilder.headers();
headers.add(ORIGIN, "http://foo.bar");
headers.add(ACCESS_CONTROL_REQUEST_METHOD, "PUT");
headers.add(ACCESS_CONTROL_REQUEST_HEADERS, "X-foo, X-bar");
WebClientResponse res = reqBuilder.request().toCompletableFuture().get();
assertThat(res.status(), is(Http.Status.OK_200));
assertThat(res.headers().first(ACCESS_CONTROL_ALLOW_ORIGIN), present(is("http://foo.bar")));
assertThat(res.headers().first(ACCESS_CONTROL_ALLOW_METHODS), present(is("PUT")));
assertThat(res.headers().first(ACCESS_CONTROL_ALLOW_HEADERS), present(containsString("X-foo")));
assertThat(res.headers().first(ACCESS_CONTROL_ALLOW_HEADERS), present(containsString("X-bar")));
}
Aggregations