Overview
In various testing scenarios, validating the response status code is crucial to ensure the health, availability, and expected behavior of web services and applications. LoadForge provides a straightforward mechanism to set expected status codes and automatically consider responses as failures if they don't match the expected status. This feature proves invaluable when validating APIs, ensuring error handling is in place, and monitoring web service behaviors.
Expecting Specific Status Codes
By default, LoadForge will regard any HTTP response with a status code other than 2xx (e.g., 200 OK) as a failure. However, there are scenarios where you might want to specifically test for error responses. One common case is to verify that a certain endpoint returns a 404 Not Found error as expected.
For instance, if you have an endpoint that should not be accessible and needs to return a 404 error, you can script your test to explicitly check for this.
Here's a sample code snippet demonstrating how you can test for a 404 status code:
Sample Code
import time
from locust import HttpUser, task, between
class QuickstartUser(HttpUser):
wait_time = between(3, 5)
@task(1)
def index_page(self):
# Basic GET request
self.client.get("/")
# Checking for a specific status code (404 in this case)
with self.client.get("/does_not_exist/", catch_response=True) as response:
if response.status_code == 404:
response.success()
else:
response.failure(f"Unexpected status code: {response.status_code}")
In this example, the test checks the /does_not_exist/
endpoint. If the status code is 404, the test passes; otherwise, an error is reported in your LoadForge report.
Supercharge Your Testing with LoadForge
Locust Test Example
LoadForge leverages the power of the open-source tool, locust. If you're already a locust user, you can directly import your existing locust scripts into LoadForge to take your testing to the next level with more robust features and expansive infrastructure.