Add test view mixin to check for responses content types. Relates to GitLab issue #311.

This commit is contained in:
Roberto Rosario
2016-10-21 03:41:15 -04:00
parent 91144a749f
commit a44c76165b
2 changed files with 26 additions and 2 deletions

View File

@@ -2,10 +2,10 @@ from __future__ import unicode_literals
from django.test import TestCase
from .mixins import OpenFileCheckMixin, TempfileCheckMixin
from .mixins import ContentTypeCheckMixin, OpenFileCheckMixin, TempfileCheckMixin
class BaseTestCase(OpenFileCheckMixin, TempfileCheckMixin, TestCase):
class BaseTestCase(ContentTypeCheckMixin, OpenFileCheckMixin, TempfileCheckMixin, TestCase):
"""
This is the most basic test case class any test in the project should use.
"""

View File

@@ -7,6 +7,30 @@ import psutil
from ..settings import setting_temporary_directory
class ContentTypeCheckMixin(object):
expected_content_type = 'text/html; charset=utf-8'
def _pre_setup(self):
super(ContentTypeCheckMixin, self)._pre_setup()
test_instance = self
class CustomClient(self.client_class):
def request(self, *args, **kwargs):
response = super(CustomClient, self).request(*args, **kwargs)
content_type = response._headers['content-type'][1]
test_instance.assertEqual(
content_type, test_instance.expected_content_type,
msg='Unexpected response content type: {}, expected: {}.'.format(
content_type, test_instance.expected_content_type
)
)
return response
self.client = CustomClient()
class TempfileCheckMixin(object):
def _get_temporary_entries_count(self):
return len(os.listdir(setting_temporary_directory.value))