MCPcopy
hub / github.com/caddyserver/caddy / TestAdminHandlerErrorHandling

Function TestAdminHandlerErrorHandling

admin_test.go:228–256  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

226}
227
228func TestAdminHandlerErrorHandling(t *testing.T) {
229 initAdminMetrics()
230
231 handler := adminHandler{
232 mux: http.NewServeMux(),
233 }
234
235 handler.mux.Handle("/error", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
236 err := fmt.Errorf("test error")
237 handler.handleError(w, r, err)
238 }))
239
240 req := httptest.NewRequest(http.MethodGet, "/error", nil)
241 rr := httptest.NewRecorder()
242
243 handler.ServeHTTP(rr, req)
244
245 if rr.Code == http.StatusOK {
246 t.Error("expected error response, got success")
247 }
248
249 var apiErr APIError
250 if err := json.NewDecoder(rr.Body).Decode(&apiErr); err != nil {
251 t.Fatalf("decoding response: %v", err)
252 }
253 if apiErr.Message != "test error" {
254 t.Errorf("expected error message 'test error', got '%s'", apiErr.Message)
255 }
256}
257
258func TestAdminHandlerServeHTTPRedactsSensitiveHeadersInLogs(t *testing.T) {
259 core, logs := observer.New(zap.InfoLevel)

Callers

nothing calls this directly

Calls 5

handleErrorMethod · 0.95
ServeHTTPMethod · 0.95
initAdminMetricsFunction · 0.85
HandleMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected