5759c1862e
M0-M7 已完成:核心网关(身份/RBAC/TOTP/OIDC/SAML/Provider/配额/路由/内容策略/审计/定价)+ 资源市场(MCP/Skills/数字员工)。 含 22 个 PostgreSQL 迁移、管理端/门户端前端源码、OpenAPI 契约、部署 compose。 Co-Authored-By: Claude <noreply@anthropic.com>
35 lines
925 B
Go
35 lines
925 B
Go
package identity
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestClientIP(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
remoteAddr string
|
|
xfwd string
|
|
want string
|
|
}{
|
|
{"xfwd first value", "10.0.0.1:52341", "203.0.113.9, 10.0.0.2", "203.0.113.9"},
|
|
{"xfwd single", "10.0.0.1:52341", "198.51.100.7", "198.51.100.7"},
|
|
{"xfwd with spaces", "10.0.0.1:52341", " 192.0.2.5 ", "192.0.2.5"},
|
|
{"no xfwd falls back to remote", "203.0.113.9:8080", "", "203.0.113.9"},
|
|
{"no xfwd and no port", "[2001:db8::1]:443", "", "2001:db8::1"},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodPost, "/", nil)
|
|
req.RemoteAddr = tc.remoteAddr
|
|
if tc.xfwd != "" {
|
|
req.Header.Set("X-Forwarded-For", tc.xfwd)
|
|
}
|
|
if got := ClientIP(req); got != tc.want {
|
|
t.Fatalf("ClientIP() = %q, want %q", got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|