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

Function TestMetricsHTTPSCatchAll

modules/caddyhttp/metrics_test.go:434–481  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

432}
433
434func TestMetricsHTTPSCatchAll(t *testing.T) {
435 ctx, _ := caddy.NewContext(caddy.Context{Context: context.Background()})
436
437 // Test that HTTPS requests allow catch-all even when AllowCatchAllHosts is false
438 metrics := &Metrics{
439 PerHost: true,
440 ObserveCatchallHosts: false,
441 hasHTTPSServer: true, // Simulate having HTTPS servers
442 init: sync.Once{},
443 httpMetrics: &httpMetrics{},
444 allowedHosts: make(map[string]struct{}), // Empty - no explicitly allowed hosts
445 }
446
447 h := HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
448 w.Write([]byte("hello"))
449 return nil
450 })
451
452 ih := newMetricsInstrumentedRoute(ctx, "test", h, metrics)
453
454 // Test HTTPS request (should be allowed even though not in allowedHosts)
455 r1 := httptest.NewRequest("GET", "https://unknown.com/", nil)
456 r1.Host = "unknown.com"
457 r1.TLS = &tls.ConnectionState{} // Mark as TLS/HTTPS
458 w1 := httptest.NewRecorder()
459 ih.ServeHTTP(w1, r1)
460
461 // Test HTTP request (should be mapped to "_other")
462 r2 := httptest.NewRequest("GET", "http://unknown.com/", nil)
463 r2.Host = "unknown.com"
464 // No TLS field = HTTP request
465 w2 := httptest.NewRecorder()
466 ih.ServeHTTP(w2, r2)
467
468 // Check that HTTPS request gets real host, HTTP gets "_other"
469 expected := `
470 # HELP caddy_http_requests_total Counter of HTTP(S) requests made.
471 # TYPE caddy_http_requests_total counter
472 caddy_http_requests_total{handler="test",host="_other",server="UNKNOWN"} 1
473 caddy_http_requests_total{handler="test",host="unknown.com",server="UNKNOWN"} 1
474 `
475
476 if err := testutil.GatherAndCompare(ctx.GetMetricsRegistry(), strings.NewReader(expected),
477 "caddy_http_requests_total",
478 ); err != nil {
479 t.Errorf("HTTPS catch-all test failed: %s", err)
480 }
481}
482
483func TestMetricsInstrumentedRoute(t *testing.T) {
484 ctx, _ := caddy.NewContext(caddy.Context{Context: context.Background()})

Callers

nothing calls this directly

Calls 5

GetMetricsRegistryMethod · 0.95
HandlerFuncFuncType · 0.85
ServeHTTPMethod · 0.65
WriteMethod · 0.45

Tested by

no test coverage detected