View Javadoc
1   /*
2    * Copyright (c) 2002-2026 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.util;
16  
17  import static java.nio.charset.StandardCharsets.UTF_8;
18  
19  import java.io.ByteArrayOutputStream;
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.zip.GZIPOutputStream;
25  
26  import org.apache.commons.io.FileUtils;
27  import org.apache.commons.io.IOUtils;
28  import org.htmlunit.MockWebConnection;
29  import org.htmlunit.SimpleWebTestCase;
30  import org.htmlunit.WebRequest;
31  import org.htmlunit.WebResponse;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Tests for {@link DebuggingWebConnection}.
36   *
37   * @author Marc Guillemot
38   * @author Ronald Brill
39   */
40  public class DebuggingWebConnectionTest extends SimpleWebTestCase {
41  
42      /**
43       * @throws Exception if the test fails
44       */
45      @Test
46      public void nameValueListToJsMap() throws Exception {
47          assertEquals("{}", DebuggingWebConnection.nameValueListToJsMap(null));
48          assertEquals("{}", DebuggingWebConnection.nameValueListToJsMap(Collections.emptyList()));
49  
50          List<NameValuePair> list = Collections.singletonList(new NameValuePair("name", "value"));
51          assertEquals("{'name': 'value'}", DebuggingWebConnection.nameValueListToJsMap(list));
52  
53          list = Collections.singletonList(new NameValuePair("na me", "value"));
54          assertEquals("{'na me': 'value'}", DebuggingWebConnection.nameValueListToJsMap(list));
55  
56          list = new ArrayList<>();
57          list.add(new NameValuePair("na me", "value1"));
58          list.add(new NameValuePair("key", "value 2"));
59          list.add(new NameValuePair("key 2", "value 3"));
60          list.add(new NameValuePair("key 4", "with ' quote")); // can it really happen in header?
61          final String expected = "{'na me': 'value1', 'key': 'value 2', 'key 2': 'value 3', 'key 4': 'with \\' quote'}";
62          assertEquals(expected, DebuggingWebConnection.nameValueListToJsMap(list));
63      }
64  
65      /**
66       * @throws Exception if the test fails
67       */
68      @Test
69      public void escapeJSString() throws Exception {
70          assertEquals("", DebuggingWebConnection.escapeJSString(""));
71          assertEquals("hello", DebuggingWebConnection.escapeJSString("hello"));
72          assertEquals("I\\'m here", DebuggingWebConnection.escapeJSString("I'm here"));
73      }
74  
75      /**
76       * @throws Exception if the test fails
77       */
78      @Test
79      public void chooseExtension() throws Exception {
80          assertEquals(".html", DebuggingWebConnection.chooseExtension(MimeType.TEXT_HTML));
81  
82          assertEquals(".js", DebuggingWebConnection.chooseExtension("text/javascript"));
83  
84          assertEquals(".css", DebuggingWebConnection.chooseExtension(MimeType.TEXT_CSS));
85  
86          assertEquals(".xml", DebuggingWebConnection.chooseExtension(MimeType.TEXT_XML));
87  
88          assertEquals(".txt", DebuggingWebConnection.chooseExtension(MimeType.TEXT_PLAIN));
89      }
90  
91      /**
92       * Ensures that Content-Encoding headers are removed when JavaScript is uncompressed.
93       * (was causing java.io.IOException: Not in GZIP format).
94       * @throws Exception if the test fails
95       */
96      @Test
97      public void gzip() throws Exception {
98          final ByteArrayOutputStream baos = new ByteArrayOutputStream();
99          try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(baos)) {
100             IOUtils.write("alert(1)", gzipOutputStream, UTF_8);
101         }
102 
103         final MockWebConnection mockConnection = new MockWebConnection();
104         final List<NameValuePair> responseHeaders = Arrays.asList(
105             new NameValuePair("Content-Encoding", "gzip"));
106         mockConnection.setResponse(URL_FIRST, baos.toByteArray(), 200, "OK", MimeType.TEXT_JAVASCRIPT,
107             responseHeaders);
108 
109         final String dirName = "test-" + getClass().getSimpleName();
110         try (DebuggingWebConnection dwc = new DebuggingWebConnection(mockConnection, dirName)) {
111 
112             final WebRequest request = new WebRequest(URL_FIRST);
113             final WebResponse response = dwc.getResponse(request); // was throwing here
114             assertNull(response.getResponseHeaderValue("Content-Encoding"));
115 
116             FileUtils.deleteDirectory(dwc.getReportFolder());
117         }
118     }
119 }