1
2
3
4
5
6
7
8
9
10
11
12
13
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
36
37
38
39
40 public class DebuggingWebConnectionTest extends SimpleWebTestCase {
41
42
43
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"));
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
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
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
93
94
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);
114 assertNull(response.getResponseHeaderValue("Content-Encoding"));
115
116 FileUtils.deleteDirectory(dwc.getReportFolder());
117 }
118 }
119 }