From: Naoki Kambe Date: Tue, 16 Oct 2012 09:47:20 +0000 (+0900) Subject: [2298] add a unittest using XML validation with XSD X-Git-Tag: trac2487_base~1^2~26^2~8 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=cb4bb2acca32709e458227a858d710949a01d102;p=thirdparty%2Fkea.git [2298] add a unittest using XML validation with XSD 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. --- diff --git a/src/bin/stats/tests/b10-stats-httpd_test.py b/src/bin/stats/tests/b10-stats-httpd_test.py index 276a65945d..22326fba66 100644 --- a/src/bin/stats/tests/b10-stats-httpd_test.py +++ b/src/bin/stats/tests/b10-stats-httpd_test.py @@ -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):