MCPcopy
hub / github.com/prometheus/client_golang / TestWriteResponse

Function TestWriteResponse

exp/api/remote/remote_headers_test.go:25–100  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

23)
24
25func TestWriteResponse(t *testing.T) {
26 t.Run("new response has empty headers", func(t *testing.T) {
27 resp := NewWriteResponse()
28 if len(resp.ExtraHeaders()) != 0 {
29 t.Errorf("expected empty headers, got %v", resp.ExtraHeaders())
30 }
31 })
32
33 t.Run("setters and getters", func(t *testing.T) {
34 resp := NewWriteResponse()
35
36 resp.SetStatusCode(http.StatusOK)
37 if got := resp.StatusCode(); got != http.StatusOK {
38 t.Errorf("expected status code %d, got %d", http.StatusOK, got)
39 }
40
41 stats := WriteResponseStats{
42 Samples: 10,
43 Histograms: 5,
44 Exemplars: 2,
45 confirmed: true,
46 }
47 resp.Add(stats)
48 if diff := cmp.Diff(stats, resp.Stats(), cmpopts.IgnoreUnexported(WriteResponseStats{})); diff != "" {
49 t.Errorf("stats mismatch (-want +got):\n%s", diff)
50 }
51
52 toAdd := WriteResponseStats{
53 Samples: 10,
54 Histograms: 5,
55 Exemplars: 2,
56 confirmed: true,
57 }
58 resp.Add(toAdd)
59 if diff := cmp.Diff(WriteResponseStats{
60 Samples: 20,
61 Histograms: 10,
62 Exemplars: 4,
63 confirmed: true,
64 }, resp.Stats(), cmpopts.IgnoreUnexported(WriteResponseStats{})); diff != "" {
65 t.Errorf("stats mismatch (-want +got):\n%s", diff)
66 }
67
68 resp.SetExtraHeader("Test-Header", "test-value")
69 if got := resp.ExtraHeaders().Get("Test-Header"); got != "test-value" {
70 t.Errorf("expected header value %q, got %q", "test-value", got)
71 }
72 })
73
74 t.Run("set headers on response writer", func(t *testing.T) {
75 resp := NewWriteResponse()
76 resp.Add(WriteResponseStats{
77 Samples: 10,
78 Histograms: 5,
79 Exemplars: 2,
80 confirmed: true,
81 })
82 resp.SetExtraHeader("Custom-Header", "custom-value")

Callers

nothing calls this directly

Calls 10

ExtraHeadersMethod · 0.95
SetStatusCodeMethod · 0.95
StatusCodeMethod · 0.95
StatsMethod · 0.95
SetExtraHeaderMethod · 0.95
SetHeadersMethod · 0.95
NewWriteResponseFunction · 0.85
RunMethod · 0.80
AddMethod · 0.65
HeaderMethod · 0.45

Tested by

no test coverage detected