http-assert

A command-line tool for performing HTTP requests and asserting properties of the response. This tool is designed for testing HTTP endpoints, health checks, monitoring, and CI/CD pipelines.
Purpose
http-assert
combines the functionality of making HTTP requests with the ability to validate responses against multiple criteria. It's particularly useful for:
- Health checks and monitoring: Verify that your APIs are returning expected responses
- CI/CD pipelines: Validate deployed services before proceeding with deployment
- Integration testing: Test HTTP endpoints with various assertion conditions
- Load balancer testing: Use host mapping to test different backend servers
- SSL/TLS validation: Test secure endpoints with certificate validation options
Installation
From Source
go install github.com/korya/http-assert@latest
Build from Repository
git clone https://github.com/korya/http-assert.git
cd http-assert
go build -o http-assert .
Usage
Basic Syntax
http-assert [flags] <URL>
Request Options
--request | -X | HTTP method (default: GET) |
--data | -d | Request body data |
--header | -H | Set request headers (can be used multiple times) |
--max-time | -m | Request timeout in seconds (default: 20) |
--insecure | -k | Skip SSL certificate verification |
--maphost | | Map hostname:port to different destination |
Assertion Options
--assert-ok | Assert 2xx status code |
--assert-status | Assert specific status code |
--assert-header | Assert header matches regex pattern |
--assert-header-eq | Assert header equals exact value |
--assert-header-missing | Assert header is not present |
--assert-body | Assert body matches regex pattern |
--assert-body-eq | Assert body equals exact value |
--assert-body-empty | Assert body is empty |
--assert-redirect | Assert redirect location matches regex |
--assert-redirect-eq | Assert redirect location equals exact value |
Logging Options
--verbose | -v | Enable verbose logging |
--silent | -s | Only log errors |
--log-level | | Set log level (debug, info, warn, error) |
Examples
Basic Health Check
http-assert --assert-ok https://api.example.com/health
POST Request with JSON Body
http-assert -X POST \
-H "Content-Type: application/json" \
-d '{"username":"test","password":"secret"}' \
--assert-status 201 \
https://api.example.com/login
Multiple Assertions
http-assert \
--assert-ok \
--assert-header-eq "Content-Type: application/json" \
--assert-body "\"status\":\"success\"" \
https://api.example.com/status
http-assert \
--assert-header-eq "X-API-Version: v1" \
--assert-header-missing "X-Debug-Info" \
--assert-header "Cache-Control: max-age=\d+" \
https://api.example.com/data
SSL and Security Testing
http-assert --insecure --assert-ok https://self-signed.example.com
http-assert --max-time 5 --assert-ok https://slow-api.example.com
Host Mapping for Load Balancer Testing
http-assert \
--maphost "api.example.com:443=backend1.internal:8443" \
--assert-ok \
https://api.example.com/health
http-assert \
--maphost "*:80=192.168.1.10" \
--assert-status 200 \
http://loadbalancer.example.com
Redirect Testing
http-assert \
--assert-redirect-eq "https://new-domain.com/path" \
https://old-domain.com/path
http-assert \
--assert-redirect "https://.*\.example\.com/.*" \
https://redirect.example.com
http-assert \
--assert-redirect-eq "https://example.com/target" \
"https://example.com/redirect?url=https://example.com/target"
Body Content Validation
http-assert \
--assert-body-eq "OK" \
https://api.example.com/ping
http-assert \
--assert-body "\"users\":\s*\[\]" \
https://api.example.com/users
http-assert \
--assert-body-empty \
https://api.example.com/delete-resource
Environment Variables
You can also configure the tool using environment variables with the HTTP_ASSERT_
prefix:
export HTTP_ASSERT_VERBOSE=true
export HTTP_ASSERT_MAX_TIME=30
export HTTP_ASSERT_INSECURE=true
http-assert --assert-ok https://api.example.com
Exit Codes
0
: All assertions passed
93
: Failed to perform HTTP request or assertions failed
103
: Invalid command line arguments or other errors
Use Cases
CI/CD Pipeline Integration
#!/bin/bash
deploy-service.sh
sleep 10
http-assert \
--max-time 30 \
--assert-ok \
--assert-header-eq "X-Service-Version: $EXPECTED_VERSION" \
https://api.example.com/health
if [ $? -eq 0 ]; then
echo "Deployment validation passed"
else
echo "Deployment validation failed"
exit 1
fi
Monitoring Script
#!/bin/bash
ENDPOINTS=(
"https://api.example.com/health"
"https://db.example.com/ping"
"https://cache.example.com/status"
)
for endpoint in "${ENDPOINTS[@]}"; do
if http-assert --silent --assert-ok "$endpoint"; then
echo "✓ $endpoint"
else
echo "✗ $endpoint"
fi
done
Load Balancer Health Check
BACKENDS=("backend1.internal" "backend2.internal" "backend3.internal")
for backend in "${BACKENDS[@]}"; do
echo "Testing $backend..."
http-assert \
--maphost "api.example.com:443=$backend:8443" \
--assert-ok \
--assert-header "X-Backend-Server: $backend" \
https://api.example.com/health
done