Index ¦ Archives ¦ Atom ¦ RSS

Handle CSRF token in Locust test

Locust and Django CSRF

Simple solution to handle Django Cross Site Request Forgery protection into Locust load tests

class UserBehavior(TaskSet):
    def on_start(self):
        self.login()

    def login(self):
        response = self.client.get("/auth/login/")
        csrftoken = response.cookies['csrftoken']
        response = self.client.post("/auth/login/", 
                                    {"username": "admin",
                                     "password": "password"},
                                    headers={"X-CSRFToken": csrftoken})


class WebsiteUser(HttpLocust):
    host = 'http://localhost:8000'
    task_set = UserBehavior

© 2014 Stefano Apostolico. Built using Pelican. Member of the Internet Defense League.