MCPcopy
hub / github.com/go-chi/chi / TestXForwardForIP

Function TestXForwardForIP

middleware/realip_test.go:35–66  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

33}
34
35func TestXForwardForIP(t *testing.T) {
36 xForwardedForIPs := []string{
37 "100.100.100.100",
38 "100.100.100.100, 200.200.200.200",
39 "100.100.100.100,200.200.200.200",
40 }
41
42 r := chi.NewRouter()
43 r.Use(RealIP)
44
45 for _, v := range xForwardedForIPs {
46 req, _ := http.NewRequest("GET", "/", nil)
47 req.Header.Add("X-Forwarded-For", v)
48
49 w := httptest.NewRecorder()
50
51 realIP := ""
52 r.Get("/", func(w http.ResponseWriter, r *http.Request) {
53 realIP = r.RemoteAddr
54 w.Write([]byte("Hello World"))
55 })
56 r.ServeHTTP(w, req)
57
58 if w.Code != 200 {
59 t.Fatal("Response Code should be 200")
60 }
61
62 if realIP != "100.100.100.100" {
63 t.Fatal("Test get real IP error.")
64 }
65 }
66}
67
68func TestXForwardForXRealIPPrecedence(t *testing.T) {
69 req, _ := http.NewRequest("GET", "/", nil)

Callers

nothing calls this directly

Calls 5

UseMethod · 0.95
GetMethod · 0.95
AddMethod · 0.80
WriteMethod · 0.65
ServeHTTPMethod · 0.45

Tested by

no test coverage detected