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] <javascript:>> 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
>
>
> 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] <javascript:>.
> For more options, visit 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.