Search in sources :

Example 11 with Headers

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"));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Headers(io.helidon.common.http.Headers) Http(io.helidon.common.http.Http) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) Test(org.junit.jupiter.api.Test)

Example 12 with Headers

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());
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Headers(io.helidon.common.http.Headers) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) Test(org.junit.jupiter.api.Test)

Example 13 with Headers

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("*")));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Headers(io.helidon.common.http.Headers) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) Test(org.junit.jupiter.api.Test)

Example 14 with Headers

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);
}
Also used : AttributeKey(io.netty.util.AttributeKey) Arrays(java.util.Arrays) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) DataChunk(io.helidon.common.http.DataChunk) MessageBodyReadableContent(io.helidon.media.common.MessageBodyReadableContent) AsciiString(io.netty.util.AsciiString) MessageBodyWriterContext(io.helidon.media.common.MessageBodyWriterContext) MediaType(io.helidon.common.http.MediaType) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) Flow(java.util.concurrent.Flow) Duration(java.time.Duration) Map(java.util.Map) Parameters(io.helidon.common.http.Parameters) URI(java.net.URI) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Logger(java.util.logging.Logger) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Objects(java.util.Objects) MessageBodyReaderContext(io.helidon.media.common.MessageBodyReaderContext) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Optional(java.util.Optional) Headers(io.helidon.common.http.Headers) HttpRequest(io.helidon.common.http.HttpRequest) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) HttpVersion(io.netty.handler.codec.http.HttpVersion) ChannelOption(io.netty.channel.ChannelOption) Context(io.helidon.common.context.Context) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) StringTokenizer(java.util.StringTokenizer) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Single(io.helidon.common.reactive.Single) Http(io.helidon.common.http.Http) WebClientService(io.helidon.webclient.spi.WebClientService) HttpHeaderValues(io.netty.handler.codec.http.HttpHeaderValues) HttpMethod(io.netty.handler.codec.http.HttpMethod) IOException(java.io.IOException) GenericType(io.helidon.common.GenericType) Contexts(io.helidon.common.context.Contexts) ChannelFuture(io.netty.channel.ChannelFuture) TimeUnit(java.util.concurrent.TimeUnit) Channel(io.netty.channel.Channel) AtomicLong(java.util.concurrent.atomic.AtomicLong) Bootstrap(io.netty.bootstrap.Bootstrap) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) Collections(java.util.Collections) HttpUtil(io.netty.handler.codec.http.HttpUtil) ChannelFuture(io.netty.channel.ChannelFuture) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) WebClientService(io.helidon.webclient.spi.WebClientService) URI(java.net.URI) ChannelFutureListener(io.netty.channel.ChannelFutureListener) CompletableFuture(java.util.concurrent.CompletableFuture) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) Bootstrap(io.netty.bootstrap.Bootstrap)

Example 15 with Headers

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")));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Headers(io.helidon.common.http.Headers) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

Headers (io.helidon.common.http.Headers)25 WebClientRequestBuilder (io.helidon.webclient.WebClientRequestBuilder)24 WebClientResponse (io.helidon.webclient.WebClientResponse)24 Test (org.junit.jupiter.api.Test)23 Order (org.junit.jupiter.api.Order)8 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)8 Http (io.helidon.common.http.Http)4 Matchers.containsString (org.hamcrest.Matchers.containsString)3 WebClient (io.helidon.webclient.WebClient)2 WebClientResponseHeaders (io.helidon.webclient.WebClientResponseHeaders)2 WebServer (io.helidon.webserver.WebServer)2 GenericType (io.helidon.common.GenericType)1 Context (io.helidon.common.context.Context)1 Contexts (io.helidon.common.context.Contexts)1 DataChunk (io.helidon.common.http.DataChunk)1 HttpRequest (io.helidon.common.http.HttpRequest)1 MediaType (io.helidon.common.http.MediaType)1 Parameters (io.helidon.common.http.Parameters)1 Single (io.helidon.common.reactive.Single)1 MessageBodyReadableContent (io.helidon.media.common.MessageBodyReadableContent)1