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

Function TestNewAdminHandlerRouterRegistration

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

Source from the content-addressed store, hash-verified

435}
436
437func TestNewAdminHandlerRouterRegistration(t *testing.T) {
438 originalModules := make(map[string]ModuleInfo)
439 maps.Copy(originalModules, modules)
440 defer func() {
441 modules = originalModules
442 }()
443
444 mockRoute := AdminRoute{
445 Pattern: "/mock",
446 Handler: AdminHandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
447 w.WriteHeader(http.StatusOK)
448 return nil
449 }),
450 }
451
452 mock := &mockModule{
453 mockRouter: mockRouter{
454 routes: []AdminRoute{mockRoute},
455 },
456 }
457 RegisterModule(mock)
458
459 addr, err := ParseNetworkAddress("localhost:2019")
460 if err != nil {
461 t.Fatalf("Failed to parse address: %v", err)
462 }
463
464 admin := &AdminConfig{
465 EnforceOrigin: false,
466 }
467 handler, err := admin.newAdminHandler(addr, false, Context{})
468 if err != nil {
469 t.Fatalf("Failed to create admin handler: %v", err)
470 }
471
472 req := httptest.NewRequest("GET", "/mock", nil)
473 req.Host = "localhost:2019"
474 rr := httptest.NewRecorder()
475
476 handler.ServeHTTP(rr, req)
477
478 if rr.Code != http.StatusOK {
479 t.Errorf("Expected status code %d but got %d", http.StatusOK, rr.Code)
480 t.Logf("Response body: %s", rr.Body.String())
481 }
482}
483
484type mockProvisionableRouter struct {
485 mockRouter

Callers

nothing calls this directly

Calls 7

newAdminHandlerMethod · 0.95
AdminHandlerFuncFuncType · 0.85
RegisterModuleFunction · 0.85
ParseNetworkAddressFunction · 0.85
ServeHTTPMethod · 0.65
WriteHeaderMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected