I am trying to create automated tests and what I want is to check in my TestCase if there is a location header which it should be based on my code in views.py(Already tested it in Advanced REST Client). However, I can not to parse it in my tests.py
Here is my code:
from rest_framework import status
from rest_framework.test import APITestCase
url_1 = reverse('artists-list')
class ArtistTest(APITestCase):
# Check the response if there is no data
def test_get(self):
# Checks the artists
# self.client attribute will be an APIClient instance
# Basically it will act as a client
response = self.client.get(url_1)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.assertEqual(response.content, '') # There should be no data
# self.assertEqual(len(data), 0)
# print ("%s.%s DONE - 1" % (self.__class__.__name__, inspect.stack()[0][3]))
def test_post(self):
_data = {"name": "50 Cent", "birth_date":"2005-02-13"}
response = self.client.post(url_1, _data)
print "----"
print response.headers
data = json.loads(response.content)["data"]
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(data, _data)
self.assertEqual(Artist.objects.count(), 1)
self.assertEqual(Artist.objects.get().name, '50 Cent')
P.S.
Please be aware that:
print response.headers # this throws an error
print response # shows the header but I want it to be parsed