Repository: gin-gonic/gin
File: context_test.go (Go)

Code before:
	req := httptest.NewRequest("GET", "/test", nil)
	req.Header.Add("X-Forwarded-For", "1.2.3.4, 127.0.0.1")
	req.Header.Add("X-Forwarded-For", "5.6.7.8")
	req.RemoteAddr = "127.0.0.1:1234"

	// Create response recorder
	w := httptest.NewRecorder()

	// 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) {
	engine := New()
	engine.SetTrustedProxies([]string{"127.0.0.1"})
	engine.ForwardedByClientIP = true
	engine.RemoteIPHeaders = []string{"X-Forwarded-For"}

	req := httptest.NewRequest("GET", "/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
	expected := "1.2.3.4"
	if clientIP != expected {
		t.Errorf("Expected ClientIP to be %s, got %s", expected, clientIP)
	}
}
// Tests that the response is serialized as Secure JSON
// and Content-Type is set to application/json
func TestContextRenderSecureJSON(t *testing.T) {
	w := httptest.NewRecorder()
	c, router := CreateTestContext(w)

Review comment (suggestion):
```suggestion
	engine.SetTrustedProxies([]string{localhostIP})
```

Code after:
	c.engine.RemoteIPHeaders = []string{"X-Forwarded-For"}
	_ = c.engine.SetTrustedProxies([]string{localhostIP})

	// Should return 5.6.7.8 (last non-trusted IP)
	assert.Equal(t, "5.6.7.8", c.ClientIP())
}

func TestContextClientIPWithSingleHeader(t *testing.T) {
	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"

	c.engine.ForwardedByClientIP = true
	c.engine.RemoteIPHeaders = []string{"X-Forwarded-For"}
	_ = c.engine.SetTrustedProxies([]string{localhostIP})

	// Should return 1.2.3.4
	assert.Equal(t, "1.2.3.4", c.ClientIP())
}

// Tests that the response is serialized as Secure JSON
// and Content-Type is set to application/json
func TestContextRenderSecureJSON(t *testing.T) {
	w := httptest.NewRecorder()
	c, router := CreateTestContext(w)

	router.SecureJsonPrefix("&&&START&&&")
	c.SecureJSON(http.StatusCreated, []string{"foo", "bar"})

	assert.Equal(t, http.StatusCreated, w.Code)
	assert.Equal(t, "&&&START&&&[\"foo\",\"bar\"]", w.Body.String())
	assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
}

// Tests that no Custom JSON is rendered if code is 204
func TestContextRenderNoContentSecureJSON(t *testing.T) {
	w := httptest.NewRecorder()
	c, _ := CreateTestContext(w)

	c.SecureJSON(http.StatusNoContent, []string{"foo", "bar"})

	assert.Equal(t, http.StatusNoContent, w.Code)
	assert.Empty(t, w.Body.String())
	assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
}

func TestContextRenderNoContentAsciiJSON(t *testing.T) {
	w := httptest.NewRecorder()
	c, _ := CreateTestContext(w)