use of io.helidon.webclient.WebClient in project helidon by oracle.
the class WebclientServiceValuePropagationTest method testInvalidSchema.
@Test
public void testInvalidSchema() {
WebClient webClient = WebClient.builder().baseUri("http://localhost:80").addService(new InvalidSchemaService()).build();
CompletionException exception = assertThrows(CompletionException.class, () -> webClient.get().request(String.class).await());
assertThat(exception.getCause().getMessage(), is("invalid transport protocol is not supported!"));
}
use of io.helidon.webclient.WebClient in project helidon by oracle.
the class TestDefaultCorsSupport method testOptionsWithCors.
@Test
void testOptionsWithCors() throws ExecutionException, InterruptedException {
WebServer server = null;
WebClient client;
try {
server = WebServer.create(prepRouting(true)).start().toCompletableFuture().get();
client = WebClient.builder().baseUri("http://localhost:" + server.port()).get();
WebClientRequestBuilder reqBuilder = client.options().path("/greet");
Headers h = reqBuilder.headers();
h.add("Origin", "http://foo.com");
h.add("Host", "bar.com");
WebClientResponse response = reqBuilder.submit().toCompletableFuture().get();
WebClientResponseHeaders headers = response.headers();
List<String> allowOrigins = headers.values(CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN);
assertThat(allowOrigins, contains("*"));
} finally {
if (server != null) {
server.shutdown();
}
}
}
use of io.helidon.webclient.WebClient in project helidon by oracle.
the class TestDefaultCorsSupport method testOptionsWithoutCors.
@Test
void testOptionsWithoutCors() throws ExecutionException, InterruptedException {
WebServer server = null;
WebClient client;
try {
server = WebServer.create(prepRouting(false)).start().toCompletableFuture().get();
client = WebClient.builder().baseUri("http://localhost:" + server.port()).get();
WebClientRequestBuilder reqBuilder = client.options().path("/greet");
Headers h = reqBuilder.headers();
h.add("Origin", "http://foo.com");
h.add("Host", "bar.com");
WebClientResponse response = reqBuilder.submit().toCompletableFuture().get();
WebClientResponseHeaders headers = response.headers();
List<String> allowOrigins = headers.values(CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN);
assertThat(allowOrigins.size(), is(0));
} finally {
if (server != null) {
server.shutdown();
}
}
}
use of io.helidon.webclient.WebClient in project helidon by oracle.
the class TestDefaultCorsSupport method testGetWithoutCors.
@Test
void testGetWithoutCors() throws ExecutionException, InterruptedException {
WebServer server = null;
WebClient client;
try {
server = WebServer.create(prepRouting(false)).start().toCompletableFuture().get();
client = WebClient.builder().baseUri("http://localhost:" + server.port()).get();
WebClientResponse response = client.get().path("/greet").submit().toCompletableFuture().get();
String greeting = response.content().as(String.class).toCompletableFuture().get();
assertThat(greeting, is("Hello World!"));
} finally {
if (server != null) {
server.shutdown();
}
}
}
use of io.helidon.webclient.WebClient 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();
}
Aggregations