Commit f98c6a80 authored by Craig Weber's avatar Craig Weber

Add better SOAP testing tools

parent 7b899faa
Pipeline #1588219 passed with stage
......@@ -44,6 +44,13 @@ Usage
Changelog
=========
1.1.0
------------------
- Adds some better testing tools for consumer code.
- New class `soap.tests.XMLAssertions` with methods: `assertNodeCount`, `assertNodeText`, `assertNodeAttributes`. Uses `lxml` if it is installed, but otherwise falls back to `elementree`.
- Class `soap.tests.SoapTest` now inherits from `soap.tests.XMLAssertions`
- Add optional parameter `test_request` to `soap.tests.SoapTest._build_transport_with_reply`. Pass in a function to have it called (with the request as a parameter) before the mocked reply is sent. Useful to write unit tests to make sure your SOAP requests are well-formed.
1.0.2
------------------
- Use docker executor for tests.
......
......@@ -7,12 +7,60 @@ import re
from .http import HttpTransport
class SoapTest(object):
try:
from lxml import etree
except ImportError:
try:
# Python 2.5
import xml.etree.cElementTree as etree
except ImportError:
try:
# Python 2.5
import xml.etree.ElementTree as etree
except ImportError:
try:
# normal cElementTree install
import cElementTree as etree
except ImportError:
try:
# normal ElementTree install
import elementtree.ElementTree as etree
except ImportError:
pass
class XMLAssertions(object):
def assertNodeCount(self, xml_str, xpath, num):
doc = etree.fromstring(xml_str)
nodes = doc.xpath(xpath)
self.assertEqual(num, len(nodes))
def assertNodeText(self, xml_str, xpath, expected):
doc = etree.fromstring(xml_str)
nodes = doc.xpath(xpath)
self.assertTrue(len(nodes) > 0)
for node in nodes:
self.assertEqual(expected, node.text)
def assertNodeAttributes(self, xml_str, xpath, attributes):
doc = etree.fromstring(xml_str)
nodes = doc.xpath(xpath)
self.assertTrue(len(nodes) > 0)
for node in nodes:
for attribute, value in attributes.items():
self.assertTrue(attribute in node.attrib)
self.assertEqual(value, node.attrib[attribute])
class SoapTest(XMLAssertions):
def setUp(self):
soap.clients = {}
def _build_transport_with_reply(self, body, status=200, pattern=None):
def _build_transport_with_reply(self, body, status=200, pattern=None, test_request=None):
headers = HTTPMessage()
headers.add_header('Content-Type', 'text/xml; charset=utf-8')
reply = Reply(status, headers, body)
......@@ -21,6 +69,8 @@ class SoapTest(object):
def surrogate(request, *args, **kwargs):
if pattern and not re.search(pattern, request.url):
return HttpTransport.send(transport, *args, **kwargs)
if test_request:
test_request(request)
return reply
transport.send = mock.MagicMock()
transport.send.side_effect = surrogate
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment