Hi,

I was not sure it was in the documentation but it is already:
For drf: http://www.django-rest-framework.org/api-guide/testing/#api-test-cases
For Django: 
https://docs.djangoproject.com/en/2.0/topics/testing/tools/#customizing-the-test-client

Regards,
Xavier.

> Le 23 janv. 2018 à 05:54, Leo T <[email protected]> a écrit :
> 
> Thanks Xavier.
> 
> For anyone who might run into a similar problem with the same setup: I got it 
> working by overriding the request client in the testcase's setup method.
> 
> Is there a reason to use unittest testcase instead of Django testcase? If 
> not, would it make sense to add this caveat in the doc?
> 
> On Monday, January 22, 2018 at 4:53:38 PM UTC-5, Xavier Ordoquy wrote:
> No, you are not. See 
> https://github.com/django/django/blob/master/django/test/testcases.py#L221 
> <https://github.com/django/django/blob/master/django/test/testcases.py#L221>
> 
>> Le 22 janv. 2018 à 22:47, Leo T <leo.t...@ <>gmail.com <http://gmail.com/>> 
>> a écrit :
>> 
>> Hi Xavier, I am using DRF's RequestsClient, here is the full code:
>> 
>> from django.test import TestCase
>> from dummy.models import Dummy
>> from rest_framework.test import RequestsClient
>> from requests import Session
>> from user.models import User
>> 
>> class DummyTestCase(TestCase):
>>      
>>      client = RequestsClient()
>>      url = '/dummies/'
>>      headers = {'content-type': 'application/json'}
>>      token = ''
>>      headers = {}
>> 
>>      def setUp(self):
>>              Dummy.objects.create(name='this is a dummy test')
>>              User.objects.create_user('username', 'email', 'Password1234')
>>              result = self.client.post('/api-token-auth/', 
>> data={'username':'username','password':'Password1234'})
>>              self.token = result.data['token']
>>              self.headers['Authorization'] = 'JWT ' + self.token
>> 
>>      def test_can_GET_dummies(self):
>>              self.client.headers.update({'Authorization': 'JWT ' + 
>> self.token})
>>              #print(self.headers)
>>              response = self.client.get(self.url, headers=self.headers)
>>              self.assertEqual(response.status_code, 200)
>>              self.assertEqual(len(response.data), 1)
>> 
>> 
>> As you can see I am setting up a simple Django/DRF/JWT architecture and 
>> testing GET on a dummy model. Everything work fine using Postman, curl, 
>> etc.. But when I use this code and
>> 
>> python manage.py test
>> 
>> I get the above AttributeError on the headers of my RequestsClient object. 
>> If I don't include the faulty line, I get the expected HTTP 401 Unauthorized 
>> as I am not sending the JWT token (I removed Basic and Session 
>> authentication in the settings).
>> 
>> Any idea of what might cause the problem?
>> 
>> Thanks
>> 
>> 
>> 
>> On Monday, January 22, 2018 at 4:34:43 PM UTC-5, Xavier Ordoquy wrote:
>> Hi,
>> 
>> Your code sample works fine on my installation.
>> Ensure you do use the DRF’s RequestsClient and not Django’s.
>> 
>> Regards,
>> Xavier,
>> Linovia.
>> 
>>> Le 22 janv. 2018 à 22:29, Leo T <[email protected] <>> a écrit :
>>> 
>>> The following code produces the following error. Have I missed something? I 
>>> followed the attached doc.
>>> 
>>> Code:
>>> client = RequestsClient()
>>> client.headers.update({'x-test': 'true'})
>>> 
>>> Error:
>>> AttributeError: 'Client' object has no attribute 'headers'
>>> 
>>> Doc:
>>> http://www.django-rest-framework.org/api-guide/testing/#headers-authentication
>>>  
>>> <http://www.django-rest-framework.org/api-guide/testing/#headers-authentication>
>>> 
>>> 
>>> Thanks for the help
>>> 
>>> -- 
>>> You received this message because you are subscribed to the Google Groups 
>>> "Django REST framework" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an 
>>> email to [email protected] <>.
>>> For more options, visit https://groups.google.com/d/optout 
>>> <https://groups.google.com/d/optout>.
>> 
>> 
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django REST framework" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected] <>.
>> For more options, visit https://groups.google.com/d/optout 
>> <https://groups.google.com/d/optout>.
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django REST framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected] 
> <mailto:[email protected]>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to