tenseleyflow/shithub / 5a50008

Browse files

metrics(test): pin promhttp gzip-disabled in addition to middleware bypass

Authored by espadonne
SHA
5a50008c04c1b84c634dd430e1be6334f3620f24
Parents
56e254e
Tree
6421162

1 changed file

StatusFile+-
M internal/web/handlers/metrics_compress_test.go 38 6
internal/web/handlers/metrics_compress_test.gomodified
@@ -11,15 +11,27 @@ import (
1111
 	"testing"
1212
 
1313
 	"github.com/go-chi/chi/v5"
14
+
15
+	"github.com/tenseleyFlow/shithub/internal/infra/metrics"
1416
 )
1517
 
1618
 // /metrics MUST be served uncompressed even when the scraper advertises
1719
 // gzip support. Alloy 1.16 (and several other prom-compatible scrapers)
1820
 // mis-handle Content-Encoding: gzip and parse the raw 0x1f magic byte
1921
 // as text, failing the scrape silently with up=0.
20
-func TestMetricsServedUncompressedWithGzipAccept(t *testing.T) {
22
+//
23
+// Two layers can produce gzip on this route:
24
+//  1. The chi Compress middleware in the public route group.
25
+//  2. promhttp's own DisableCompression knob (default false).
26
+//
27
+// Each test below pins one layer so a regression in either fires loud.
28
+
29
+// Layer 1: the route is mounted outside the Compress middleware group.
30
+func TestMetricsRouteBypassesCompressMiddleware(t *testing.T) {
2131
 	t.Parallel()
2232
 
33
+	// Stub handler — the test is about the middleware path, not the
34
+	// real /metrics body shape.
2335
 	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2436
 		w.Header().Set("Content-Type", "text/plain; version=0.0.4; charset=utf-8")
2537
 		_, _ = io.WriteString(w, "# HELP test_metric A test metric\n# TYPE test_metric counter\ntest_metric 1\n")
@@ -47,11 +59,31 @@ func TestMetricsServedUncompressedWithGzipAccept(t *testing.T) {
4759
 	if enc := rec.Header().Get("Content-Encoding"); enc != "" {
4860
 		t.Errorf("Content-Encoding = %q, want empty (Prometheus scrapers expect plain text)", enc)
4961
 	}
50
-	body := rec.Body.String()
51
-	if !strings.Contains(body, "test_metric 1") {
52
-		t.Errorf("body missing metric text; got %q", body)
62
+	if strings.HasPrefix(rec.Body.String(), "\x1f\x8b") {
63
+		t.Error("body starts with gzip magic bytes — middleware compressed /metrics")
64
+	}
65
+}
66
+
67
+// Layer 2: the real metrics.Handler() must have promhttp's compression
68
+// disabled. Without DisableCompression: true on HandlerOpts, promhttp
69
+// gzips when the client advertises Accept-Encoding: gzip — entirely
70
+// independent of our middleware. The post-hardening audit caught this:
71
+// the layer-1 test passed but the live droplet still emitted gzip.
72
+func TestMetricsHandlerPromhttpCompressionDisabled(t *testing.T) {
73
+	t.Parallel()
74
+
75
+	rec := httptest.NewRecorder()
76
+	req := httptest.NewRequest(http.MethodGet, "/metrics", nil)
77
+	req.Header.Set("Accept-Encoding", "gzip")
78
+	metrics.Handler("", "").ServeHTTP(rec, req)
79
+
80
+	if got := rec.Code; got != http.StatusOK {
81
+		t.Fatalf("status = %d, want 200", got)
82
+	}
83
+	if enc := rec.Header().Get("Content-Encoding"); enc != "" {
84
+		t.Errorf("Content-Encoding = %q, want empty (DisableCompression must be set on promhttp.HandlerOpts)", enc)
5385
 	}
54
-	if strings.HasPrefix(body, "\x1f\x8b") {
55
-		t.Errorf("body starts with gzip magic bytes — middleware compressed /metrics")
86
+	if strings.HasPrefix(rec.Body.String(), "\x1f\x8b") {
87
+		t.Error("body starts with gzip magic bytes — promhttp compressed despite DisableCompression intent")
5688
 	}
5789
 }