
Tags, Automated Tests, Run Updates and more
We've got a heap of new updates for you, focused on multi-user organizations and easier test creation. Tag System We...
Testing REST-based HTTP APIs with Bearer Token Authentication
You are now browsing the LoadForge locust test directory. You can use these tests as a starting point for your own tests, or use our AI wizard to generate one automatically.
Load testing a REST API is streamlined and intuitive with LoadForge. To demonstrate the capability, we've used our own REST API in the examples below. LoadForge provides the flexibility to define custom headers as required, and the ability to send requests based on the data returned from your API.
Ensure you disable API rate limits associated with your token to prevent an influx of failure responses during testing.
Below are two illustrative examples:
The following Python script demonstrates how to conduct simple GET requests to your API with bearer token authentication:
from locust import HttpUser, task, between
class QuickstartUser(HttpUser):
# The simulated user will pause for a duration between 5 and 9 seconds before executing the tasks.
wait_time = between(5, 9)
def on_start(self):
# Initializing the Authorization header with the bearer token.
self.client.headers.update({'Authorization': 'Bearer TOKEN_HERE'})
@task(1)
def index_page(self):
# Making four GET requests every wait_time duration.
self.client.get("/api/hosts")
self.client.get("/api/tests")
self.client.get("/api/result/1488")
self.client.get("/api/result/1463")
This test provides insights into load testing the LoadForge REST API. The script below contains comprehensive comments to elucidate each step:
The test script comprises four main segments:
This exemplifies the convenience of load testing, be it for a Laravel API or any RESTful API. The capacity to retrieve all results and then fetch random result IDs is particularly beneficial, given the inherent ability to parse responses within the test.
import random
from locust import HttpUser, task, between
class AdvancedUser(HttpUser):
wait_time = between(5, 9)
def get_results(self):
self.results = []
# Retrieve all results and store their unique IDs for subsequent tasks.
response = self.client.get("/api/results")
for result in response.json():
self.results.append(result['id'])
def on_start(self):
# Distribute start times among users.
self.wait()
# Initialize the Bearer authentication token.
self.client.headers.update({'Authorization': 'Bearer TOKEN_HERE'})
# Fetch and store all potential results.
self.get_results()
@task(1)
def read_hosts(self):
self.client.get("/api/hosts")
@task(2)
def read_tests(self):
self.client.get("/api/tests")
@task(5)
def read_results(self):
# Fetch a random result based on the previously retrieved list.
self.client.get("/api/result/" + str(random.choice(self.results)))
@task(1)
def crud_host(self):
# Create a new Host and extract its details from the response.
response = self.client.post("/api/hosts/create", {'url': 'www.test.com', 'port': '443', 'protocol': 'https'})
host = response.json()
# Clean up by deleting the recently created host.
self.client.post("/api/hosts/delete", {"host_id": host['host_id']})
LoadForge is built upon the robust foundation of locust, allowing open-source locust users to readily utilize this script. If you're already a locust aficionado, think about importing your script to LoadForge and elevate your testing endeavors!