Overview
HTTPSpec supports a comprehensive set of assertion operators to validate HTTP responses. Assertions are defined using the//# syntax in your .http or .httpspec files.
Syntax
- key: The response property to check (
status,body, orheader["name"]) - operator: The comparison operator (see table below)
- value: The expected value to compare against
Supported Operators
| Operator | Alias | Description | Type Support |
|---|---|---|---|
== | equal | Exact equality check | status, body, headers |
!= | not_equal | Not equal check | status, body, headers |
contains | - | Checks if value contains substring | status, body, headers |
not_contains | - | Checks if value does not contain substring | status, body, headers |
starts_with | - | Checks if value starts with string | status, body, headers |
ends_with | - | Checks if value ends with string | status, body, headers |
matches_regex | - | Checks if value matches regex pattern | status, body, headers |
not_matches_regex | - | Checks if value does not match regex | status, body, headers |
All operator names are case-insensitive. For example,
EQUAL, Equal, and equal are all valid.Operator Details
Equal (== or equal)
Performs exact equality comparison.
Status Code:
Header assertions use case-insensitive comparison for the expected value (source: assertion_checker.zig:207).
Not Equal (!= or not_equal)
Verifies that values are not equal.
Status Code:
Contains (contains)
Checks if the response value contains a specific substring.
Status Code:
Implementation Details
Implementation Details
The contains operator uses
std.mem.indexOf() for substring matching (source: assertion_checker.zig:242-253).Not Contains (not_contains)
Verifies that the response value does not contain a specific substring.
Status Code:
Starts With (starts_with)
Checks if the response value starts with a specific prefix.
Status Code:
This operator is defined in the parser (parser.zig:25) and tested in the test suite (assertion_checker.zig:507-560).
Ends With (ends_with)
Checks if the response value ends with a specific suffix.
Status Code:
Matches Regex (matches_regex)
Validates that the response value matches a regular expression pattern.
Status Code:
Regex Implementation
Regex Implementation
HTTPSpec uses the
regex library for pattern matching. The regex compilation and matching occurs in matchesRegex() function (assertion_checker.zig:149-157).Not Matches Regex (not_matches_regex)
Verifies that the response value does not match a regular expression pattern.
Status Code:
Assertion Keys
Status Code
Usestatus as the key to assert on HTTP status codes.
Response Body
Usebody as the key to assert on the response body content.
Response Headers
Useheader["header-name"] format to assert on specific headers.
header["..."] (assertion_checker.zig:141-147):
Type Compatibility
Status Assertions
All operators work with status codes. Status codes are internally converted to strings for comparison.Body Assertions
All operators work with response bodies. Body content is treated as a string.Header Assertions
All operators work with headers. Missing headers are treated specially:- For
==(equal): Missing header causes assertion failure with “header_missing” reason - For
!=(not_equal): Missing header is acceptable (assertion passes) - For
contains: Missing header causes assertion failure - For
not_contains: Missing header is acceptable (assertion passes) - For regex operators: Missing header causes assertion failure
Error Handling
Invalid Assertion Key
If an assertion uses an invalid key (notstatus, body, or header["..."]), the test fails with invalid_assertion_key reason.
Status Format Error
If the status assertion value cannot be parsed as an integer, the test fails withstatus_format_error reason.
Malformed Header Syntax
Header assertions must follow the exactheader["name"] format. Invalid syntax results in invalid_assertion_key error.
Examples
Complete Test Example
Error Handling Test
Regex Pattern Matching
See Also
- Troubleshooting - Debug assertion failures
- HTTP File Format - Learn about the file syntax
- Quick Start - Get started with HTTPSpec