Skip to content

Commit

Permalink
Merge pull request #208 from KaczuH/ApiTest_json_failing_on_get
Browse files Browse the repository at this point in the history
Revert `APITestCase` defaulting to JSON format.
  • Loading branch information
frankwiles authored Jun 24, 2024
2 parents 5600104 + 497e675 commit 96f061e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 34 deletions.
25 changes: 0 additions & 25 deletions test_plus/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,31 +381,6 @@ def __init__(self, *args, **kwargs):
self.client_class = get_api_client()
super(APITestCase, self).__init__(*args, **kwargs)

# APITest requests now default to JSON
def request(self, method, url_name, *args, **kwargs):
data = kwargs.pop("data", {})
extra = kwargs.pop("extra", {})
kwargs["data"] = json.dumps(data) if data is not None else None

if extra and "format" in extra:
format = extra.pop("format")
else:
format = None

if extra and "content_type" in extra:
content_type = extra.pop("content_type")
else:
content_type = None

# DRF wants either `format` or `content_type` but not both
if format and content_type is None:
extra.update({"format": "json"})
else:
extra.update({"content_type": "application/json"})

kwargs["extra"] = extra
return super().request(method, url_name, *args, **kwargs)


# Note this class inherits from TestCase defined above.
class CBVTestCase(TestCase):
Expand Down
21 changes: 17 additions & 4 deletions test_project/test_app/tests/test_unittests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import json
import re
import uuid

import django
import factory
import factory.django
import sys
import unittest
Expand Down Expand Up @@ -649,13 +650,25 @@ def test_post_with_format(self):

def test_post_with_content_type(self):
data = {'testing': {'prop': 'value'}}
response = self.post('view-json', data=data, extra={'content_type': 'application/json'})
response = self.post('view-json', data=json.dumps(data), extra={'content_type': 'application/json'})
assert response["content-type"] == "application/json"
self.response_200()

def test_post_with_format_and_content_type(self):
def test_post_with_non_primitive_data_type(self):
data = {"uuid": uuid.uuid4()}
response = self.post('view-json', data=data)
assert response["content-type"] == "application/json"
self.response_200()

def test_get_with_content_type(self):
data = {'testing': {'prop': 'value'}}
response = self.post('view-json', data=data, extra={'format': 'json', 'content_type': 'application/json'})
response = self.get('view-json', data=data, extra={'content_type': 'application/json'})
assert response["content-type"] == "application/json"
self.response_200()

def test_get_with_non_primitive_data_type(self):
data = {"uuid": uuid.uuid4()}
response = self.get('view-json', data=data)
assert response["content-type"] == "application/json"
self.response_200()

Expand Down
13 changes: 8 additions & 5 deletions test_project/test_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,15 @@ def view_redirect(request):


def view_json(request):
ctype = request.META['CONTENT_TYPE']
if not ctype.startswith('application/json'):
raise ValueError("Request's content-type should be 'application/json'. Got '{}' instead.".format(ctype))
if request.method == 'POST':
ctype = request.META['CONTENT_TYPE']
if not ctype.startswith('application/json'):
raise ValueError("Request's content-type should be 'application/json'. Got '{}' instead.".format(ctype))
data = json.loads(request.body.decode('utf-8'))
return HttpResponse(json.dumps(data), content_type='application/json')

return HttpResponse('', content_type='application/json')

data = json.loads(request.body.decode('utf-8'))
return HttpResponse(json.dumps(data), content_type='application/json')


@login_required
Expand Down
4 changes: 4 additions & 0 deletions test_project/test_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,7 @@
STATIC_URL = '/static/'

TEST_RUNNER = 'test_plus.runner.NoLoggingRunner'

REST_FRAMEWORK = {
'TEST_REQUEST_DEFAULT_FORMAT': 'json',
}

0 comments on commit 96f061e

Please sign in to comment.