This document explains the API coverage testing system that ensures the Mailpit Go client library implements all available Mailpit API endpoints.
The API coverage test (e2e_api_coverage_test.go) automatically:
- Fetches the latest Mailpit OpenAPI specification from the official repository
- Extracts all API routes and their details
- Maps routes to implemented client methods
- Reports coverage statistics and missing implementations
- Fails if required routes are missing or coverage falls below 95%
e2e_api_coverage_test.go- Main API coverage testscripts/api-coverage.sh- Maintenance utilities script
# Run both online and offline coverage tests
make test-api-coverage
# Or run directly with Go
go test -v -run TestAPIRouteCoverage -timeout=3m# When you don't need to fetch latest spec
go test -v -run TestAPIRouteCoverageOffline -timeout=30s# Full test with utilities
./scripts/api-coverage.sh test
# Offline test only
./scripts/api-coverage.sh test-offline
# Check dependencies
./scripts/api-coverage.sh check-depsThe test fetches the OpenAPI specification from:
- Primary:
https://raw.githubusercontent.com/axllent/mailpit/develop/server/ui/api/v1/swagger.json - Fallback:
https://raw.githubusercontent.com/axllent/mailpit/master/server/ui/api/v1/swagger.json
Routes are mapped to client methods using a predefined mapping table in findMatchingMethod():
routeMethodMap := map[string]string{
"GET:/api/v1/messages": "ListMessages",
"POST:/api/v1/send": "SendMessage",
// ... more mappings
}- Required Routes: Core API functionality that must be implemented
- Optional Routes: Advanced features that may not be available in all setups
- Coverage Threshold: 95% minimum coverage required
The test also identifies potentially incorrect mappings and suggests improvements.
- New Routes Added: The test will detect and fail, showing missing routes
- Routes Modified: Update the mapping table if parameter names change
- Routes Removed: Remove from mapping table and optional routes list
- Identify the Route: Check test output for missing routes
- Implement Client Method: Add the method to the
Clientinterface and implementation - Update Mapping: Add to
routeMethodMapinfindMatchingMethod() - Test: Run coverage test to verify
Example:
// In findMatchingMethod(), add:
"POST:/api/v1/new-endpoint": "NewEndpointMethod",
// In client.go interface, add:
NewEndpointMethod(ctx context.Context, param string) (*Response, error)Some routes may not be available in all Mailpit configurations. Mark them as optional:
// In checkRequiredRoutes(), add to optionalRoutes:
"GET:/api/v1/optional-feature": true,If the Mailpit repository changes the spec location:
./scripts/api-coverage.sh update-spec-url "https://new-url/swagger.json"✅ API Route Coverage Test PASSED!
Coverage: 100.00% (23/23 routes implemented)
❌ MISSING ROUTES:
GET /api/v1/new-endpoint
Summary: New endpoint description
Operation ID: NewEndpointParams
Notes: No matching client method found
⚠️ MAPPING QUALITY WARNINGS:
- PUT /api/v1/tags/{Tag} is mapped to DeleteTag() but should probably be RenameTag()
Consider implementing proper methods for these routes.
- List, get, delete messages
- Message headers, source, parts, attachments
- Message validation (HTML, links, spam)
- Search messages with queries
- Delete search results
- Send messages via SMTP
- Get, set, delete tags
- Tag message associations
- Server info, health checks
- Web UI configuration
- Chaos engineering configuration (optional)
- Run API coverage tests in CI/CD pipeline
- Test against multiple Mailpit versions
- Monitor for API changes in Mailpit releases
- Required Routes: Implement immediately when missing
- Optional Routes: Implement based on user needs
- Quality Issues: Address mapping problems for better accuracy
- Update route mappings when implementing new methods
- Document any special handling or limitations
- Keep API coverage documentation current
- Handle optional endpoints gracefully (404 errors expected)
- Provide clear error messages for missing implementations
- Test error scenarios as well as success cases
Add to your CI pipeline:
# GitHub Actions example
- name: Test API Coverage
run: |
go test -v -run TestAPIRouteCoverage -timeout=3m
if [ $? -ne 0 ]; then
echo "API coverage test failed. Check for missing route implementations."
exit 1
fi- Network Failures: Test falls back to offline mode automatically
- Spec Format Changes: Update parsing logic in
extractAPIRoutes() - Parameter Name Variations: Add alternative mappings in
normalizePathParameters() - New Endpoint Types: Extend route categorization logic
For detailed debugging, modify the test to log more information:
// Add more verbose logging
t.Logf("Route: %+v", route)
t.Logf("Mapping attempt: %s", routeKey)Download and examine the spec manually:
curl -s https://raw.githubusercontent.com/axllent/mailpit/develop/server/ui/api/v1/swagger.json | jq '.'Planned improvements:
- Automatic Stub Generation: Generate method stubs for missing routes
- Version Compatibility: Test against multiple Mailpit API versions
- Performance Metrics: Track API response times and performance
- Schema Validation: Validate request/response schemas match spec
- Integration Tests: Combine with E2E tests for full validation
When contributing new API methods:
- Check the coverage test first to understand missing routes
- Implement the client method following existing patterns
- Update the route mapping table
- Run the coverage test to verify
- Update documentation if needed
This ensures the library maintains comprehensive API coverage and stays current with Mailpit development.