All checks were successful
continuous-integration/drone/push Build is passing
- Complete unit, integration, and E2E test coverage (189 test cases) - Enhanced CI/CD pipeline with race detection and quality checks - Comprehensive godoc documentation for all packages - Updated README with API docs, examples, and deployment guides
64 lines
2.1 KiB
Go
64 lines
2.1 KiB
Go
// Package testdata provides utilities and test data for testing the GeoIP API service.
|
|
//
|
|
// This package contains helper functions for creating test databases, managing test files,
|
|
// and providing sample IP addresses with expected results for comprehensive testing.
|
|
package testdata
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
// TestDBPath returns the relative path to the testdata directory
|
|
// where test database files are stored.
|
|
func TestDBPath() string {
|
|
return filepath.Join("testdata")
|
|
}
|
|
|
|
// CreateTempTestDB creates a temporary test database file with minimal MMDB format headers.
|
|
// It returns the path to the temporary directory containing the test database.
|
|
// The created file contains basic MaxMind DB format magic bytes for testing file operations.
|
|
func CreateTempTestDB(t *testing.T, filename string) string {
|
|
t.Helper()
|
|
|
|
tempDir := t.TempDir()
|
|
dbPath := filepath.Join(tempDir, filename)
|
|
|
|
// Create a minimal valid MMDB file header (this is just for testing file operations)
|
|
// In real tests, we'd use actual MaxMind test database files
|
|
content := []byte{
|
|
0xab, 0xcd, 0xef, // Magic bytes for MaxMind DB format
|
|
0x01, 0x00, 0x00, 0x00, // Version
|
|
}
|
|
|
|
err := os.WriteFile(dbPath, content, 0644)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create test database: %v", err)
|
|
}
|
|
|
|
return tempDir
|
|
}
|
|
|
|
// TestIPs maps test IP addresses to their expected ISO country codes.
|
|
// This includes both IPv4 and IPv6 addresses, as well as special cases
|
|
// like localhost and private IPs that should be handled gracefully.
|
|
var TestIPs = map[string]string{
|
|
"8.8.8.8": "US", // Google DNS
|
|
"1.1.1.1": "US", // Cloudflare DNS
|
|
"199.43.0.43": "US", // Test IP used in health check
|
|
"127.0.0.1": "", // Localhost - should handle gracefully
|
|
"192.168.1.1": "", // Private IP - should handle gracefully
|
|
"2001:4860:4860::8888": "US", // Google IPv6 DNS
|
|
}
|
|
|
|
// InvalidIPs contains a list of invalid IP address strings for testing error handling.
|
|
// These include empty strings, malformed addresses, and non-IP text that should
|
|
// cause parsing errors in the GeoIP lookup functions.
|
|
var InvalidIPs = []string{
|
|
"",
|
|
"invalid",
|
|
"256.256.256.256",
|
|
"1.2.3",
|
|
"not.an.ip",
|
|
} |