Skip to content

Commit

Permalink
test: Add StreamingHTTPResponse unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoBessel committed May 6, 2024
1 parent a32998e commit b8ce877
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion xblock/test/django/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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'
)

0 comments on commit b8ce877

Please sign in to comment.