Skip to content

Commit

Permalink
Merge pull request #53 from goodtune/assertResponseHeaders
Browse files Browse the repository at this point in the history
Add assertResponseHeaders
  • Loading branch information
frankwiles committed Jan 31, 2017
2 parents e9a808d + f0b3e7b commit c3cacdf
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
16 changes: 16 additions & 0 deletions docs/methods.rst
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,22 @@ the chunk of HTML::
self.get('hello-world')
self.assertResponseNotContains('<p>Hello, Frank!</p>')

assertResponseHeaders(headers, response=None)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Sometimes your views or middleware will set custom headers::

def test_custom_headers(self):
self.get('my-url-name')
self.assertResponseHeaders({'X-Custom-Header': 'Foo'})
self.assertResponseHeaders({'X-Does-Not-Exist': None})

You might also want to check standard headers::

def test_content_type(self):
self.get('my-json-view')
self.assertResponseHeaders({'Content-Type': 'application/json'})

get\_check\_200(url\_name, \*args, \*\*kwargs)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
16 changes: 16 additions & 0 deletions test_plus/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,22 @@ def assertResponseNotContains(self, text, response=None, html=True, **kwargs):
response = self._which_response(response)
self.assertNotContains(response, text, html=html, **kwargs)

def assertResponseHeaders(self, headers, response=None):
"""
Check that the headers in the response are as expected.
Only headers defined in `headers` are compared, other keys present on
the `response` will be ignored.
:param headers: Mapping of header names to expected values
:type headers: :class:`collections.Mapping`
:param response: Response to check headers against
:type response: :class:`django.http.response.HttpResponse`
"""
response = self._which_response(response)
compare = {h: response.get(h) for h in headers}
self.assertEqual(compare, headers)

def get_context(self, key):
if self.last_response is not None:
self.assertTrue(key in self.last_response.context)
Expand Down
7 changes: 7 additions & 0 deletions test_project/test_app/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,13 @@ def test_assertresponsecontains(self):
self.assertResponseContains('<p>Hello world</p>')
self.assertResponseNotContains('<p>Hello Frank</p>')

def test_assert_response_headers(self):
self.get('view-headers')
self.assertResponseHeaders({'Content-Type': 'text/plain'})
self.assertResponseHeaders({'X-Custom': '1'})
self.assertResponseHeaders({'X-Custom': '1', 'X-Non-Existent': None})
self.assertResponseHeaders({'X-Non-Existent': None})


class TestPlusCBViewTests(CBVTestCase):

Expand Down
9 changes: 5 additions & 4 deletions test_project/test_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from django.conf.urls.defaults import url, include

from .views import (
data_1, data_5, needs_login, view_200, view_201, view_301, view_302,
view_400, view_401, view_403, view_404, view_405, view_410,
view_contains, view_context_with, view_context_without,
view_is_ajax, view_redirect, FormErrors
FormErrors, data_1, data_5, needs_login, view_200, view_201, view_301,
view_302, view_400, view_401, view_403, view_404, view_405, view_410,
view_contains, view_context_with, view_context_without, view_headers,
view_is_ajax, view_redirect,
)

urlpatterns = [
Expand All @@ -31,4 +31,5 @@
url(r'^view/isajax/$', view_is_ajax, name='view-is-ajax'),
url(r'^view/contains/$', view_contains, name='view-contains'),
url(r'^view/form-errors/$', FormErrors.as_view(), name='form-errors'),
url(r'^view/headers/$', view_headers, name='view-headers'),
]
6 changes: 6 additions & 0 deletions test_project/test_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ def view_contains(request):
return render(request, 'test.html', {})


def view_headers(request):
response = HttpResponse('', content_type='text/plain', status=200)
response['X-Custom'] = 1
return response


# Class-based test views

class CBView(generic.View):
Expand Down

0 comments on commit c3cacdf

Please sign in to comment.