1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit;
16
17 import static org.eclipse.jetty.http.HttpVersion.HTTP_1_1;
18
19 import java.net.URL;
20
21 import javax.net.ssl.SSLHandshakeException;
22
23 import org.eclipse.jetty.server.SslConnectionFactory;
24 import org.eclipse.jetty.util.ssl.SslContextFactory;
25 import org.eclipse.jetty.util.ssl.SslContextFactory.Server;
26 import org.junit.jupiter.api.Assertions;
27 import org.junit.jupiter.api.Test;
28
29
30
31
32
33
34
35
36 public class HttpWebConnectionTruststoreTest extends WebServerTestCase {
37
38
39
40
41 @Test
42 public void selfSignedCertInTruststore() throws Exception {
43 final WebClient webClient = getWebClient();
44 webClient.getOptions().setSSLTrustStore(
45 getClass().getClassLoader().getResource("self-signed-cert.keystore"),
46 "nopassword", "jks");
47
48 final URL https = new URL("https://localhost:" + PORT2 + "/");
49 loadPage("<div>test</div>", https);
50 }
51
52
53
54
55 @Test
56 public void selfSignedCertNotInTruststore() throws Exception {
57 final URL https = new URL("https://localhost:" + PORT2 + "/");
58
59 Assertions.assertThrows(SSLHandshakeException.class,
60 () -> loadPage("<div>test</div>", https));
61 }
62
63 @Override
64 protected boolean isHttps() {
65 return true;
66 }
67
68 @Override
69 public SslConnectionFactory getSslConnectionFactory() {
70 final URL url = HttpWebConnectionInsecureSSLWithClientCertificateTest.class
71 .getClassLoader().getResource("self-signed-cert.keystore");
72
73 final SslContextFactory contextFactory = new Server.Server();
74 contextFactory.setKeyStoreType("jks");
75 contextFactory.setKeyStorePath(url.toExternalForm());
76 contextFactory.setKeyStorePassword("nopassword");
77 return new SslConnectionFactory(contextFactory, HTTP_1_1.toString());
78 }
79 }