0

I am creating jUnit tests to check that my proxy servlet which fetches XMLs for a client application returns them without corruption or any changes.

In my jUnit class I have a doGet method which carries out my HTTP get requests to the proxy servlet, it passes it a URL with GET params at the end

An XML response should be received back to the client (jUnit).

protected String doGet() throws IOException, ParserConfigurationException, SAXException
{
    String requestURL = url + params;
    System.out.println(requestURL);
    // fetch XML from URL
        System.out.println(requestURL);
    
        URL url = new URL(requestURL);
        HttpURLConnection connection =
            (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Accept", "application/xml");

        InputStream xml = connection.getInputStream();
        System.out.println(connection.getResponseMessage());
        System.out.println(connection.getResponseCode());

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        
        org.w3c.dom.Document text = db.parse(xml);
        System.out.print(text.getNodeValue());
        return text.toString();
}

Here is my output:

OK

200

null

[#document: null]

I am trying to get the string value of the HTTP response which is an XML document, so I can compare it with another string to make sure it is correct.

How do I get this string value?

Thanks

Community
  • 1
  • 1
tomaytotomato
  • 3,788
  • 16
  • 64
  • 119
  • See [How to I output org.w3c.dom.Element to string format in java?](http://stackoverflow.com/questions/1219596/how-to-i-output-org-w3c-dom-element-to-string-format-in-java) – McDowell Apr 24 '13 at 10:15

1 Answers1

0

Solved it using this little elegant line of code from the Apache.IO.Utils Common Lib

org.apache.commons.io.IOUtils.toString(new URL(requestURL));
tomaytotomato
  • 3,788
  • 16
  • 64
  • 119