This document describes the comprehensive test suite for the htaccess-monitor Go application.
main_test.go)Unit tests cover individual functions and components:
TestGetStatusIcon - Tests status code to icon mapping (200, 302, 404, etc.)TestMax - Tests the max utility functionTestStripAnsiCodes - Tests ANSI escape code removalTestVisualWidth - Tests visual width calculation for strings with emojisTestPadToWidth - Tests string padding functionalityTestApplyFilters - Tests result filtering (hide passes/fails)TestParseLinkTestFile - Tests CSV file parsingTestParseLinkTestFileInvalid - Tests error handling for invalid CSVTestParseLinkTestFileNotFound - Tests missing file handlingTestTestURL - Tests HTTP request functionality with mock serversTestTestURLWithUserAgent - Tests user agent header handlingTestTestSpecialURL - Tests special URL handling (wp-admin, robots.txt, sitemap)TestRunLinkTests - Tests the complete link testing workflowTestCountryStructure - Tests Country structTestHTTPResult - Tests HTTPResult structTestLinkTestResult - Tests LinkTestResult structintegration_test.go)Integration tests verify complete workflows:
TestIntegrationFullWorkflow - Tests end-to-end workflow with CSV parsing and HTTP testingTestIntegrationFilterWorkflow - Tests filtering functionality across multiple resultsTestIntegrationHTTPTimeout - Tests timeout handling for slow serversTestIntegrationSpecialURLs - Tests special URL handling (wp-admin, robots.txt, sitemaps)TestIntegrationRedirectChain - Tests redirect handlingTestIntegrationUserAgentHandling - Tests GoogleBot vs regular user handlingTestIntegrationCSVEdgeCases - Tests CSV parsing edge cases (spaces, quotes, missing columns)TestIntegrationCountryCodeNormalization - Tests country code uppercase conversionPerformance benchmarks for critical functions:
BenchmarkGetStatusIcon - Status icon retrieval performanceBenchmarkStripAnsiCodes - ANSI code stripping performanceBenchmarkApplyFilters - Filter application performance# Run unit tests
make go-test
# Run unit tests with coverage report
make go-test-coverage
# Run integration tests
make go-test-integration
# Run benchmarks
make go-test-bench
# Run all tests
make go-test-all
# Run linter
make go-lint
# Run unit tests
cd apps/htaccess-monitor
go test -v
# Run with coverage
go test -v -cover
# Generate coverage report
go test -coverprofile=coverage.out
go tool cover -html=coverage.out -o coverage.html
# Run integration tests
go test -v -tags=integration
# Run benchmarks
go test -bench=. -benchmem
# Run specific test
go test -v -run TestGetStatusIcon
# Run with race detector
go test -race
Current coverage: 19.5% of statements
Coverage focuses on:
main_test.go - Unit tests (16 test functions)integration_test.go - Integration tests (8 test functions)coverage.out - Coverage data (generated)coverage.html - HTML coverage report (generated)func TestYourFunction(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"test case 1", "input1", "expected1"},
{"test case 2", "input2", "expected2"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := yourFunction(tt.input)
if result != tt.expected {
t.Errorf("yourFunction(%s) = %s, want %s",
tt.input, result, tt.expected)
}
})
}
}
// +build integration
func TestIntegrationYourFeature(t *testing.T) {
// Setup
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}))
defer server.Close()
// Test
result := yourFunction(server.URL)
// Assert
if result != expected {
t.Errorf("Expected %v, got %v", expected, result)
}
}
func BenchmarkYourFunction(b *testing.B) {
input := "test input"
b.ResetTimer()
for i := 0; i < b.N; i++ {
yourFunction(input)
}
}
Tests use httptest.NewServer for HTTP testing:
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Verify request headers
if r.Header.Get("X-Test-Country") != "US" {
t.Error("Country header not set")
}
// Send response
w.Header().Set("Location", "http://example.com/redirect")
w.WriteHeader(302)
}))
defer server.Close()
// Use server.URL in tests
result := testURL(server.URL, "US", "")
Tests are designed to run in CI/CD pipelines:
# Example GitHub Actions workflow
- name: Run tests
run: |
cd apps/htaccess-monitor
go test -v -race -coverprofile=coverage.out
go test -v -tags=integration
t.Run() for organized test outputhttptest for HTTP testingt.TempDir() for file operationsdefer for resource cleanup-race flag# Clean and rebuild
make clean
make go-deps
make go-test
# Ensure coverage.out exists
cd apps/htaccess-monitor
go test -coverprofile=coverage.out
go tool cover -html=coverage.out -o coverage.html
Integration tests include HTTP timeout tests that take 10+ seconds. This is expected behavior.
# Run with race detector
go test -race
Current benchmark results (AMD Ryzen 9 7950X):
BenchmarkGetStatusIcon-32 1000000000 0.19 ns/op 0 B/op 0 allocs/op
BenchmarkStripAnsiCodes-32 809494 1264 ns/op 1723 B/op 24 allocs/op
BenchmarkApplyFilters-32 418665 3073 ns/op 15712 B/op 7 allocs/op
When adding new features:
make go-lint