Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
XML Webservice in Django for Debugging
from django.test import TestCase
from django.test.client import Client
import logging
logging.disable("debug")
class XMLWebserviceTest(TestCase):
c = Client()
def setUp(self):
self.valid_xml = """
<note>
<to>User</to>
<from>Me</from>
<heading>Example XML file</heading>
<body>This is a short note!</body>
</note>
"""
self.invalid_xml = self.valid_xml[:-3]
def test_get_request_should_return_505(self):
self.c_get = self.c.get('/webservice/xmldump/')
self.assertEquals(405, self.c_get.status_code)
def test_responses_with_xml(self):
self.c_with_body = self.c.post('/webservice/xmldump/', self.valid_xml, content_type="text/xml")
self.assertEqual(200, self.c_with_body.status_code)
self.assertNotEqual("", self.c_with_body.content)
def test_responses_error_no_send_xml(self):
self.c_with_body = self.c.post('/webservice/xmldump/')
self.assertEqual(500, self.c_with_body.status_code)
def test_responses_with_invalid_xml_should_receive_500(self):
self.c_with_body = self.c.post('/webservice/xmldump/', self.invalid_xml, content_type="text/xml")
self.assertEqual(500, self.c_with_body.status_code)
self.assertNotEqual("", self.c_with_body.content)
def test_responses_with_xml_no_dump(self):
self.c_nodump = self.c.post('/webservice/xmldump/?nodump', self.valid_xml, content_type="text/xml")
self.assertEqual(200, self.c_nodump.status_code)
self.assertEqual("", self.c_nodump.content)
def test_responses_with_response_code_and_no_dump(self):
self.c_response_code_and_nodump = self.c.post('/webservice/xmldump/?nodump&response_code=401', self.valid_xml,
content_type="text/xml")
self.assertEqual(401, self.c_response_code_and_nodump.status_code)
self.assertEqual("", self.c_response_code_and_nodump.content)
def test_responses_with_response_code_and_dump(self):
self.c_only_response_code = self.c.post('/webservice/xmldump/?response_code=401', self.valid_xml,
content_type="text/xml")
self.assertEqual(401, self.c_only_response_code.status_code)
self.assertEqual("", self.c_only_response_code.content)
@require_http_methods(['POST'])
def xmldump(request):
"""
This view can be used to debug a configuration on a reverse proxy oder
xml firewall.
- E.g. ?response_code=500 to receive a HTTP INTERNAL SERVER ERROR
- Send an XML file to the view and it gets back to you
- ?no_dump to omit the XML file in the response body
"""
response = HttpResponse(mimetype="text/xml;charset=UTF-8")
# specific response code is asked
response_code = request.GET.get('response_code')
if response_code:
response.status_code = int(response_code)
logger.debug("client asked for a response with %s" % response.status_code)
return response
# get data from request
try:
data = request.POST.items()[0]
logger.debug(data)
except IndexError:
data = None
response.status_code = 500
# validate xml and return if no nodump query parameter specified
try:
s_data = StringIO.StringIO()
for line in data:
s_data.write(line)
s_data.seek(0)
root = etree.parse(s_data)
logger.debug(etree.tostring(root))
# check if nodump
if request.META['QUERY_STRING'] == "nodump":
logger.debug("client asked for a empty response body %s" % response.status_code)
else:
for line in data:
response.write(line)
except Exception, e:
logger.error(e.message)
response.write(e.message)
response.status_code = 500
return response
@philipsahli

This comment has been minimized.

Copy link
Owner Author

philipsahli commented Jun 24, 2013

This code is now available in the repository fatrix/testws

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.