View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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.htmlunit.junit.BrowserRunner;
27  import org.htmlunit.util.WebConnectionWrapper;
28  import org.junit.Test;
29  import org.junit.runner.RunWith;
30  
31  /**
32   * Tests for insecure SSL.
33   *
34   * @author Ahmed Ashour
35   * @author Ronald Brill
36   */
37  @RunWith(BrowserRunner.class)
38  public class HttpWebConnectionInsecureSSLTest extends WebServerTestCase {
39  
40      /**
41       * @throws Exception if an error occurs
42       */
43      @Test(expected = SSLHandshakeException.class)
44      public void normal() throws Exception {
45          final URL https = new URL("https://localhost:" + PORT2 + "/");
46          loadPage("<div>test</div>", https);
47      }
48  
49      /**
50       * @throws Exception if an error occurs
51       */
52      @Test
53      public void insecureSSL() throws Exception {
54          final WebClient webClient = getWebClient();
55          webClient.getOptions().setUseInsecureSSL(true);
56  
57          final URL https = new URL("https://localhost:" + PORT2 + "/");
58          loadPage("<div>test</div>", https);
59      }
60  
61      /**
62       * @throws Exception if an error occurs
63       */
64      @Test
65      public void insecureSSL_withWrapper() throws Exception {
66          final WebClient webClient = getWebClient();
67          webClient.setWebConnection(new WebConnectionWrapper(webClient.getWebConnection()));
68          webClient.getOptions().setUseInsecureSSL(true);
69  
70          final URL https = new URL("https://localhost:" + PORT2 + "/");
71          loadPage("<div>test</div>", https);
72      }
73  
74      @Override
75      protected boolean isHttps() {
76          return true;
77      }
78  
79      @Override
80      public SslConnectionFactory getSslConnectionFactory() {
81          final URL url = HttpWebConnectionInsecureSSLWithClientCertificateTest.class
82                  .getClassLoader().getResource("insecureSSL.pfx");
83  
84          final SslContextFactory contextFactory = new Server.Server();
85          contextFactory.setKeyStorePath(url.toExternalForm());
86          contextFactory.setKeyStorePassword("nopassword");
87          return new SslConnectionFactory(contextFactory, HTTP_1_1.toString());
88      }
89  }