MCPcopy Create free account
hub / github.com/felixge/httpsnoop / TestCaptureMetrics

Function TestCaptureMetrics

capture_metrics_test.go:14–126  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

12)
13
14func TestCaptureMetrics(t *testing.T) {
15 // Some of the edge cases tested below cause the net/http pkg to log some
16 // messages that add a lot of noise to the `go test -v` output, so we discard
17 // the log here.
18 log.SetOutput(io.Discard)
19 defer log.SetOutput(os.Stderr)
20
21 tests := []struct {
22 Name string
23 Handler http.Handler
24 WantDuration time.Duration
25 WantWritten int64
26 WantCode int
27 WantErr string
28 }{
29 {
30 Name: "simple",
31 Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}),
32 WantCode: http.StatusOK,
33 },
34 {
35 Name: "headers and body",
36 Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
37 w.WriteHeader(http.StatusBadRequest)
38 w.WriteHeader(http.StatusNotFound)
39 w.Write([]byte("foo"))
40 w.Write([]byte("bar"))
41 time.Sleep(25 * time.Millisecond)
42 }),
43 WantCode: http.StatusBadRequest,
44 WantWritten: 6,
45 WantDuration: 25 * time.Millisecond,
46 },
47 {
48 Name: "header after body",
49 Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
50 w.Write([]byte("foo"))
51 w.WriteHeader(http.StatusNotFound)
52 }),
53 WantCode: http.StatusOK,
54 },
55 {
56 Name: "reader",
57 Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
58 rrf := w.(io.ReaderFrom)
59 rrf.ReadFrom(strings.NewReader("reader from is ok"))
60 }),
61 WantWritten: 17,
62 WantCode: http.StatusOK,
63 },
64 {
65 Name: "string writer",
66 Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
67 _, _ = io.WriteString(w, "write string")
68 }),
69 WantWritten: int64(len("write string")),
70 WantCode: http.StatusOK,
71 },

Callers

nothing calls this directly

Calls 6

CaptureMetricsMethod · 0.95
errContainsFunction · 0.85
WriteHeaderMethod · 0.45
WriteMethod · 0.45
ReadFromMethod · 0.45
WriteStringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…