Reputation: 53843
I've got an endpoint which I built with Django Rest Framework. It works great when I test it with something like Postman, but my unit tests fail.
When I test my JSON endpoints I always post Python dicts instead of JSON strings, like this:
response = self.client.post('/json-endpoint/', {'a': 1}, format='json')
When I test the XML endpoint I tried to post a raw xml string like this:
response = self.client.post('/xml-endpoint/', '<?xml version="1.0" encoding="UTF-8" ?><myDataStructure></myDataStructure>', format='xml')
But this doesn't work. In my Viewset I override the create()
method, and in there, my XML somehow seems to be "packaged" into another XML. If I print out request.body
in the create() method I see this:
b'<?xml version="1.0" encoding="utf-8"?>\n<root><?xml version="1.0" encoding="UTF-8" ?>\n<myDataStructure>\n etc..
As you can see my XML is somehow encoded and then packed into another <?xml version="1.0" encoding="utf-8"?>\n<root>
.
How can I properly provide the XML when I write unit tests for the XML endpoint?
Upvotes: 0
Views: 353
Reputation: 1078
In unit tests I use this:
result = self.client.post(
path='/xml-endpoint/',
data='<?xml version="1.0" encoding="UTF-8" ?><myDataStructure></myDataStructure>',
content_type='text/xml',
)
Upvotes: 1