API Controller Testing Scripts
Quick PowerShell scripts to test read-only API endpoints for 200 response codes.
Scripts
1. Test-Controllers.ps1 - Comprehensive Testing
Full-featured script with detailed output and error tracking.
Usage:
# Basic usage
.\Test-Controllers.ps1
# Custom base URL
.\Test-Controllers.ps1 -BaseUrl "https://localhost:5000"
# With verbose output
.\Test-Controllers.ps1 -Verbose
# Custom timeout
.\Test-Controllers.ps1 -TimeoutSeconds 60
Features:
- Tests all read-only endpoints
- Tests specific ID endpoints
- Color-coded output
- Detailed error reporting
- Success rate calculation
- Exit codes for CI/CD
2. Quick-Test.ps1 - Fast Validation
Minimal script for quick validation of core endpoints.
Usage:
# Basic usage
.\Quick-Test.ps1
# Custom base URL
.\Quick-Test.ps1 -BaseUrl "https://localhost:5000"
Features:
- Tests core endpoints only
- Minimal output
- Fast execution
- Simple pass/fail results
3. Test-Config.json - Configuration
JSON configuration file for customizing test parameters.
Tested Endpoints
Original Endpoints (Phase 1)
/api/junction- Get All Junctions/api/pipe- Get All Pipes/api/pump- Get All Pumps/api/tank- Get All Tanks/api/valve- Get All Valves/api/reservoir- Get All Reservoirs
Core Entity Controllers (Phase 2)
/api/pattern- Get All Patterns/api/datacurve- Get All Data Curves/api/timeparameters- Get All Time Parameters/api/options- Get All Options/api/demand- Get All Demands/api/source- Get All Sources/api/reaction- Get All Reactions/api/tankmixing- Get All Tank Mixings
Advanced Entity Controllers (Phase 3)
/api/energy- Get All Energy/api/emitter- Get All Emitters/api/leakage- Get All Leakages/api/quality- Get All Quality/api/status- Get All Status/api/report- Get All Reports
Prerequisites
- PowerShell 5.1+ or PowerShell Core 6+
- API running on the specified base URL
- Network access to the API endpoints
Example Output
🚀 Quick API Test - https://localhost:7000
✅ /api/pattern
✅ /api/datacurve
✅ /api/junction
❌ /api/pipe - Error: The remote server returned an error: (404) Not Found.
✅ /api/pump
✅ /api/tank
✅ /api/valve
✅ /api/reservoir
📊 Results: 7/8 passed
Exit Codes
- 0 - All tests passed
- 1 - One or more tests failed
Troubleshooting
Common Issues
Connection Refused
- Ensure the API is running
- Check the base URL and port
- Verify firewall settings
404 Not Found
- Controller may not be implemented
- Check route configuration
- Verify controller exists
Timeout
- Increase timeout with
-TimeoutSeconds - Check network connectivity
- Verify API performance
- Increase timeout with
SSL/TLS Issues
- Use HTTP instead of HTTPS for local testing
- Check certificate configuration
- Use
-SkipCertificateCheckif needed
Customization
Edit Test-Config.json to:
- Add new endpoints
- Modify test IDs
- Change default settings
- Add new controller groups
Integration with CI/CD
# GitHub Actions example
- name: Test API Endpoints
run: |
powershell -File Test-Controllers.ps1 -BaseUrl "https://api.example.com"
continue-on-error: false
# Bash script example
#!/bin/bash
powershell -File Test-Controllers.ps1 -BaseUrl "https://api.example.com"
if [ $? -eq 0 ]; then
echo "All tests passed!"
else
echo "Some tests failed!"
exit 1
fi