All checks were successful
Check / check (pull_request) Successful in 3m10s
Replace every httptest.NewRequest call with httptest.NewRequestWithContext using the test's t.Context(). Thread t *testing.T through the createSetupFormRequest and createLoginFormRequest helpers so they can supply a context. make lint under golangci-lint 2.12.2 drops from 94 findings to 47 (remaining: 23 gosec, 24 goconst, tracked in #176/#177/#178). make test and make fmt-check pass unchanged. Closes #175
74 lines
2.2 KiB
Go
74 lines
2.2 KiB
Go
package handlers_test
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// TestRenderTemplateBuffersOutput verifies that successful template rendering
|
|
// produces a complete HTML response (not partial/corrupt).
|
|
func TestRenderTemplateBuffersOutput(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
testCtx := setupTestHandlers(t)
|
|
|
|
// The setup page is simple and has no DB dependencies
|
|
request := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/setup", nil)
|
|
recorder := httptest.NewRecorder()
|
|
|
|
handler := testCtx.handlers.HandleSetupGET()
|
|
handler.ServeHTTP(recorder, request)
|
|
|
|
assert.Equal(t, http.StatusOK, recorder.Code)
|
|
|
|
body := recorder.Body.String()
|
|
// A properly buffered response should contain the closing </html> tag,
|
|
// proving the full template was rendered before being sent.
|
|
assert.Contains(t, body, "</html>")
|
|
// Should NOT contain the error text that would be appended on failure
|
|
assert.NotContains(t, body, "Internal Server Error")
|
|
}
|
|
|
|
// TestDashboardRenderTemplateBuffersOutput verifies the dashboard handler
|
|
// also uses buffered template rendering.
|
|
func TestDashboardRenderTemplateBuffersOutput(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
testCtx := setupTestHandlers(t)
|
|
|
|
request := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/", nil)
|
|
recorder := httptest.NewRecorder()
|
|
|
|
handler := testCtx.handlers.HandleDashboard()
|
|
handler.ServeHTTP(recorder, request)
|
|
|
|
assert.Equal(t, http.StatusOK, recorder.Code)
|
|
|
|
body := recorder.Body.String()
|
|
assert.Contains(t, body, "</html>")
|
|
assert.NotContains(t, body, "Internal Server Error")
|
|
}
|
|
|
|
// TestLoginRenderTemplateBuffersOutput verifies the login handler
|
|
// uses buffered template rendering.
|
|
func TestLoginRenderTemplateBuffersOutput(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
testCtx := setupTestHandlers(t)
|
|
|
|
request := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/login", nil)
|
|
recorder := httptest.NewRecorder()
|
|
|
|
handler := testCtx.handlers.HandleLoginGET()
|
|
handler.ServeHTTP(recorder, request)
|
|
|
|
assert.Equal(t, http.StatusOK, recorder.Code)
|
|
|
|
body := recorder.Body.String()
|
|
assert.Contains(t, body, "</html>")
|
|
assert.NotContains(t, body, "Internal Server Error")
|
|
}
|