Performance Testing with Locust (Part – 4)

Written by Divyanshi Bhamri, QA Engineer at MyGate

1. Introduction


Hello again! This is the fourth part of my series on performance testing with Locust. I hope you all enjoyed my previous posts.

In this article we will talk about sending the GET and POST authentication requests using Locust.

Note: We are going to use hosts similar to reqres [1] restful web services to demonstrate our examples.

Authentication can be done in 2 ways such as:-

  1. Cookie based
  2. Token based

We will be covering token based authentications. Token can either be put in request headers or request body.

2. Case: GET Request Using Token in Request Headers


Note: For GET authenticate requests, a token can only be put in the request headers, since GET does not have any body.

We will be using https://demo.testfire.net/swagger/index.html, a swagger file which can be used to make an API request to a particular endpoint.

Try to grab the authorization token, using the swagger link mentioned above.

Step 1: Save this script with .py extension. I have saved this script as auth2.py.

Step 2: Run the following command.

Step 3: Open a browser and navigate to http://localhost:8089

Step 4: Upon opening the page, enter the number of users and hatch rate. Click on the ‘Start swarming’ button.

Step 5: Check the locust UI, in which the test starts automatically.

Note: In this approach we first get the authorization token using one of the api endpoints in the swagger file and then we put the same token in the headers section with the key ‘Authorization’ in the python script.

3. Post Request Cases

3.1. Case: Sending POST request with token in the Body Parameter

Step 1: Save this script with .py extension. I have saved this script as auth.py.

Step 2: Run the following command.

Step 3: Open a browser and navigate to http://localhost:8089

Step 4: Upon opening the page, enter the number of users and hatch rate. Click on the ‘Start swarming’ button.

Step 5: Check the locust UI in which the test starts automatically.

Note: I have used https://app.fakejson.com [3] for testing this endpoint. Just signup, get the token and replace the token field with yours. The token in this script is placed in the body parameter with the key set as ‘token’.

3.2. Case: Sending POST request with token in the Request Header


Step 1: Save this script with .py extension. I have saved this script as auth3.py.

Step 2: Run the following command.

Step 3: Open a browser and navigate to http://localhost:8089
Step 4: Upon opening the page, enter the number of users and hatch rate. Click on the ‘Start swarming’ button.

Step 5: Check the locust UI in which the test starts automatically.

Note: In this approach we first get the authorization token using one of the API endpoints in the swagger file and then we put the same token in the headers with the key set as ‘Authorization’ in the python script.

4. Conclusion


This is the fourth article of the series. In the next part we will learn about some more interesting aspects of Locust [4].

Thanks for reading. I hope this article provides you with a better idea about how to send an authenticated request for both GET and POST methods.

References

[1] Mock API
https://reqres.in/api
[2] Vulnerable Application
https://demo.testfire.net/
[3] Fake Online REST API for Testing and Prototyping
https://app.fakejson.com
[4] Locust Documentation.
https://docs.locust.io/

Disclaimer

This is a curated post of an article published at Medium.

Admin

MyGate is building technology to simplify check-ins everywhere. Our flagship solution, launched in 2016, currently facilitates over 2 million check-in requests every day at thousands of gated premises across the 25+ cities in which we operate, and continues to show rapid growth. We’ve strengthened our commitment to information privacy by following GDPR guidelines, the global gold standard for data privacy.

Introducing All Home Utility Payments on the MyGate app

It’s a recurring struggle to pay and manage your home utility bills via different platforms. Getting an overview of this spending is another story. To make life easy for...

Personalizing Customer Connections for Brands with Geo-Targeting

“Geo-targeting allows businesses to personalise their messagingand connect with customers on a more intimate level.”Ryan Holmes All brands are online today and reach out to their audience through online...

MyGate, convenience, and better communication

“Convenience is the catalyst that drives innovation in business.”Elon Musk Convenience is a mindful and necessary business virtue. Every brand is based on solutions to some problem. Communicating this...

A Match Made in Marketing Heaven: MyGate’s Engaged User Base

Attention brands and brand managers! It’s time to leave the old-fashioned advertising methods behind and hop on board the MyGate express! We’ve got some exciting news to share with...

Make Your Brand Unmissable On The Mygate App

“Our jobs as marketers are to understand how the customer wants to buy and help them do so.” Bryan Eisenberg, Founder & CMO at IdealSpot Brands are increasingly increasing...

Helping brands solve the problems of specific consumer tribes

“Our jobs as marketers are to understandhow the customer wants to buy and help them to do so.”– Bryan Eisenberg Personalisation is key to reaching the right audience and...