1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package waffle.servlet;
25
26 import com.sun.jna.Platform;
27
28 import java.io.IOException;
29 import java.io.StringReader;
30
31 import javax.servlet.ServletException;
32 import javax.xml.XMLConstants;
33 import javax.xml.parsers.DocumentBuilder;
34 import javax.xml.parsers.DocumentBuilderFactory;
35 import javax.xml.parsers.ParserConfigurationException;
36
37 import org.junit.jupiter.api.Assertions;
38 import org.junit.jupiter.api.Test;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.w3c.dom.Document;
42 import org.w3c.dom.Node;
43 import org.xml.sax.InputSource;
44 import org.xml.sax.SAXException;
45
46 import waffle.mock.http.SimpleHttpRequest;
47 import waffle.mock.http.SimpleHttpResponse;
48
49
50
51
52 class WaffleInfoServletTest {
53
54
55 private static final Logger LOGGER = LoggerFactory.getLogger(WaffleInfoServletTest.class);
56
57
58
59
60
61
62
63
64
65
66
67
68
69 @Test
70 void testGetInfo() throws ParserConfigurationException, SAXException, IOException, ServletException {
71 final SimpleHttpRequest request = new SimpleHttpRequest();
72 request.addHeader("hello", "waffle");
73
74 final SimpleHttpResponse response = new SimpleHttpResponse();
75
76 final WaffleInfoServlet servlet = new WaffleInfoServlet();
77 servlet.doGet(request, response);
78
79 final String xml = response.getOutputText();
80 final Document doc = WaffleInfoServletTest.loadXMLFromString(xml);
81
82 WaffleInfoServletTest.LOGGER.info("GOT: {}", xml);
83
84
85 Assertions.assertEquals(Platform.class.getPackage().getImplementationVersion(),
86 doc.getDocumentElement().getAttribute("jna"));
87
88 final Node node = doc.getDocumentElement().getFirstChild().getNextSibling()
89 .getFirstChild().getNextSibling()
90 .getNextSibling().getNextSibling();
91
92
93 Assertions.assertEquals("headers", node.getNodeName());
94 final Node child = node.getFirstChild().getNextSibling();
95 Assertions.assertEquals("hello", child.getNodeName());
96 }
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 private static Document loadXMLFromString(final String xml)
114 throws ParserConfigurationException, SAXException, IOException {
115 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
116 factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
117 factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
118 final DocumentBuilder builder = factory.newDocumentBuilder();
119 final InputSource is = new InputSource(new StringReader(xml));
120 return builder.parse(is);
121 }
122 }