Validating an HTML document with a REST guarantee

I am trying to use REST Assured to check some properties of an HTML document returned by my server. The SSCCE demonstrating the problem will be as follows:

import static com.jayway.restassured.path.xml.config.XmlPathConfig.xmlPathConfig; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import org.junit.Test; import com.jayway.restassured.path.xml.XmlPath; public class HtmlDocumentTest { @Test public void titleShouldBeHelloWorld() { final XmlPath xml = new XmlPath("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" + "<html xmlns=\"http://www.w3.org/1999/xhtml\">" + "<head><title>Hello world</title></head><body></body></html>") .using(xmlPathConfig().with().feature("http://apache.org/xml/features/disallow-doctype-decl", false)); assertThat(xml.get("//title[text()]"), is("Hello world")); } } 

Now this attempt ends with com.jayway.restassured.path.xml.exception.XmlPathException: Failed to parse the XML document caused by disconnecting all possible errors, java.net.ConnectException: Connection timed out after about 30 seconds!

If I delete the line using xmlPathConfig().with().feature(...) , the test will immediately end due to DOCTYPE is disallowed when the feature "http://apache.org/xml/features/disallow-doctype-decl" set to true. .

If I delete the doctype line from the document, the parsing succeeds, but if the error says β€œ Expected: is "Hello world" but: was <Hello worldnull> ” however, another problem occurs (but feel free to give instructions for this, too...). And removing doctype is not an option for me anyway.

So the question is: how do you check the properties of an HTML document using doctype using REST Assured? The documentation states that "REST Assured providers are predefined by parsers such as HTML, XML, and JSON." , But I can’t find examples on how to activate and work with this HTML parser! There is no " HtmlPath " class, such as XmlPath , and this timeout exception is very puzzling ...

+5
source share
2 answers

I checked your code. The point is that XmlPath of Restassured is not an Xpath, but it uses property access syntax. If you add body content to your HTML sample, you will see that your XPath does not do much. The actual name of the GPath query language . The following example works, pay attention also to using CompatibilityMode.HTML, which has the necessary configuration for you:

 import static org.junit.Assert.assertEquals; import org.junit.Test; import com.jayway.restassured.path.xml.XmlPath; import com.jayway.restassured.path.xml.XmlPath.CompatibilityMode; public class HtmlDocumentTest { @Test public void titleShouldBeHelloWorld() { XmlPath doc = new XmlPath( CompatibilityMode.HTML, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" + "<html xmlns=\"http://www.w3.org/1999/xhtml\">" + "<head><title>Hello world</title></head>" + "<body>some body" + "<div class=\"content\">wrapped</div>" + "<div class=\"content\">wrapped2</div>" + "</body></html>"); String title = doc.getString("html.head.title"); String content = doc.getString("html.body.div.find { it.@class == 'content' }"); String content2 = doc.getString("**.findAll { it.@class == 'content' }[1]"); assertEquals("Hello world", title); assertEquals("wrapped", content); assertEquals("wrapped2", content2); } } 
+3
source

If you use DSL (given / when / then) then XmlPath with CompatibilityMode.HTML used automatically if the response header header contains an html compatible media type (e.g. text/html ). For example, if /index.html contains the following html page:

 <html> <title>My page</title> <body>Something</body> </html> 

then you can check the name and body as follows:

 when(). get("/index.html"). then(). statusCode(200). body("html.title", equalTo("My page"), "html.body", equalTo("Something")); 
+5
source

Source: https://habr.com/ru/post/1233852/


All Articles