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