Skip to content

Commit

Permalink
Fixed regression that tests using format still work
Browse files Browse the repository at this point in the history
Error only occurred on tests which return no content and use
a renderer without charset (e.g. JSONRenderer)
  • Loading branch information
sliverc committed Jan 7, 2025
1 parent 089f6a6 commit ff6b519
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion rest_framework/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def _encode_data(self, data, format=None, content_type=None):
ret = renderer.render(data)

# Coerce text to bytes if required.
if isinstance(ret, str):
if isinstance(ret, str) and renderer.charset:
ret = ret.encode(renderer.charset)

return ret, content_type
Expand Down
14 changes: 12 additions & 2 deletions tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from django.test import TestCase, override_settings
from django.urls import path

from rest_framework import fields, parsers, serializers
from rest_framework import fields, parsers, renderers, serializers, status
from rest_framework.authtoken.models import Token
from rest_framework.decorators import api_view, parser_classes
from rest_framework.decorators import api_view, parser_classes, renderer_classes
from rest_framework.response import Response
from rest_framework.test import (
APIClient, APIRequestFactory, URLPatternsTestCase, force_authenticate
Expand Down Expand Up @@ -55,6 +55,10 @@ class BasicSerializer(serializers.Serializer):
def post_json_view(request):
return Response(request.data)

@api_view(['DELETE'])
@renderer_classes((renderers.JSONRenderer, ))
def delete_json_view(request):
return Response(status=status.HTTP_204_NO_CONTENT)

@api_view(['POST'])
def post_view(request):
Expand All @@ -69,6 +73,7 @@ def post_view(request):
path('redirect-view/', redirect_view),
path('redirect-view/<int:code>/', redirect_307_308_view),
path('post-json-view/', post_json_view),
path('delete-json-view/', delete_json_view),
path('post-view/', post_view),
]

Expand Down Expand Up @@ -254,6 +259,11 @@ def test_post_encodes_data_based_on_json_content_type(self):
assert response.status_code == 200
assert response.data == data

def test_delete_based_on_format(self):
response = self.client.delete('/delete-json-view/', format='json')
assert response.status_code == status.HTTP_204_NO_CONTENT
assert response.data is None


class TestAPIRequestFactory(TestCase):
def test_csrf_exempt_by_default(self):
Expand Down

0 comments on commit ff6b519

Please sign in to comment.