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