-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathXmlPageTest.java
More file actions
412 lines (357 loc) · 14.5 KB
/
XmlPageTest.java
File metadata and controls
412 lines (357 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* Copyright (c) 2002-2026 Gargoyle Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.htmlunit.xml;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.ByteOrderMark;
import org.htmlunit.MockWebConnection;
import org.htmlunit.Page;
import org.htmlunit.WebClient;
import org.htmlunit.WebServerTestCase;
import org.htmlunit.html.DomAttr;
import org.htmlunit.html.DomElement;
import org.htmlunit.html.DomText;
import org.htmlunit.http.HttpStatus;
import org.htmlunit.util.MimeType;
import org.htmlunit.util.StringUtils;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Node;
import jakarta.servlet.Servlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
/**
* Tests for {@link XmlPage}.
*
* @author Marc Guillemot
* @author Ahmed Ashour
* @author Ronald Brill
*/
public class XmlPageTest extends WebServerTestCase {
/**
* @throws Exception if the test fails
*/
@Test
public void asNormalizedText() throws Exception {
asNormalizedText("<msg>abc</msg>", "abc");
}
/**
* @throws Exception if the test fails
*/
@Test
public void asNormalizedTextOnlyText() throws Exception {
final WebClient client = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setDefaultResponse("<msg>abc</msg>", 200, "OK", MimeType.TEXT_XML);
client.setWebConnection(webConnection);
final XmlPage xmlPage = client.getPage(URL_FIRST);
assertEquals("abc", ((DomText) xmlPage.getFirstByXPath("/msg/text()")).asNormalizedText());
}
/**
* @throws Exception if the test fails
*/
@Test
public void asTextComplex() throws Exception {
final String xml
= "<msg>1"
+ "<h1>h1</h1>"
+ "<h2>h2"
+ "<h3>h3</h3>"
+ "<h3></h3>"
+ "txt"
+ "</h2>o"
+ "</msg>";
asNormalizedText(xml, "1h1h2h3txto");
}
/**
* @throws Exception if the test fails
*/
@Test
public void asTextPart() throws Exception {
final String xml
= "<outer>outer"
+ "<msg><h1>h1</h1></msg>"
+ "xy</outer>";
asNormalizedText(xml, "h1");
}
/**
* Test for issue #1817.
* @throws Exception if the test fails
*/
@Test
public void asTextEmpty() throws Exception {
asNormalizedText("<msg></msg>", "");
}
private void asNormalizedText(final String xml, final String expected) throws Exception {
final WebClient client = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setDefaultResponse(xml, 200, "OK", MimeType.TEXT_XML);
client.setWebConnection(webConnection);
final XmlPage xmlPage = client.getPage(URL_FIRST);
final DomElement msg = xmlPage.getFirstByXPath("//msg");
assertNotNull("No element found by XPath '//msg'", msg);
assertEquals(expected, msg.asNormalizedText());
}
/**
* Tests namespace.
* @throws Exception if the test fails
*/
@Test
public void namespace() throws Exception {
final String content
= "<?xml version='1.0'?>\n"
+ "<RDF xmlns='http://www.w3.org/1999/02/22-rdf-syntax-ns#' "
+ "xmlns:em='http://www.mozilla.org/2004/em-rdf#'>"
+ "<Description about='urn:mozilla:install-manifest'>"
+ "<em:name>My Plugin</em:name>"
+ "</Description>\n"
+ "</RDF>";
final XmlPage xmlPage = testDocument(content, MimeType.TEXT_XML);
final Node node = xmlPage.getXmlDocument().getFirstChild().getFirstChild().getFirstChild();
assertEquals("em:name", node.getNodeName());
assertEquals("name", node.getLocalName());
assertEquals("http://www.mozilla.org/2004/em-rdf#", node.getNamespaceURI());
}
/**
* Tests a simple valid XML document.
* @throws Exception if the test fails
*/
@Test
public void validDocument() throws Exception {
final String content
= "<?xml version=\"1.0\"?>\n"
+ "<foo>\n"
+ " <foofoo name='first'>something</foofoo>\n"
+ " <foofoo name='second'>something else</foofoo>\n"
+ "</foo>";
final XmlPage xmlPage = testDocument(content, MimeType.TEXT_XML);
assertEquals("foo", xmlPage.getXmlDocument().getFirstChild().getNodeName());
}
/**
* Test that UTF-8 is used as default encoding for xml responses
* (issue 3385410).
* @throws Exception if the test fails
*/
@Test
public void defaultEncoding() throws Exception {
final String content
= "<?xml version=\"1.0\"?>\n"
+ "<foo>\n"
+ "\u0434\n"
+ "</foo>";
final byte[] bytes = StringUtils.toByteArray(content, UTF_8);
final WebClient client = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setDefaultResponse(bytes, 200, "OK", MimeType.TEXT_XML);
client.setWebConnection(webConnection);
final Page page = client.getPage(URL_FIRST);
assertTrue(page instanceof XmlPage);
final XmlPage xmlPage = (XmlPage) page;
assertEquals(content, xmlPage.getWebResponse().getContentAsString());
assertNotNull(xmlPage.getXmlDocument());
assertEquals("foo", xmlPage.getXmlDocument().getFirstChild().getNodeName());
}
/**
* Utility method to test XML page of different MIME types.
* @param content the XML content
* @param mimeType the MIME type
* @return the page returned by the WebClient
* @throws Exception if a problem occurs
*/
private XmlPage testDocument(final String content, final String mimeType) throws Exception {
final WebClient client = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setDefaultResponse(content, 200, "OK", mimeType);
client.setWebConnection(webConnection);
final Page page = client.getPage(URL_FIRST);
assertEquals(URL_FIRST, page.getUrl());
assertEquals("OK", page.getWebResponse().getStatusMessage());
assertEquals(HttpStatus.OK_200, page.getWebResponse().getStatusCode());
assertEquals(mimeType, page.getWebResponse().getContentType());
assertTrue(page instanceof XmlPage);
final XmlPage xmlPage = (XmlPage) page;
assertEquals(content, xmlPage.getWebResponse().getContentAsString());
assertNotNull(xmlPage.getXmlDocument());
return xmlPage;
}
/**
* Tests a simple invalid (badly formed) XML document.
* @throws Exception if the test fails
*/
@Test
public void invalidDocument() throws Exception {
final WebClient client = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
final String content
= "<?xml version=\"1.0\"?>\n"
+ "<foo>\n"
+ " <foofoo invalid\n"
+ " <foofoo name='first'>something</foofoo>\n"
+ " <foofoo name='second'>something else</foofoo>\n"
+ "</foo>";
webConnection.setDefaultResponse(content, 200, "OK", MimeType.TEXT_XML);
client.setWebConnection(webConnection);
final Page page = client.getPage(URL_FIRST);
assertEquals(URL_FIRST, page.getUrl());
assertEquals("OK", page.getWebResponse().getStatusMessage());
assertEquals(HttpStatus.OK_200, page.getWebResponse().getStatusCode());
assertEquals(MimeType.TEXT_XML, page.getWebResponse().getContentType());
assertTrue(page instanceof XmlPage);
final XmlPage xmlPage = (XmlPage) page;
assertEquals(content, xmlPage.getWebResponse().getContentAsString());
assertNull(xmlPage.getXmlDocument());
}
/**
* Tests a simple empty XML document.
* @throws Exception if the test fails
*/
@Test
public void emptyDocument() throws Exception {
final WebClient client = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
final String content = "";
webConnection.setDefaultResponse(content, 200, "OK", MimeType.TEXT_XML);
client.setWebConnection(webConnection);
final Page page = client.getPage(URL_FIRST);
assertEquals(URL_FIRST, page.getUrl());
assertEquals("OK", page.getWebResponse().getStatusMessage());
assertEquals(HttpStatus.OK_200, page.getWebResponse().getStatusCode());
assertEquals(MimeType.TEXT_XML, page.getWebResponse().getContentType());
assertTrue(page instanceof XmlPage);
final XmlPage xmlPage = (XmlPage) page;
assertEquals(content, xmlPage.getWebResponse().getContentAsString());
assertNull(xmlPage.getXmlDocument());
}
/**
* Tests a simple empty XML document.
* @throws Exception if the test fails
*/
@Test
public void blankDocument() throws Exception {
final WebClient client = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
final String content = "\t \n\r\n";
webConnection.setDefaultResponse(content, 200, "OK", MimeType.TEXT_XML);
client.setWebConnection(webConnection);
final Page page = client.getPage(URL_FIRST);
assertEquals(URL_FIRST, page.getUrl());
assertEquals("OK", page.getWebResponse().getStatusMessage());
assertEquals(HttpStatus.OK_200, page.getWebResponse().getStatusCode());
assertEquals("text/xml", page.getWebResponse().getContentType());
assertTrue(page instanceof XmlPage);
final XmlPage xmlPage = (XmlPage) page;
assertEquals(content, xmlPage.getWebResponse().getContentAsString());
assertNull(xmlPage.getXmlDocument());
}
/**
* @throws Exception if the test fails
*/
@Test
public void voiceXML() throws Exception {
final String content =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<vxml xmlns=\"http://www.w3.org/2001/vxml\""
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
+ " xsi:schemaLocation=\"http://www.w3.org/2001/vxml "
+ " http://www.w3.org/TR/voicexml20/vxml.xsd\""
+ " version=\"2.0\">\n"
+ " <form>\n"
+ " <block>Hello World!</block>\n"
+ " </form>\n"
+ "</vxml>";
final XmlPage xmlPage = testDocument(content, "application/voicexml+xml");
assertEquals("vxml", xmlPage.getXmlDocument().getFirstChild().getNodeName());
}
/**
* @throws Exception if the test fails
*/
@Test
public void xpath() throws Exception {
final String html
= "<?xml version=\"1.0\"?>\n"
+ "<foo>\n"
+ " <foofoo name='first'>something</foofoo>\n"
+ " <foofoo name='second'>something else</foofoo>\n"
+ "</foo>";
final XmlPage xmlPage = testDocument(html, MimeType.TEXT_XML);
assertEquals(1, xmlPage.getByXPath("//foofoo[@name='first']").size());
}
/**
* Test for issue #1820.
* @throws Exception if the test fails
*/
@Test
public void xpathAttribute() throws Exception {
final String html
= "<?xml version=\"1.0\"?>\n"
+ "<foo>\n"
+ " <MARKGR INTREGN=\"1289218\" BILING=\"Y\" OOCD=\"CH\" INTREGD=\"20160111\">\n"
+ " </MARKGR>\n"
+ "</foo>";
final XmlPage xmlPage = testDocument(html, MimeType.TEXT_XML);
assertEquals(1, xmlPage.getByXPath("//MARKGR").size());
assertNotNull(xmlPage.getFirstByXPath("//MARKGR"));
assertEquals(0, xmlPage.getByXPath("//markgr").size());
assertNull(xmlPage.getFirstByXPath("//markgr"));
assertEquals(1, xmlPage.getByXPath("//MARKGR/@INTREGN").size());
assertTrue(xmlPage.getFirstByXPath("//MARKGR/@INTREGN") instanceof DomAttr);
assertEquals(0, xmlPage.getByXPath("//MARKGR/@intregn").size());
assertNull(xmlPage.getFirstByXPath("//MARKGR/@intregn"));
}
/**
* @throws Exception if the test fails
*/
@Test
public void noResponse() throws Exception {
final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
servlets.put("/test", NoResponseServlet.class);
startWebServer("./", servlets);
final WebClient client = getWebClient();
client.getPage(URL_FIRST + "test");
}
/**
* Servlet for {@link #noResponse()}.
*/
public static class NoResponseServlet extends HttpServlet {
/**
* {@inheritDoc}
*/
@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
response.setContentType(MimeType.TEXT_XML);
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
}
}
/**
* @throws Exception if the test fails
*/
@Test
public void bom() throws Exception {
final byte[] bom = ByteOrderMark.UTF_8.getBytes();
final byte[] xml = "<msg>abc</msg>".getBytes(UTF_8);
final byte[] bytes = ByteBuffer.allocate(bom.length + xml.length).put(bom).put(xml).array();
final WebClient client = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setDefaultResponse(bytes, 200, "OK", MimeType.TEXT_XML);
client.setWebConnection(webConnection);
final XmlPage xmlPage = client.getPage(URL_FIRST);
final DomElement msg = xmlPage.getFirstByXPath("//msg");
assertNotNull("No element found by XPath '//msg'", msg);
assertEquals("abc", msg.asNormalizedText());
}
}