View Javadoc
1   /*
2    * MIT License
3    *
4    * Copyright (c) 2010-2024 The Waffle Project Contributors: https://github.com/Waffle/waffle/graphs/contributors
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy
7    * of this software and associated documentation files (the "Software"), to deal
8    * in the Software without restriction, including without limitation the rights
9    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10   * copies of the Software, and to permit persons to whom the Software is
11   * furnished to do so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
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   * Test the WaffleInfoServlet.
51   */
52  class WaffleInfoServletTest {
53  
54      /** The Constant LOGGER. */
55      private static final Logger LOGGER = LoggerFactory.getLogger(WaffleInfoServletTest.class);
56  
57      /**
58       * Test get info.
59       *
60       * @throws ParserConfigurationException
61       *             the parser configuration exception
62       * @throws SAXException
63       *             the SAX exception
64       * @throws IOException
65       *             Signals that an I/O exception has occurred.
66       * @throws ServletException
67       *             the servlet exception
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          // Make sure JNA Version is properly noted
85          Assertions.assertEquals(Platform.class.getPackage().getImplementationVersion(),
86                  doc.getDocumentElement().getAttribute("jna"));
87  
88          final Node node = doc.getDocumentElement().getFirstChild().getNextSibling() // request
89                  .getFirstChild().getNextSibling() // AuthType
90                  .getNextSibling().getNextSibling();
91  
92          // Make sure the headers were added correctly
93          Assertions.assertEquals("headers", node.getNodeName());
94          final Node child = node.getFirstChild().getNextSibling();
95          Assertions.assertEquals("hello", child.getNodeName());
96      }
97  
98      /**
99       * Load xml from string.
100      *
101      * @param xml
102      *            the xml
103      *
104      * @return the document
105      *
106      * @throws ParserConfigurationException
107      *             the parser configuration exception
108      * @throws SAXException
109      *             the SAX exception
110      * @throws IOException
111      *             Signals that an I/O exception has occurred.
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 }