Search in sources :

Example 11 with Http

use of io.helidon.common.http.Http in project helidon by oracle.

the class TestUtil method prepRouting.

static Routing.Builder prepRouting() {
    CrossOriginConfig cors3COC = CrossOriginConfig.builder().allowOrigins("http://foo.bar", "http://bar.foo").allowMethods("DELETE", "PUT").build();
    /*
         * Use the default config for the service at "/greet" and then programmatically add the config for /cors3.
         */
    CorsSupport.Builder corsSupportBuilder = CorsSupport.builder().name("CORS SE " + SERVICE_3);
    corsSupportBuilder.addCrossOrigin(SERVICE_3.path(), cors3COC);
    /*
         * Use the loaded config to build a CrossOriginConfig for /cors4.
         */
    /*
         * Load a specific config for "/othergreet."
         */
    Config twoCORSConfig = minimalConfig(ConfigSources.classpath("twoCORS.yaml"));
    Config twoCORSMappedConfig = twoCORSConfig.get("cors-2-setup");
    if (!twoCORSMappedConfig.exists()) {
        throw new IllegalArgumentException("Expected config 'cors-2-setup' from twoCORS.yaml not found");
    }
    Config somewhatRestrictedConfig = twoCORSConfig.get("somewhat-restrictive");
    if (!somewhatRestrictedConfig.exists()) {
        throw new IllegalArgumentException("Expected config 'somewhat-restrictive' from twoCORS.yaml not found");
    }
    Config corsMappedSetupConfig = Config.create().get("cors-setup");
    if (!corsMappedSetupConfig.exists()) {
        throw new IllegalArgumentException("Expected config 'cors-setup' from default app config not found");
    }
    return Routing.builder().register(GREETING_PATH, CorsSupport.createMapped(corsMappedSetupConfig), new GreetService()).register(OTHER_GREETING_PATH, CorsSupport.createMapped(twoCORSMappedConfig), new GreetService("Other Hello")).any(TestHandlerRegistration.CORS4_CONTEXT_ROOT, // handler settings from config subnode
    CorsSupport.create(somewhatRestrictedConfig), (req, resp) -> resp.status(Http.Status.OK_200).send()).get(// handler settings in-line
    TestHandlerRegistration.CORS4_CONTEXT_ROOT, CorsSupport.builder().allowOrigins("*").allowMethods("GET").build(), (req, resp) -> resp.status(Http.Status.OK_200).send());
}
Also used : WebClient(io.helidon.webclient.WebClient) Config(io.helidon.config.Config) ConfigSource(io.helidon.config.spi.ConfigSource) TimeoutException(java.util.concurrent.TimeoutException) Supplier(java.util.function.Supplier) CORSTestService(io.helidon.webserver.cors.CorsTestServices.CORSTestService) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) SERVICE_3(io.helidon.webserver.cors.CorsTestServices.SERVICE_3) WebServer(io.helidon.webserver.WebServer) Http(io.helidon.common.http.Http) ConfigSources(io.helidon.config.ConfigSources) Routing(io.helidon.webserver.Routing) Config(io.helidon.config.Config)

Example 12 with Http

use of io.helidon.common.http.Http 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 13 with Http

use of io.helidon.common.http.Http in project helidon by oracle.

the class MultiPortTest method compositeRedirectWebServer.

@Test
public void compositeRedirectWebServer() throws Exception {
    // start all of the servers
    webServer = WebServer.builder(Routing.builder().get("/foo", commonHandler)).tls(webServerTls).addSocket(SocketConfiguration.create("redirect")).addNamedRouting("redirect", Routing.builder().any((req, res) -> {
        res.status(Http.Status.MOVED_PERMANENTLY_301).headers().add(Http.Header.LOCATION, String.format("https://%s:%d%s", req.headers().first(Http.Header.HOST).map(s -> s.contains(":") ? s.subSequence(0, s.indexOf(":")) : s).orElseThrow(() -> new IllegalStateException("Header 'Host' not found!")), req.webServer().port(), req.path()));
        res.send();
    })).build();
    webServer.start().toCompletableFuture().join();
    WebClient webClient = WebClient.builder().tls(WebClientTls.builder().trustAll(true).build()).build();
    // Response response = client.target("http://localhost:" + webServer.port("redirect")).path("/foo").request()
    // .get();
    // assertThat("Unexpected response: " + response, response.getHeaderString("Location"),
    // AllOf.allOf(StringContains.containsString("https://localhost:"), StringContains.containsString
    // ("/foo")));
    // assertThat("Unexpected response: " + response, response.getStatus(), is(Http.Status.MOVED_PERMANENTLY_301
    // .code()));
    // 
    // assertResponse("https", webServer.port(), "/foo", is("Root! 1"));
    // 
    // Response responseRedirected = client.target(response.getHeaderString("Location")).request().get();
    // assertThat("Unexpected response: " + responseRedirected, responseRedirected.readEntity(String.class), is
    // ("Root! 2"));
    assertResponse("https", webServer.port(), "/foo", is("Root! 1"));
    webClient.get().uri("http://localhost:" + webServer.port("redirect")).path("/foo").request().thenApply(it -> {
        assertThat("Unexpected response: " + it, it.headers().first(Http.Header.LOCATION).get(), AllOf.allOf(StringContains.containsString("https://localhost:"), StringContains.containsString("/foo")));
        assertThat("Unexpected response: " + it, it.status(), is(Http.Status.MOVED_PERMANENTLY_301));
        return it;
    }).thenCompose(it -> webClient.get().uri(it.headers().first(Http.Header.LOCATION).get()).request(String.class)).thenAccept(it -> assertThat("Unexpected response: " + it, it, is("Root! 2"))).toCompletableFuture().get();
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) BeforeEach(org.junit.jupiter.api.BeforeEach) WebClientTls(io.helidon.webclient.WebClientTls) WebClient(io.helidon.webclient.WebClient) Config(io.helidon.config.Config) Resource(io.helidon.common.configurable.Resource) AllOf(org.hamcrest.core.AllOf) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) StringContains(org.hamcrest.core.StringContains) BeforeAll(org.junit.jupiter.api.BeforeAll) KeyConfig(io.helidon.common.pki.KeyConfig) Matcher(org.hamcrest.Matcher) Is.is(org.hamcrest.core.Is.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Http(io.helidon.common.http.Http) ConfigSources(io.helidon.config.ConfigSources) WebClient(io.helidon.webclient.WebClient) Test(org.junit.jupiter.api.Test)

Example 14 with Http

use of io.helidon.common.http.Http in project helidon by oracle.

the class Status204Test method callPutAndGet.

@Test
void callPutAndGet() throws Exception {
    WebClient webClient = WebClient.builder().baseUri("http://localhost:" + server.port()).build();
    webClient.put().submit("test call").thenAccept(it -> assertThat(it.status(), is(Http.Status.NO_CONTENT_204))).thenCompose(it -> webClient.get().request(String.class)).thenAccept(it -> assertThat(it, is("test"))).toCompletableFuture().get();
}
Also used : Test(org.junit.jupiter.api.Test) CoreMatchers.is(org.hamcrest.CoreMatchers.is) BeforeEach(org.junit.jupiter.api.BeforeEach) AfterEach(org.junit.jupiter.api.AfterEach) WebClient(io.helidon.webclient.WebClient) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Http(io.helidon.common.http.Http) WebClient(io.helidon.webclient.WebClient) Test(org.junit.jupiter.api.Test)

Example 15 with Http

use of io.helidon.common.http.Http in project helidon by oracle.

the class TestHttpParseFineTuning method testDefaults.

@Test
void testDefaults() {
    // default is 8Kb for headers
    // and 4096 for initial line
    WebServer ws = WebServer.builder().host("localhost").routing(Routing.builder().register("/static", StaticContentSupport.create("/static")).any((req, res) -> res.send("any")).build()).build().start().await(10, TimeUnit.SECONDS);
    WebClient client = WebClient.builder().baseUri("http://localhost:" + ws.port()).validateHeaders(false).build();
    testHeader(client, 8000, true);
    testInitialLine(client, 10, true);
    testHeader(client, 8900, false);
    testHeader(client, 8900, false);
    // now test with big initial line
    testInitialLine(client, 5000, false);
    testHeaderName(client, "X_HEADER", true);
    testHeaderName(client, "X\tHEADER", false);
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CoreMatchers.is(org.hamcrest.CoreMatchers.is) WebClient(io.helidon.webclient.WebClient) Config(io.helidon.config.Config) WebClientResponse(io.helidon.webclient.WebClientResponse) Map(java.util.Map) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Http(io.helidon.common.http.Http) ConfigSources(io.helidon.config.ConfigSources) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) WebClient(io.helidon.webclient.WebClient) Test(org.junit.jupiter.api.Test)

Aggregations

Http (io.helidon.common.http.Http)27 WebClient (io.helidon.webclient.WebClient)16 DataChunk (io.helidon.common.http.DataChunk)12 Config (io.helidon.config.Config)12 WebClientResponse (io.helidon.webclient.WebClientResponse)12 Routing (io.helidon.webserver.Routing)11 WebServer (io.helidon.webserver.WebServer)10 Test (org.junit.jupiter.api.Test)10 MediaType (io.helidon.common.http.MediaType)9 Optional (java.util.Optional)9 Single (io.helidon.common.reactive.Single)8 WebClientRequestBuilder (io.helidon.webclient.WebClientRequestBuilder)8 Logger (java.util.logging.Logger)8 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)8 Json (jakarta.json.Json)7 JsonBuilderFactory (jakarta.json.JsonBuilderFactory)7 Collections (java.util.Collections)7 JsonpSupport (io.helidon.media.jsonp.JsonpSupport)6 SecurityContext (io.helidon.security.SecurityContext)6 JsonObject (jakarta.json.JsonObject)5