DRF. Test APIClient, problems with request body and credentials

122 views
Skip to first unread message

Alexander December

unread,
Jul 7, 2023, 12:38:32 PM7/7/23
to Django REST framework
Hi, guys!
During last 4 hours I've tried to get clear knowledge bout the situation with post-requests made by API Client. Think, I''ve found the bug.

Briefly background - we try to test standard DRF APIView, post method but WITH our specific credentials (fundamentally, we need custom obligatory headers)
1. We create test with standard APITestCase
2. We create usual def - test_post_data like so:
response = self.client.post(
path='/my_lovely_url/',
data={'a':'a'},
format='json',
)
and ...everything works fine.
3. We need to add some custom credentials by standard method client.credentials(**kwargs) like so:
CUSTOM_CREDS = {
'CONTENT_LENGTH': 12,
'CONTENT_TYPE': 'application/json',
'ACCEPT': '*/*',
}

Passing creds into client.credentials() unexpectedly kill data we transferred to ['data'] param.
self.client.credentials(**CUSTOM_CREDS)

Checking response.content with overrided credentials will give us None no matter we passed to data-param.


CUSTOM_CREDS = {
'CONTENT_LENGTH': 12,
'CONTENT_TYPE': 'application/json',
'ACCEPT': '*/*',
}
self.client.credentials(**CUSTOM_CREDS)

response = self.client.post(
'/my_lovely_url/',
{'a':'a'},
format='json',
)
print(response.status_code) -> 200
print(response.content) -> Always None

Has anybody faced with that? Maybe there is some positive solution??
Thanks in advance



Reply all
Reply to author
Forward
0 new messages