69012bb1ae
New test files: - config/config_test.go (100% coverage) - constants/constants_test.go (100% coverage) - httputil/response_test.go (100% coverage) - validation/rules_test.go (91.9% coverage) - middleware/logger_test.go, security_test.go, security_logger_test.go - handlers/errors_test.go Updated documentation: - doc/27-GO-TESTING.md: Complete testing guide - doc/00-GO-DOCUMENTATION-INDEX.md: Added testing section - doc/01-ARCHITECTURE.md: Updated package structure - doc/DECISIONS.md: Added ADR-004 caching decision - PROJECT-MEMORY.md: Added Go testing section
183 lines
4.3 KiB
Go
183 lines
4.3 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
// TestHome tests the Home handler
|
|
func TestHome(t *testing.T) {
|
|
handler := newTestCVHandler(t, "localhost:8080", nil)
|
|
|
|
tests := []struct {
|
|
name string
|
|
lang string
|
|
expectStatus int
|
|
expectContains string
|
|
}{
|
|
{
|
|
name: "Default language (English)",
|
|
lang: "",
|
|
expectStatus: http.StatusOK,
|
|
expectContains: "Juan Andrés Moreno Rubio",
|
|
},
|
|
{
|
|
name: "English language",
|
|
lang: "en",
|
|
expectStatus: http.StatusOK,
|
|
expectContains: "Juan Andrés Moreno Rubio",
|
|
},
|
|
{
|
|
name: "Spanish language",
|
|
lang: "es",
|
|
expectStatus: http.StatusOK,
|
|
expectContains: "Juan Andrés Moreno Rubio",
|
|
},
|
|
{
|
|
name: "Invalid language",
|
|
lang: "fr",
|
|
expectStatus: http.StatusBadRequest,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// Create request
|
|
req := httptest.NewRequest(http.MethodGet, "/?lang="+tt.lang, nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
// Call handler
|
|
handler.Home(w, req)
|
|
|
|
// Check status code
|
|
if w.Code != tt.expectStatus {
|
|
t.Errorf("Expected status %d, got %d", tt.expectStatus, w.Code)
|
|
}
|
|
|
|
// Check response body contains expected content (if success)
|
|
if tt.expectStatus == http.StatusOK && tt.expectContains != "" {
|
|
body := w.Body.String()
|
|
if len(body) == 0 {
|
|
t.Error("Expected non-empty response body")
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestCVContent tests the CVContent handler
|
|
func TestCVContent(t *testing.T) {
|
|
handler := newTestCVHandler(t, "localhost:8080", nil)
|
|
|
|
tests := []struct {
|
|
name string
|
|
lang string
|
|
expectStatus int
|
|
}{
|
|
{
|
|
name: "Default language",
|
|
lang: "",
|
|
expectStatus: http.StatusOK,
|
|
},
|
|
{
|
|
name: "English language",
|
|
lang: "en",
|
|
expectStatus: http.StatusOK,
|
|
},
|
|
{
|
|
name: "Spanish language",
|
|
lang: "es",
|
|
expectStatus: http.StatusOK,
|
|
},
|
|
{
|
|
name: "Invalid language",
|
|
lang: "de",
|
|
expectStatus: http.StatusBadRequest,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// Create request
|
|
req := httptest.NewRequest(http.MethodGet, "/cv-content?lang="+tt.lang, nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
// Call handler
|
|
handler.CVContent(w, req)
|
|
|
|
// Check status code
|
|
if w.Code != tt.expectStatus {
|
|
t.Errorf("Expected status %d, got %d", tt.expectStatus, w.Code)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestDefaultCVShortcut tests the DefaultCVShortcut handler
|
|
// NOTE: This test requires a running server for PDF generation via chromedp
|
|
func TestDefaultCVShortcut(t *testing.T) {
|
|
// Skip if running in CI or if server is not available
|
|
// PDF generation requires a running HTTP server that chromedp can connect to
|
|
if testing.Short() {
|
|
t.Skip("Skipping PDF generation test - requires running server")
|
|
}
|
|
|
|
// Check if server is actually running on port 1999
|
|
resp, err := http.Get("http://localhost:1999/health")
|
|
if err != nil || resp.StatusCode != http.StatusOK {
|
|
t.Skip("Skipping PDF generation test - server not running on localhost:1999")
|
|
}
|
|
resp.Body.Close()
|
|
|
|
handler := newTestCVHandler(t, "localhost:1999", nil)
|
|
|
|
tests := []struct {
|
|
name string
|
|
path string
|
|
expectStatus int
|
|
}{
|
|
{
|
|
name: "Valid shortcut URL (current year EN)",
|
|
path: "/cv-jamr-2025-en.pdf",
|
|
expectStatus: http.StatusOK,
|
|
},
|
|
{
|
|
name: "Valid shortcut URL (current year ES)",
|
|
path: "/cv-jamr-2025-es.pdf",
|
|
expectStatus: http.StatusOK,
|
|
},
|
|
{
|
|
name: "Invalid year",
|
|
path: "/cv-jamr-2020-en.pdf",
|
|
expectStatus: http.StatusNotFound,
|
|
},
|
|
{
|
|
name: "Invalid language",
|
|
path: "/cv-jamr-2025-fr.pdf",
|
|
expectStatus: http.StatusNotFound,
|
|
},
|
|
{
|
|
name: "Invalid format",
|
|
path: "/cv-wrong-format.pdf",
|
|
expectStatus: http.StatusNotFound,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// Create request
|
|
req := httptest.NewRequest(http.MethodGet, tt.path, nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
// Call handler
|
|
handler.DefaultCVShortcut(w, req)
|
|
|
|
// Check status code
|
|
if w.Code != tt.expectStatus {
|
|
t.Errorf("Expected status %d, got %d", tt.expectStatus, w.Code)
|
|
}
|
|
})
|
|
}
|
|
}
|