Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locust passing headers

Is it possible to have Locust pass a header command with a secure token to load test an API?

I am trying to test our api for an encoder with a header flag for a token as the server being tested has to receive a token with the request, ie.

curl -H “Authorization: Token token string” http://someserver

like image 908
Liam Douglas Avatar asked Aug 24 '18 15:08

Liam Douglas


2 Answers

Yes, you can use:

token_string = "token string"

resp = self.client.post(
            url="http://someserver",
            data=json.dumps(data),
            auth=None,
            headers={"authorization": "Token " + token_string},
            name="http://someserver",
        )
like image 101
Matt Doyle Avatar answered Sep 17 '22 04:09

Matt Doyle


If you want to use same headers for every request you can also set them to the client in the on_start method. They will be automatically used with every client request.

class User(HttpUser):

    def on_start(self):
        self.client.headers = {'Authorization': 'my-auth-token'}

    @task
    def my_authenticated_task(self):
        self.client.post('enspoint')  # this will use headers we set earlier
like image 25
matusko Avatar answered Sep 20 '22 04:09

matusko