(t *testing.T)
| 11 | ) |
| 12 | |
| 13 | func TestGetQuotedRemoteAddr(t *testing.T) { |
| 14 | f := func(remoteAddr, xForwardedFor, expectedAddr string) { |
| 15 | t.Helper() |
| 16 | |
| 17 | req := &http.Request{ |
| 18 | RemoteAddr: remoteAddr, |
| 19 | } |
| 20 | if xForwardedFor != "" { |
| 21 | req.Header = map[string][]string{ |
| 22 | "X-Forwarded-For": {xForwardedFor}, |
| 23 | } |
| 24 | } |
| 25 | addr := GetQuotedRemoteAddr(req) |
| 26 | if addr != expectedAddr { |
| 27 | t.Fatalf("unexpected remote addr;\ngot\n%s\nwant\n%s", addr, expectedAddr) |
| 28 | } |
| 29 | |
| 30 | // Verify that the addr can be unmarshaled as JSON string |
| 31 | var s string |
| 32 | if err := json.Unmarshal([]byte(addr), &s); err != nil { |
| 33 | t.Fatalf("cannot unmarshal addr: %s", err) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | f("1.2.3.4", "", `"1.2.3.4"`) |
| 38 | f("1.2.3.4", "foo.bar", `"1.2.3.4, X-Forwarded-For: foo.bar"`) |
| 39 | f("1.2\n\"3.4", "foo\nb\"ar", `"1.2\n\"3.4, X-Forwarded-For: foo\nb\"ar"`) |
| 40 | } |
| 41 | |
| 42 | func TestBasicAuthMetrics(t *testing.T) { |
| 43 | origUsername := *httpAuthUsername |
nothing calls this directly
no test coverage detected
searching dependent graphs…