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