-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2920 from andrewbaldwin44/feature/csrf-example
Add CSRF example
- Loading branch information
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from locust import HttpUser, between, task | ||
|
||
import re | ||
|
||
|
||
class WebsiteUser(HttpUser): | ||
host = "http://127.0.0.1:8089" | ||
wait_time = between(2, 5) | ||
|
||
@task | ||
def authenticate(self): | ||
with self.client.get("/sign-in", catch_response=True) as response: | ||
match = re.search( | ||
r'<form.*name="authenticity_token"[^>]*value="([^"]*)"', | ||
response.text, | ||
) | ||
token = match.group(1) | ||
|
||
with self.client.post( | ||
"/sign-in", | ||
{ | ||
"user[email]": "username", | ||
"user[password]": "password", | ||
"authenticity_token": token, | ||
}, | ||
catch_response=True, | ||
) as response: | ||
if "welcome" not in response.url: | ||
response.failure("Login failed") |