I am trying to load test an endpoint that allows users to create posts using locust, it is worth mentioning that users need to be authenticated before they can access this endpoint.
Problem: When I hit the endpoint using locust, I keep getting the error saying "Authentication provided were not provided" even though I have done so and I have also configured my endpoint to accept token (I am using the default DRF token feature). Any idea what I am doing wrong? any help will be appreciated. Below is my code:
Locust file
class ListPostUser(HttpUser):
wait_time = between(1, 5)
@task(3)
def create_post(self):
data = {
"title": "This is another random title",
"body": "This is the body of a randomly titled post"
}
headers = {
"Authorization": "Token 508e650b0ca1613818939089190a1661a75865b1"
}
response = self.client.post("blog/create", json=data, headers=headers)
print(response)
print(response.json())
@task
def comment_detail(self):
self.client.get("blog/comment/1")
@task(2)
def post_detail(self):
self.client.get("blog/post/1")
def on_start(self):
self.client.post("login/", json={"username": "username", "password": "mypassword"})
views file
class CreatePostAPI(APIView):
permission_classes = (IsAuthenticated, )
def post(self, request):
serializer = CreatePostSerializer(data=request.data)
if serializer.is_valid():
serializer.save(user=self.request.user)
return Response(serializer.data, status=status.HTTP_200_OK)
I should also note that the endpoint works fine on POSTMAN, I only get the error when I am load testing with locust
CodePudding user response:
Not sure if this will help you find the real issue, but you can investigate the exact payload sent by examining response.request
, like this:
print(response.request.headers)
print(response.request.data)
CodePudding user response:
I finally found a solution to the issue, All I needed to do was add TokenAuthentication
to the list of default authentication classes in settings.py file
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES':
[
'rest_framework.authentication.TokenAuthentication',
...
],
}