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.Test;
33
34
35
36
37
38
39 public class DebuggingWebConnectionTest extends SimpleWebTestCase {
40
41
42
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"));
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
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
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
92
93
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);
113 assertNull(response.getResponseHeaderValue("Content-Encoding"));
114
115 FileUtils.deleteDirectory(dwc.getReportFolder());
116 }
117 }
118 }