Refactor ClientIP X-Forwarded-For tests

This commit is contained in:
nurysso 2026-01-01 18:39:26 +05:30
parent 9b8104279b
commit dffc2e3fb1

View File

@ -1144,66 +1144,34 @@ func TestContextRenderNoContentIndentedJSON(t *testing.T) {
} }
func TestContextClientIPWithMultipleHeaders(t *testing.T) { func TestContextClientIPWithMultipleHeaders(t *testing.T) {
// Create a new Gin engine c, _ := CreateTestContext(httptest.NewRecorder())
engine := New() c.Request, _ = http.NewRequest(http.MethodGet, "/test", nil)
// Set trusted proxies // Multiple X-Forwarded-For headers
err := engine.SetTrustedProxies([]string{"127.0.0.1"}) c.Request.Header.Add("X-Forwarded-For", "1.2.3.4, "+localhostIP)
if err != nil { c.Request.Header.Add("X-Forwarded-For", "5.6.7.8")
t.Fatalf("Failed to set trusted proxies: %v", err) c.Request.RemoteAddr = localhostIP + ":1234"
}
engine.ForwardedByClientIP = true
engine.RemoteIPHeaders = []string{"X-Forwarded-For"}
// Create a test request with multiple X-Forwarded-For headers c.engine.ForwardedByClientIP = true
req := httptest.NewRequest(http.MethodGet, "/test", nil) c.engine.RemoteIPHeaders = []string{"X-Forwarded-For"}
req.Header.Add("X-Forwarded-For", "1.2.3.4, 127.0.0.1") _ = c.engine.SetTrustedProxies([]string{localhostIP})
req.Header.Add("X-Forwarded-For", "5.6.7.8")
req.RemoteAddr = "127.0.0.1:1234"
// Create response recorder // Should return 5.6.7.8 (last non-trusted IP)
w := httptest.NewRecorder() assert.Equal(t, "5.6.7.8", c.ClientIP())
// Create context
c, _ := CreateTestContext(w)
c.Request = req
c.engine = engine
// Test ClientIP
clientIP := c.ClientIP()
// Should return 5.6.7.8 (the last non-trusted IP)
expected := "5.6.7.8"
if clientIP != expected {
t.Errorf("Expected ClientIP to be %s, got %s", expected, clientIP)
}
} }
func TestContextClientIPWithSingleHeader(t *testing.T) { func TestContextClientIPWithSingleHeader(t *testing.T) {
engine := New() c, _ := CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest(http.MethodGet, "/test", nil)
c.Request.Header.Set("X-Forwarded-For", "1.2.3.4, "+localhostIP)
c.Request.RemoteAddr = localhostIP + ":1234"
if err := engine.SetTrustedProxies([]string{"127.0.0.1"}); err != nil { c.engine.ForwardedByClientIP = true
t.Fatalf("SetTrustedProxies failed: %v", err) c.engine.RemoteIPHeaders = []string{"X-Forwarded-For"}
} _ = c.engine.SetTrustedProxies([]string{localhostIP})
engine.ForwardedByClientIP = true
engine.RemoteIPHeaders = []string{"X-Forwarded-For"}
req := httptest.NewRequest(http.MethodGet, "/test", nil)
req.Header.Set("X-Forwarded-For", "1.2.3.4, 127.0.0.1")
req.RemoteAddr = "127.0.0.1:1234"
w := httptest.NewRecorder()
c, _ := CreateTestContext(w)
c.Request = req
c.engine = engine
clientIP := c.ClientIP()
// Should return 1.2.3.4 // Should return 1.2.3.4
expected := "1.2.3.4" assert.Equal(t, "1.2.3.4", c.ClientIP())
if clientIP != expected {
t.Errorf("Expected ClientIP to be %s, got %s", expected, clientIP)
}
} }
// Tests that the response is serialized as Secure JSON // Tests that the response is serialized as Secure JSON