MCPcopy Index your code
hub / github.com/VictoriaMetrics/VictoriaLogs / TestBasicAuthMetrics

Function TestBasicAuthMetrics

lib/httpserver/httpserver_test.go:42–84  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

40}
41
42func TestBasicAuthMetrics(t *testing.T) {
43 origUsername := *httpAuthUsername
44 origPasswd := httpAuthPassword.Get()
45 defer func() {
46 if err := httpAuthPassword.Set(origPasswd); err != nil {
47 t.Fatalf("unexpected error: %s", err)
48 }
49 *httpAuthUsername = origUsername
50 }()
51
52 f := func(user, pass string, expCode int) {
53 t.Helper()
54 req := httptest.NewRequest(http.MethodGet, "/metrics", nil)
55 req.SetBasicAuth(user, pass)
56
57 w := httptest.NewRecorder()
58 CheckBasicAuth(w, req)
59
60 res := w.Result()
61 _ = res.Body.Close()
62 if expCode != res.StatusCode {
63 t.Fatalf("wanted status code: %d, got: %d\n", res.StatusCode, expCode)
64 }
65 }
66
67 *httpAuthUsername = "test"
68 if err := httpAuthPassword.Set("pass"); err != nil {
69 t.Fatalf("unexpected error: %s", err)
70 }
71 f("test", "pass", 200)
72 f("test", "wrong", 401)
73 f("wrong", "pass", 401)
74 f("wrong", "wrong", 401)
75
76 *httpAuthUsername = ""
77 if err := httpAuthPassword.Set(""); err != nil {
78 t.Fatalf("unexpected error: %s", err)
79 }
80 f("test", "pass", 200)
81 f("test", "wrong", 200)
82 f("wrong", "pass", 200)
83 f("wrong", "wrong", 200)
84}
85
86func TestAuthKeyMetrics(t *testing.T) {
87 origUsername := *httpAuthUsername

Callers

nothing calls this directly

Calls 5

CheckBasicAuthFunction · 0.85
CloseMethod · 0.65
fFunction · 0.50
GetMethod · 0.45
SetMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…