upaas/internal/middleware/realip_test.go
clawbot 559bfa4131 fix: resolve all golangci-lint issues
Fixes #32

Changes:
- middleware.go: use max() builtin, strconv.Itoa, fix wsl whitespace
- database.go: fix nlreturn, noinlineerr, wsl whitespace
- handlers.go: remove unnecessary template.HTML conversion, unused import
- app.go: extract cleanupContainer to fix nestif, fix lll
- client.go: break long string literals to fix lll
- deploy.go: fix wsl whitespace
- auth_test.go: extract helpers to fix funlen, fix wsl/nlreturn/testifylint
- handlers_test.go: deduplicate IDOR tests, fix paralleltest
- validation_test.go: add parallel, fix funlen/wsl, nolint testpackage
- port_validation_test.go: add parallel, nolint testpackage
- ratelimit_test.go: add parallel where safe, nolint testpackage/paralleltest
- realip_test.go: add parallel, use NewRequestWithContext, fix wsl/funlen
- user.go: (noinlineerr already fixed by database.go pattern)
2026-02-15 21:55:24 -08:00

91 lines
2.0 KiB
Go

package middleware //nolint:testpackage // tests unexported realIP function
import (
"context"
"net/http"
"testing"
)
func TestRealIP(t *testing.T) { //nolint:funlen // table-driven test
t.Parallel()
tests := []struct {
name string
remoteAddr string
xRealIP string
xff string
want string
}{
{
name: "X-Real-IP takes priority",
remoteAddr: "10.0.0.1:1234",
xRealIP: "203.0.113.5",
xff: "198.51.100.1, 10.0.0.1",
want: "203.0.113.5",
},
{
name: "X-Forwarded-For used when no X-Real-IP",
remoteAddr: "10.0.0.1:1234",
xff: "198.51.100.1, 10.0.0.1",
want: "198.51.100.1",
},
{
name: "X-Forwarded-For single IP",
remoteAddr: "10.0.0.1:1234",
xff: "203.0.113.10",
want: "203.0.113.10",
},
{
name: "falls back to RemoteAddr",
remoteAddr: "192.168.1.1:5678",
want: "192.168.1.1",
},
{
name: "RemoteAddr without port",
remoteAddr: "192.168.1.1",
want: "192.168.1.1",
},
{
name: "X-Real-IP with whitespace",
remoteAddr: "10.0.0.1:1234",
xRealIP: " 203.0.113.5 ",
want: "203.0.113.5",
},
{
name: "X-Forwarded-For with whitespace",
remoteAddr: "10.0.0.1:1234",
xff: " 198.51.100.1 , 10.0.0.1",
want: "198.51.100.1",
},
{
name: "empty X-Real-IP falls through to XFF",
remoteAddr: "10.0.0.1:1234",
xRealIP: " ",
xff: "198.51.100.1",
want: "198.51.100.1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/", nil)
req.RemoteAddr = tt.remoteAddr
if tt.xRealIP != "" {
req.Header.Set("X-Real-IP", tt.xRealIP)
}
if tt.xff != "" {
req.Header.Set("X-Forwarded-For", tt.xff)
}
got := realIP(req)
if got != tt.want {
t.Errorf("realIP() = %q, want %q", got, tt.want)
}
})
}
}