MCPcopy
hub / github.com/git-lfs/git-lfs / TestStatsWithKey

Function TestStatsWithKey

lfshttp/stats_test.go:17–81  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

15)
16
17func TestStatsWithKey(t *testing.T) {
18 var called uint32
19 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
20 atomic.AddUint32(&called, 1)
21 t.Logf("srv req %s %s", r.Method, r.URL.Path)
22 assert.Equal(t, "POST", r.Method)
23
24 assert.Equal(t, "Basic ABC", r.Header.Get("Authorization"))
25 body := &verboseTest{}
26 err := json.NewDecoder(r.Body).Decode(body)
27 assert.Nil(t, err)
28 assert.Equal(t, "Verbose", body.Test)
29 w.Header().Set("Content-Type", "application/json")
30 w.Write([]byte(`{"Status":"Ok"}`))
31 }))
32 defer srv.Close()
33
34 out := &bytes.Buffer{}
35 c, _ := NewClient(nil)
36 c.ConcurrentTransfers = 5
37 c.LogHTTPStats(nopCloser(out))
38
39 req, err := http.NewRequest("POST", srv.URL, nil)
40 req = c.LogRequest(req, "stats-test")
41 req.Header.Set("Authorization", "Basic ABC")
42 req.Header.Set("Content-Type", "application/json")
43 require.Nil(t, err)
44 require.Nil(t, MarshalToRequest(req, verboseTest{"Verbose"}))
45
46 res, err := c.Do(req)
47 require.Nil(t, err)
48
49 io.Copy(io.Discard, res.Body)
50 res.Body.Close()
51 assert.Nil(t, c.Close())
52
53 assert.Equal(t, 200, res.StatusCode)
54 assert.EqualValues(t, 1, called)
55
56 stats := strings.TrimSpace(out.String())
57 t.Log(stats)
58 lines := strings.Split(stats, "\n")
59 require.Equal(t, 3, len(lines))
60 assert.True(t, strings.Contains(lines[0], "concurrent=5"))
61 expected := []string{
62 "key=stats-test",
63 "event=request",
64 "body=18",
65 "url=" + srv.URL,
66 }
67
68 for _, substr := range expected {
69 assert.True(t, strings.Contains(lines[1], substr), "missing: "+substr)
70 }
71
72 expected = []string{
73 "key=stats-test",
74 "event=response",

Callers

nothing calls this directly

Calls 15

LogHTTPStatsMethod · 0.95
LogRequestMethod · 0.95
DoMethod · 0.95
CloseMethod · 0.95
nopCloserFunction · 0.85
LogfMethod · 0.80
LogMethod · 0.80
NewClientFunction · 0.70
MarshalToRequestFunction · 0.70
GetMethod · 0.65
SetMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected