Skip to content

Instantly share code, notes, and snippets.

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
class XMLWebserviceTest(TestCase):
c = Client()
def setUp(self):
self.valid_xml = """
<heading>Example XML file</heading>
<body>This is a short note!</body>
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 ='/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 ='/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 ='/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 ='/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 ='/webservice/xmldump/?nodump&response_code=401', self.valid_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 ='/webservice/xmldump/?response_code=401', self.valid_xml,
self.assertEqual(401, self.c_only_response_code.status_code)
self.assertEqual("", self.c_only_response_code.content)
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
data = request.POST.items()[0]
except IndexError:
data = None
response.status_code = 500
# validate xml and return if no nodump query parameter specified
s_data = StringIO.StringIO()
for line in data:
root = etree.parse(s_data)
# check if nodump
if request.META['QUERY_STRING'] == "nodump":
logger.debug("client asked for a empty response body %s" % response.status_code)
for line in data:
except Exception, e:
response.status_code = 500
return response

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.