]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[2298] add a unittest using XML validation with XSD
authorNaoki Kambe <kambe@jprs.co.jp>
Tue, 16 Oct 2012 09:47:20 +0000 (18:47 +0900)
committerNaoki Kambe <kambe@jprs.co.jp>
Fri, 26 Oct 2012 11:36:38 +0000 (20:36 +0900)
XML validation with XSD by using the lxml module. the lxml module is a
third-party module. so if it is not installed on the system, the unittest using
the validation would be skipped.

src/bin/stats/tests/b10-stats-httpd_test.py

index 276a65945db24dabda5199023bb298b5a246e21e..22326fba6622e951e286aa71cdaf25595284fe3a 100644 (file)
@@ -34,6 +34,12 @@ import http.client
 import xml.etree.ElementTree
 import random
 import urllib.parse
+# load this module for xml validation with xsd. For this test, an
+# installation of lxml is required in advance. See http://lxml.de/.
+try:
+    from lxml import etree as lxml_etree
+except ImportError:
+    lxml_etree = None
 
 import isc
 import stats_httpd
@@ -458,6 +464,40 @@ class TestHttpHandler(unittest.TestCase):
         response = self.client.getresponse()
         self.assertEqual(response.status, 404)
 
+    @unittest.skipUnless(lxml_etree, "skipping XML validation with XSD")
+    def test_xml_validation_with_xsd(self):
+        """Tests for XML validation with XSD. If lxml is not
+        installed, this tests would be skipped."""
+        def request_xsd():
+            url_path = stats_httpd.XSD_URL_PATH
+            url_path = urllib.parse.quote(url_path)
+            self.client.putrequest('GET', url_path)
+            self.client.endheaders()
+            xsd_doc = self.client.getresponse()
+            xsd_doc = lxml_etree.parse(xsd_doc)
+            return lxml_etree.XMLSchema(xsd_doc)
+
+        def request_xmldoc(path=''):
+            url_path = '%s/%s' % (stats_httpd.XML_URL_PATH, path)
+            url_path = urllib.parse.quote(url_path)
+            self.client.putrequest('GET', url_path)
+            self.client.endheaders()
+            xml_doc = self.client.getresponse()
+            return lxml_etree.parse(xml_doc)
+
+        # request XSD and XML
+        xsd = request_xsd()
+        xml_doc = request_xmldoc()
+        # do validation
+        self.assertTrue(xsd.validate(xml_doc))
+
+        # validate each paths in DUMMY_DATA
+        for path in stats_httpd.item_name_list(DUMMY_DATA, ''):
+            # request XML
+            xml_doc = request_xmldoc(path)
+            # do validation
+            self.assertTrue(xsd.validate(xml_doc))
+
 class TestHttpServerError(unittest.TestCase):
     """Tests for HttpServerError exception"""
     def test_raises(self):