diff --git a/xblock/test/django/test_request.py b/xblock/test/django/test_request.py index b2e0a3035..0ca88dd22 100644 --- a/xblock/test/django/test_request.py +++ b/xblock/test/django/test_request.py @@ -44,7 +44,7 @@ def test_post_already_read(self): @pytest.mark.skipif(not HAS_DJANGO, reason='Django not available') class TestDjangoWebobResponse(TestCase): """ - Tests of the webob_to_django_response function + Tests of the webob_to_django_response function with streaming=False """ def _as_django(self, *args, **kwargs): """ @@ -82,3 +82,38 @@ def test_content_types(self): self._as_django(content_type='text/html')['Content-Type'], 'text/html; charset=UTF-8' ) + + +@pytest.mark.skipif(not HAS_DJANGO, reason='Django not available') +class TestDjangoWebobResponseStreamed(TestCase): + """ + Tests of the webob_to_django_response function with streaming=True + """ + def _as_django(self, *args, **kwargs): + """ + Return a :class:`django.http.HttpResponse` created from a `webob.Response` + initialized with `*args` and `**kwargs` + """ + return webob_to_django_response(Response(*args, **kwargs), streaming=True) + + def test_status_code(self): + self.assertEqual(self._as_django(status=200).status_code, 200) + self.assertEqual(self._as_django(status=404).status_code, 404) + self.assertEqual(self._as_django(status=500).status_code, 500) + + def test_headers(self): + self.assertIn('X-Foo', self._as_django(headerlist=[('X-Foo', 'bar')])) + self.assertEqual(self._as_django(headerlist=[('X-Foo', 'bar')])['X-Foo'], 'bar') + + def test_content_types(self): + # JSON content type (no charset should be returned) + self.assertEqual( + self._as_django(content_type='application/json')['Content-Type'], + 'application/json' + ) + + # HTML content type (UTF-8 charset should be returned) + self.assertEqual( + self._as_django(content_type='text/html')['Content-Type'], + 'text/html; charset=UTF-8' + )