Skip to content

Commit 4e0617b

Browse files
mattwang44claude
andcommitted
test: fix unit tests after content_type property change
- Add null check for request.content_type in decorator to prevent errors - Fix test utility to set content type in initial request creation instead of META - Ensures proper content_type property behavior for both WSGI and ASGI requests 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent d43a268 commit 4e0617b

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

data_spec_validator/decorator/decorators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def _collect_data(method, req_qp, req_data) -> Dict:
104104
return req_data
105105

106106
def _get_dj_payload(request):
107-
if 'application/json' in request.content_type:
107+
if request.content_type and 'application/json' in request.content_type:
108108
try:
109109
return request.body and json.loads(request.body) or {}
110110
except Exception:

test/utils.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,36 @@ def make_request(cls, path='/', method='GET', user=None, headers=None, data=None
4343
kwargs = {'REQUEST_METHOD': method, 'PATH_INFO': path, 'wsgi.input': StringIO()}
4444
if qs:
4545
kwargs.update({'QUERY_STRING': qs})
46+
47+
# Set content type in initial environ if data is provided
48+
if data and method in ['POST', 'PUT', 'PATCH', 'DELETE']:
49+
kwargs.update(
50+
{
51+
'CONTENT_TYPE': 'application/json' if is_json else 'application/x-www-form-urlencoded',
52+
'CONTENT_LENGTH': len(str(data)),
53+
}
54+
)
55+
4656
req = WSGIRequest(kwargs)
4757
else:
4858
kwargs = {'path': path, 'method': method}
4959
if qs:
5060
kwargs.update({'query_string': qs})
61+
62+
# Set content type in initial scope if data is provided
63+
if data and method in ['POST', 'PUT', 'PATCH', 'DELETE']:
64+
headers = kwargs.get('headers', [])
65+
headers.extend(
66+
[
67+
[
68+
b'content-type',
69+
('application/json' if is_json else 'application/x-www-form-urlencoded').encode(),
70+
],
71+
[b'content-length', str(len(str(data))).encode()],
72+
]
73+
)
74+
kwargs['headers'] = headers
75+
5176
req = ASGIRequest(kwargs, StringIO())
5277

5378
req.user = user
@@ -60,12 +85,6 @@ def make_request(cls, path='/', method='GET', user=None, headers=None, data=None
6085
setattr(req, 'GET', data)
6186
elif method in ['POST', 'PUT', 'PATCH', 'DELETE']:
6287
req.read() # trigger RawPostDataException and force DRF to load data from req.POST
63-
req.META.update(
64-
{
65-
'CONTENT_TYPE': 'application/json' if is_json else 'application/x-www-form-urlencoded',
66-
'CONTENT_LENGTH': len(str(data)),
67-
}
68-
)
6988
if is_json:
7089
req._body = data
7190
req.POST = {}

0 commit comments

Comments
 (0)