1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import java.io.File;
18 import java.net.URL;
19 import java.net.UnknownHostException;
20 import java.nio.file.Path;
21
22 import org.apache.commons.io.FileUtils;
23 import org.htmlunit.MockWebConnection;
24 import org.htmlunit.SimpleWebTestCase;
25 import org.htmlunit.WebClient;
26 import org.htmlunit.util.MimeType;
27 import org.junit.jupiter.api.Test;
28 import org.junit.jupiter.api.io.TempDir;
29
30
31
32
33
34
35
36 public class XmlSerializerTest extends SimpleWebTestCase {
37
38 @TempDir
39 static Path TEMP_DIR_;
40
41
42
43
44 @Test
45 public void notExistingLink() throws Exception {
46 final String html = DOCTYPE_HTML
47 + "<html>\n"
48 + "<body>"
49 + " <link rel='alternate' href='none.jpg'>\n"
50 + "</body>\n"
51 + "</html>";
52
53 final WebClient client = getWebClient();
54 final MockWebConnection connection = getMockWebConnection();
55 connection.setDefaultResponse("Error: not found", 404, "Not Found", MimeType.TEXT_HTML);
56 connection.setResponse(URL_FIRST, html);
57 client.setWebConnection(connection);
58
59 final HtmlPage page = client.getPage(URL_FIRST);
60
61 final File tmpFolder = new File(TEMP_DIR_.toFile(), "hu");
62 tmpFolder.mkdir();
63 final File file = new File(tmpFolder, "hu_XmlSerializerTest_notExistingLink.html");
64 FileUtils.deleteQuietly(file);
65
66 page.save(file);
67 assertTrue(file.exists());
68 assertTrue(file.isFile());
69 }
70
71
72
73
74 @Test
75 public void unknownHostExceptionLink() throws Exception {
76 final URL imageUrl = new URL(URL_FIRST, "none.jpg");
77 final String html = DOCTYPE_HTML
78 + "<html>\n"
79 + "<body>"
80 + " <link rel='alternate' href='" + imageUrl.toExternalForm() + "'>\n"
81 + "</body>\n"
82 + "</html>";
83
84 final WebClient client = getWebClient();
85 final MockWebConnection connection = getMockWebConnection();
86 connection.setThrowable(imageUrl, new UnknownHostException(imageUrl.toExternalForm()));
87 connection.setResponse(URL_FIRST, html);
88 client.setWebConnection(connection);
89
90 final HtmlPage page = client.getPage(URL_FIRST);
91
92 final File tmpFolder = new File(TEMP_DIR_.toFile(), "hu");
93 tmpFolder.mkdir();
94 final File file = new File(tmpFolder, "hu_XmlSerializerTest_unknownHostExceptionLink.html");
95 FileUtils.deleteQuietly(file);
96
97 page.save(file);
98 assertTrue(file.exists());
99 assertTrue(file.isFile());
100 }
101
102
103
104
105 @Test
106 public void unsupportedProtocolLink() throws Exception {
107 final String html = DOCTYPE_HTML
108 + "<html><head>"
109 + "<link rel='alternate' href='android-app://somehost'>\n"
110 + "</head></html>";
111
112 final WebClient client = getWebClient();
113 final MockWebConnection connection = getMockWebConnection();
114 connection.setResponse(URL_FIRST, html);
115 client.setWebConnection(connection);
116
117 final HtmlPage page = client.getPage(URL_FIRST);
118
119 final File tmpFolder = new File(TEMP_DIR_.toFile(), "hu");
120 tmpFolder.mkdir();
121 final File file = new File(tmpFolder, "hu_XmlSerializerTest_unsupportedProtocol.html");
122 FileUtils.deleteQuietly(file);
123
124 page.save(file);
125 assertTrue(file.exists());
126 assertTrue(file.isFile());
127 }
128
129
130
131
132 @Test
133 public void notExistingImage() throws Exception {
134 final String html = DOCTYPE_HTML
135 + "<html>\n"
136 + "<body>"
137 + " <img src='none.jpg'>\n"
138 + "</body>\n"
139 + "</html>";
140
141 final WebClient client = getWebClient();
142 final MockWebConnection connection = getMockWebConnection();
143 connection.setDefaultResponse("Error: not found", 404, "Not Found", MimeType.TEXT_HTML);
144 connection.setResponse(URL_FIRST, html);
145 client.setWebConnection(connection);
146
147 final HtmlPage page = client.getPage(URL_FIRST);
148
149 final File tmpFolder = new File(TEMP_DIR_.toFile(), "hu");
150 tmpFolder.mkdir();
151 final File file = new File(tmpFolder, "hu_XmlSerializerTest_notExistingImage.html");
152 FileUtils.deleteQuietly(file);
153
154 page.save(file);
155 assertTrue(file.exists());
156 assertTrue(file.isFile());
157 }
158
159
160
161
162 @Test
163 public void unknownHostExceptionImage() throws Exception {
164 final URL imageUrl = new URL(URL_FIRST, "none.jpg");
165 final String html = DOCTYPE_HTML
166 + "<html>\n"
167 + "<body>"
168 + " <img src='" + imageUrl.toExternalForm() + "'>\n"
169 + "</body>\n"
170 + "</html>";
171
172 final WebClient client = getWebClient();
173 final MockWebConnection connection = getMockWebConnection();
174 connection.setThrowable(imageUrl, new UnknownHostException(imageUrl.toExternalForm()));
175 connection.setResponse(URL_FIRST, html);
176 client.setWebConnection(connection);
177
178 final HtmlPage page = client.getPage(URL_FIRST);
179
180 final File tmpFolder = new File(TEMP_DIR_.toFile(), "hu");
181 tmpFolder.mkdir();
182 final File file = new File(tmpFolder, "hu_XmlSerializerTest_unknownHostExceptionImage.html");
183 FileUtils.deleteQuietly(file);
184
185 page.save(file);
186 assertTrue(file.exists());
187 assertTrue(file.isFile());
188 }
189
190
191
192
193 @Test
194 public void unsupportedProtocolImage() throws Exception {
195 final String html = DOCTYPE_HTML
196 + "<html>\n"
197 + "<body>"
198 + " <img src='android-app://somehost'>\n"
199 + "</body>\n"
200 + "</html>";
201
202 final WebClient client = getWebClient();
203 final MockWebConnection connection = getMockWebConnection();
204 connection.setResponse(URL_FIRST, html);
205 client.setWebConnection(connection);
206
207 final HtmlPage page = client.getPage(URL_FIRST);
208
209 final File tmpFolder = new File(TEMP_DIR_.toFile(), "hu");
210 tmpFolder.mkdir();
211 final File file = new File(tmpFolder, "hu_XmlSerializerTest_unsupportedProtocolImage.html");
212 FileUtils.deleteQuietly(file);
213
214 page.save(file);
215 assertTrue(file.exists());
216 assertTrue(file.isFile());
217 }
218 }