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 java.security.KeyStore;
18  
19  import javax.net.ssl.SSLContext;
20  
21  import org.apache.commons.lang3.SerializationUtils;
22  import org.htmlunit.junit.BrowserRunner;
23  import org.junit.Assert;
24  import org.junit.Test;
25  import org.junit.runner.RunWith;
26  
27  /**
28   * Tests for {@link WebClientOptions}.
29   *
30   * @author Ronald Brill
31   */
32  @RunWith(BrowserRunner.class)
33  public class WebClientOptionsTest extends SimpleWebTestCase {
34  
35      /**
36       * @throws Exception if an error occurs
37       */
38      @Test
39      public void serialization() throws Exception {
40          final WebClientOptions original = new WebClientOptions();
41  
42          final byte[] bytes = SerializationUtils.serialize(original);
43          final WebClientOptions deserialized = (WebClientOptions) SerializationUtils.deserialize(bytes);
44  
45          assertEquals(original.isJavaScriptEnabled(), deserialized.isJavaScriptEnabled());
46          assertEquals(original.isCssEnabled(), deserialized.isCssEnabled());
47  
48          assertEquals(original.isPrintContentOnFailingStatusCode(),
49                          deserialized.isPrintContentOnFailingStatusCode());
50          assertEquals(original.isThrowExceptionOnFailingStatusCode(),
51                          deserialized.isThrowExceptionOnFailingStatusCode());
52          assertEquals(original.isThrowExceptionOnScriptError(),
53                          deserialized.isThrowExceptionOnScriptError());
54          assertEquals(original.isPopupBlockerEnabled(),
55                          deserialized.isPopupBlockerEnabled());
56          assertEquals(original.isRedirectEnabled(),
57                          deserialized.isRedirectEnabled());
58  
59          assertEquals(original.isGeolocationEnabled(),
60                          deserialized.isGeolocationEnabled());
61          assertEquals(original.getGeolocation(),
62                          deserialized.getGeolocation());
63  
64          assertEquals(original.getNekoReaderBufferSize(),
65                          deserialized.getNekoReaderBufferSize());
66  
67          assertEquals(original.isWebSocketEnabled(),
68                          deserialized.isWebSocketEnabled());
69          assertEquals(original.getWebSocketMaxTextMessageSize(),
70                          deserialized.getWebSocketMaxTextMessageSize());
71          assertEquals(original.getWebSocketMaxTextMessageBufferSize(),
72                          deserialized.getWebSocketMaxTextMessageBufferSize());
73          assertEquals(original.getWebSocketMaxBinaryMessageSize(),
74                          deserialized.getWebSocketMaxBinaryMessageSize());
75          assertEquals(original.getWebSocketMaxBinaryMessageBufferSize(),
76                          deserialized.getWebSocketMaxBinaryMessageBufferSize());
77  
78          assertEquals(original.isFetchPolyfillEnabled(), deserialized.isFetchPolyfillEnabled());
79      }
80  
81      /**
82       * @throws Exception if an error occurs
83       */
84      @Test
85      public void serializationChanged() throws Exception {
86          final WebClientOptions original = new WebClientOptions();
87          original.setJavaScriptEnabled(false);
88          original.setCssEnabled(false);
89  
90          original.setPrintContentOnFailingStatusCode(false);
91          original.setThrowExceptionOnFailingStatusCode(false);
92          original.setThrowExceptionOnScriptError(false);
93          original.setPopupBlockerEnabled(true);
94          original.setRedirectEnabled(false);
95  
96          original.setGeolocationEnabled(true);
97          original.setGeolocation(new WebClientOptions.Geolocation(1d, 2d, 3d, 4d, 5d, 6d, 7d));
98  
99          original.setNekoReaderBufferSize(1234567);
100 
101         original.setWebSocketEnabled(false);
102         original.setWebSocketMaxTextMessageSize(77);
103         original.setWebSocketMaxTextMessageBufferSize(771);
104         original.setWebSocketMaxBinaryMessageSize(44);
105         original.setWebSocketMaxBinaryMessageBufferSize(441);
106 
107         original.setFetchPolyfillEnabled(true);
108 
109         final byte[] bytes = SerializationUtils.serialize(original);
110         final WebClientOptions deserialized = (WebClientOptions) SerializationUtils.deserialize(bytes);
111 
112         assertEquals(original.isJavaScriptEnabled(), deserialized.isJavaScriptEnabled());
113         assertEquals(original.isCssEnabled(), deserialized.isCssEnabled());
114 
115         assertEquals(original.isPrintContentOnFailingStatusCode(), deserialized.isPrintContentOnFailingStatusCode());
116         assertEquals(original.isThrowExceptionOnFailingStatusCode(),
117                         deserialized.isThrowExceptionOnFailingStatusCode());
118         assertEquals(original.isThrowExceptionOnScriptError(), deserialized.isThrowExceptionOnScriptError());
119         assertEquals(original.isPopupBlockerEnabled(), deserialized.isPopupBlockerEnabled());
120         assertEquals(original.isRedirectEnabled(), deserialized.isRedirectEnabled());
121 
122         assertEquals(original.isGeolocationEnabled(), deserialized.isGeolocationEnabled());
123         assertEquals(original.getGeolocation().getAccuracy(), deserialized.getGeolocation().getAccuracy());
124         assertEquals(original.getGeolocation().getLatitude(), deserialized.getGeolocation().getLatitude());
125         assertEquals(original.getGeolocation().getLongitude(), deserialized.getGeolocation().getLongitude());
126         assertEquals(original.getGeolocation().getAltitude(), deserialized.getGeolocation().getAltitude());
127         assertEquals(original.getGeolocation().getAltitudeAccuracy(),
128                 deserialized.getGeolocation().getAltitudeAccuracy());
129         assertEquals(original.getGeolocation().getHeading(), deserialized.getGeolocation().getHeading());
130         assertEquals(original.getGeolocation().getSpeed(), deserialized.getGeolocation().getSpeed());
131 
132         assertEquals(original.getNekoReaderBufferSize(), deserialized.getNekoReaderBufferSize());
133 
134         assertEquals(original.isWebSocketEnabled(),
135                         deserialized.isWebSocketEnabled());
136         assertEquals(original.getWebSocketMaxTextMessageSize(),
137                         deserialized.getWebSocketMaxTextMessageSize());
138         assertEquals(original.getWebSocketMaxTextMessageBufferSize(),
139                         deserialized.getWebSocketMaxTextMessageBufferSize());
140         assertEquals(original.getWebSocketMaxBinaryMessageSize(),
141                         deserialized.getWebSocketMaxBinaryMessageSize());
142         assertEquals(original.getWebSocketMaxBinaryMessageBufferSize(),
143                         deserialized.getWebSocketMaxBinaryMessageBufferSize());
144 
145         assertEquals(original.isFetchPolyfillEnabled(), deserialized.isFetchPolyfillEnabled());
146     }
147 
148     /**
149      * @throws Exception if an error occurs
150      */
151     @Test
152     public void serializationSSLContextProvider() throws Exception {
153         final WebClientOptions original = new WebClientOptions();
154         original.setSSLContext(SSLContext.getDefault());
155 
156         final byte[] bytes = SerializationUtils.serialize(original);
157         final WebClientOptions deserialized = (WebClientOptions) SerializationUtils.deserialize(bytes);
158 
159         assertNull(deserialized.getSSLContext());
160     }
161 
162     /**
163      * @throws Exception if an error occurs
164      */
165     @Test
166     public void serializationSSLTrustStore() throws Exception {
167         final WebClientOptions original = new WebClientOptions();
168 
169         final KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
170         original.setSSLTrustStore(keyStore);
171 
172         final byte[] bytes = SerializationUtils.serialize(original);
173         final WebClientOptions deserialized = (WebClientOptions) SerializationUtils.deserialize(bytes);
174 
175         assertNull(deserialized.getSSLTrustStore());
176     }
177 
178     /**
179      * @throws Exception if an error occurs
180      */
181     @Test
182     public void sslClientCertificateStore() throws Exception {
183         final WebClientOptions original = new WebClientOptions();
184 
185         final KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
186         original.setSSLClientCertificateKeyStore(keyStore, "secret".toCharArray());
187 
188         final byte[] bytes = SerializationUtils.serialize(original);
189         final WebClientOptions deserialized = (WebClientOptions) SerializationUtils.deserialize(bytes);
190 
191         assertNull(deserialized.getSSLClientCertificateStore());
192         Assert.assertArrayEquals("secret".toCharArray(), deserialized.getSSLClientCertificatePassword());
193     }
194 }