获取“未提供身份验证凭据”在蝗虫上测试DRF端点时
我正在尝试加载测试端点,该端点允许用户使用蝗虫创建帖子,值得一提的是,在访问此端点之前,需要对用户进行身份验证。
问题: 当我使用Locust达到端点时,我会一直遇到错误,说“未提供身份验证”即使我这样做了,我也配置了我的端点以接受令牌(我使用的是默认的DRF令牌功能)。知道我做错了什么吗?任何帮助将不胜感激。以下是我的代码:
locust文件
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"})
视图文件
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)
我还应该注意,端点在Postman上正常工作,只有在使用Locust进行负载测试时,我才会出现错误
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定这是否能帮助您找到真正的问题,但您可以通过检查
response.request
来调查发送的确切负载,如下所示:print(response.request.headers)
打印(response.request.data)
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)
我终于找到了解决该问题的解决方案,我要做的就是将
tokenauthentication
添加到settings.py文件中的默认身份验证类列表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