package com.mailserver.config; import com.mailserver.smtp.SmtpTls; import io.netty.handler.ssl.SslContextBuilder; import io.netty.handler.ssl.util.InsecureTrustManagerFactory; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import org.springframework.core.io.buffer.DefaultDataBufferFactory; import org.springframework.http.HttpStatus; import org.springframework.http.server.reactive.HttpHandler; import reactor.core.publisher.Mono; import reactor.netty.http.client.HttpClient; import java.nio.charset.StandardCharsets; import java.nio.file.Path; import static org.assertj.core.api.Assertions.assertThat; /** https://mail.jambocheck.com: the web client over HTTPS with the mail certificate. */ class HttpsConfigTest { @Test void servesTheWebAppOverTls(@TempDir Path dir) throws Exception { MailServerConfig.SmtpTlsContext tls = new MailServerConfig.SmtpTlsContext(SmtpTls.serverContext("", "", dir, "localhost")); HttpHandler handler = (request, response) -> { response.setStatusCode(HttpStatus.OK); return response.writeWith(Mono.just(DefaultDataBufferFactory.sharedInstance .wrap(("hello " + request.getURI().getScheme()).getBytes(StandardCharsets.UTF_8)))); }; io.netty.handler.ssl.SslContext trustAll = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build(); HttpsConfig.HttpsServer server = new HttpsConfig.HttpsServer(handler, tls, "127.0.0.1", 0); server.start(); try { assertThat(server.isRunning()).isTrue(); String body = HttpClient.create() .secure(spec -> spec.sslContext(trustAll)) .get().uri("https://127.0.0.1:" + server.port() + "/") .responseContent().aggregate().asString().block(); assertThat(body).isEqualTo("hello https"); } finally { server.stop(); } assertThat(server.isRunning()).isFalse(); } }