Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework Testing response headers

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
like image 529
Dean Christian Armada Avatar asked Jul 23 '16 13:07

Dean Christian Armada


People also ask

How do I check Django REST framework?

For Django REST Framework to work on top of Django, you need to add rest_framework in INSTALLED_APPS, in settings.py. Bingo..!! Django REST Framework is successfully installed, one case use it in any app of Django.

What is renderers in Django REST framework?

The rendering process takes the intermediate representation of template and context, and turns it into the final byte stream that can be served to the client. REST framework includes a number of built in Renderer classes, that allow you to return responses with various media types.

What is HttpResponse in Django?

HttpResponse Methods – Django It is used to instantiate an HttpResponse object with the given page content and content type. HttpResponse.__setitem__(header, value) It is used to set the given header name to the given value.

What is HyperlinkedModelSerializer in Django?

HyperlinkedModelSerializer is a layer of abstraction over the default serializer that allows to quickly create a serializer for a model in Django. Django REST Framework is a wrapper over default Django Framework, basically used to create APIs of various kinds.


1 Answers

There are several options:

>>> response.has_header('Location')
True

>>> response.get('Location')  # None if key not in headers
My location

>>> response['Location']  # KeyError if key doesn't exist
My location

>>> response._headers  # headers as dict
{'allow': ('Allow', 'GET, POST, HEAD, OPTIONS'), 'Location': ...}

>>> response.serialize_headers()  # headers as bytestring (in Python 3)
b'Allow: GET, POST, HEAD, OPTIONS\r\nLocation: ...'
like image 168
yofee Avatar answered Oct 21 '22 12:10

yofee