(t *testing.T)
| 40 | } |
| 41 | |
| 42 | func 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 | |
| 86 | func TestAuthKeyMetrics(t *testing.T) { |
| 87 | origUsername := *httpAuthUsername |
nothing calls this directly
no test coverage detected
searching dependent graphs…