How to save session and CSRF token in locus test

I have not tested my django web application using locust.io. In the ha form, I have a problem that it is protected by the CSRF token. I do the following:

class WebsiteTasks(TaskSet): def on_start(self): print("On start") @task def post_answer(self): self.client.get("/polls/2/vote") self.client.post("/polls/2/vote/", {"choice": "8"}) 

Why am I getting 403 error? That the message is open, locust documentation says that client objects maintain a session of life.

+5
source share
2 answers

change your code as:

 @task def post_answer(self): response = self.client.get("/polls/2/vote") csrftoken = response.cookies['csrftoken'] self.client.post("/polls/2/vote/", {"choice": "8"}, headers={"X-CSRFToken": csrftoken}) 
+13
source

I ran into this problem when running the Locust test against Django 1.8.5 and requested adding the csrf token to the cookies, headers and forms of the POST data, as well as below, so as not to catch 403. Something like:

 @task def post_answer(self): response = self.client.get("/polls/2/vote") csrftoken = response.cookies['csrftoken'] self.client.post("/polls/2/vote/", {"choice": "8", "csrfmiddlewaretoken": csrftoken}, headers={"X-CSRFToken": csrftoken}, cookies={"csrftoken": csrftoken}) 
+1
source

Source: https://habr.com/ru/post/1206430/


All Articles