use of io.helidon.webclient.WebClient in project helidon by oracle.
the class MutualTlsTest method testOptionalAuthentication.
@Test
public void testOptionalAuthentication() {
WebClient webClient = createWebClient(CONFIG.get("no-client-cert"));
assertThat(executeRequest(webClient, "https", webServer.port("optional")), is("Hello Unknown CN!"));
webClient = createWebClient(CONFIG.get("success"));
assertThat(executeRequest(webClient, "https", webServer.port("optional")), is("Hello Helidon-client!"));
}
use of io.helidon.webclient.WebClient in project helidon by oracle.
the class MutualTlsTest method testServerCertInvalidCn.
@Test
public void testServerCertInvalidCn() {
int port = webServer.port("invalid-server-cert");
WebClient webClientOne = createWebClient(CONFIG.get("server-cert-invalid-cn"));
CompletionException exception = assertThrows(CompletionException.class, () -> executeRequest(webClientOne, "https", port));
assertThat(exception.getCause().getMessage(), is("No name matching localhost found"));
WebClient webClientTwo = createWebClient(CONFIG.get("client-disable-hostname-verification"));
assertThat(executeRequest(webClientTwo, "https", port), is("Hello Helidon-client!"));
}
use of io.helidon.webclient.WebClient in project helidon by oracle.
the class GreetService method contextCheck.
/**
* Checks the existence of a {@code Context} object in a WebClient thread.
*
* @param request the request
* @param response the response
*/
private void contextCheck(ServerRequest request, ServerResponse response) {
WebClient webClient = WebClient.builder().baseUri("http://localhost:" + Main.serverPort + "/").build();
Optional<Context> context = Contexts.context();
// Verify that context was propagated with auth enabled
if (context.isEmpty()) {
response.status(Http.Status.INTERNAL_SERVER_ERROR_500).send();
return;
}
// Register instance in context
context.get().register(this);
// Ensure context is available in webclient threads
webClient.get().request().thenAccept(clientResponse -> {
Context singleContext = Contexts.context().orElseThrow();
Objects.requireNonNull(singleContext.get(GreetService.class));
response.status(Http.Status.OK_200);
response.send();
}).exceptionally(throwable -> {
response.status(Http.Status.INTERNAL_SERVER_ERROR_500);
response.send();
return null;
});
}
use of io.helidon.webclient.WebClient in project helidon by oracle.
the class ConnectionCloseTest method testCutConnection.
@Test
void testCutConnection() throws ExecutionException, InterruptedException, TimeoutException {
WebClient webClient = createNewClient();
CompletableFuture<Throwable> actualErrorCf = new CompletableFuture<>();
// Expecting WebClientException: Connection reset by the host
webClient.get().path("/connectionClose").request().flatMap(WebClientResponse::content).map(DataChunk::bytes).map(String::new).log().onError(actualErrorCf::complete).ignoreElements();
Throwable actual = actualErrorCf.get(10, TimeUnit.SECONDS);
assertThat(actual, Matchers.instanceOf(WebClientException.class));
}
use of io.helidon.webclient.WebClient in project helidon by oracle.
the class TracingPropagationTest method testTracingSuccess.
@Test
void testTracingSuccess() throws ExecutionException, InterruptedException {
MockTracer mockTracer = new MockTracer();
WebServer webServer = Main.startServer(mockTracer).toCompletableFuture().get();
Context context = Context.builder().id("tracing-unit-test").build();
context.register(mockTracer);
String uri = "http://localhost:" + webServer.port() + "/greet";
WebClient client = WebClient.builder().baseUri(uri).context(context).config(Config.create().get("client")).build();
client.get().queryParam("some", "value").fragment("fragment").request().thenCompose(WebClientResponse::close).toCompletableFuture().get();
TimeUnit.MILLISECONDS.sleep(1);
List<MockSpan> mockSpans = mockTracer.finishedSpans();
assertThat("At least one client and one server span expected", mockSpans.size(), greaterThanOrEqualTo(2));
// we need the first span - parentId 0
MockSpan clientSpan = findSpanWithParentId(mockSpans, 0);
assertThat(clientSpan.operationName(), is("GET-" + uri));
List<MockSpan.LogEntry> logEntries = clientSpan.logEntries();
assertThat(logEntries, empty());
Map<String, Object> tags = clientSpan.tags();
assertThat(tags.get(Tags.HTTP_STATUS.getKey()), is(200));
// now we want to test first child - first WebServer span
MockSpan wsSpan = findSpanWithParentId(mockSpans, clientSpan.context().spanId());
assertThat(wsSpan.operationName(), is("HTTP Request"));
tags = wsSpan.tags();
assertThat(tags.get(Tags.HTTP_METHOD.getKey()), is("GET"));
assertThat(tags.get(Tags.HTTP_URL.getKey()), is("/greet?some=value#fragment"));
assertThat(tags.get(Tags.HTTP_STATUS.getKey()), is(200));
assertThat(tags.get(Tags.COMPONENT.getKey()), is("helidon-webserver"));
webServer.shutdown().toCompletableFuture().get();
}
Aggregations