load-test icon indicating copy to clipboard operation
load-test copied to clipboard

How to simulate GET /login method with locust?

Open zecoo opened this issue 5 years ago • 2 comments

  • Describe the expected behaviour and the actual behaviour
self.client.get("/login", headers={"Authorization":"Basic %s" % base64string})

The codes of locustfile.py which simulates the login method to sock-shop seems not work with locust return:

GET /login: "HTTPError('401 Client Error: Unauthorized for url: http://example-sock-shop/login')"  

And I checked the client.js file of sock-shop, the corresponding part of login codes is:

beforeSend: function (xhr) {
            xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
        }

which means the operation of locust is correct, but why does it not work?

Besides, I have already change the "username: password" str to base64 bytes

zecoo avatar Jul 17 '20 09:07 zecoo

did you solve this problem

I don't understand why it can log in without specifying a user name and password

NASA1nk avatar Jun 04 '22 09:06 NASA1nk

I had the same problem, but now it's solved. Please find my code below:

@task
    def login(self):
        username = 'user'
        password = 'password'
        headers = {"Authorization": "Basic " + base64.b64encode(f"{username}:{password}".encode("utf-8")).decode("ascii")}
        response = self.client.get("/login", headers=headers)
        if response.status_code == 200:
            print("Login successful")
        else:
            print("Login failed with status code: ", response.status_code)

yuhang-lin avatar Apr 11 '23 01:04 yuhang-lin