Skip to content

HTTPTest API Reference

Class for performing REST API integration tests via HTTP requests.

from rapidtest import HTTPTest

Constructor

HTTPTest(
    url: str,
    timeout: int = 30
)

Parameters: - url (str): The base URL of the API (e.g., 'http://localhost:8000'). Required. - timeout (int): Request timeout in seconds. Default is 30.

Example:

api = HTTPTest(url="http://localhost:8000", timeout=30)

HTTP Methods

All HTTP methods share the following common parameters:

Parameter Type Default Description
path str Required The API endpoint to call (e.g., '/users')
status int 200/201/204 Expected HTTP status code
expected_json dict None Expected JSON body in response
keys list[str] None A subset of JSON keys that should be contained in the response
json dict/list/str/int/float/bool None JSON data to send in request body
data str/bytes/dict None Request body data
params dict None Query parameters for the URL
headers dict None Additional HTTP headers

GET Request

api.get(
    path="/users",
    status=200,
    headers={"Authorization": "Bearer token"},
    params={"page": 1}
)

POST Request

user_data = {"username": "john", "email": "john@example.com"}
api.post(
    path="/users",
    json=user_data,
    status=201,
    expected_json=user_data
)

PUT Request

api.put(
    path="/users/1",
    json={"name": "Updated Name"},
    status=200
)

PATCH Request

api.patch(
    path="/users/1",
    json={"email": "newemail@example.com"},
    status=200
)

DELETE Request

api.delete(
    path="/users/1",
    status=204
)

Response Validation

HTTPTest automatically validates:

  1. Status Code: Compares actual vs expected status code
  2. Response Body: Compares actual JSON response vs expected_json (if provided)
  3. Response Keys: Validates the presence of expected keys (if keys is provided)

Error Handling

  • Connection Errors: Displays clear error messages for network issues
  • Status Code Mismatches: Shows expected vs actual status codes
  • Body Mismatches: Highlights differences in response bodies
  • JSON Parsing Errors: Gracefully handles non-JSON responses

Return Values

All HTTP methods return a Response object on success, or None if a critical connection error occurred.

You can access properties directly: - response.status_code: HTTP status code - response.json(): JSON response body - response.text: Raw response text
- response.headers: Response headers