35 lines
851 B
Go
35 lines
851 B
Go
package httpx_test
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"git.asio.asia/nixevol/NixMsg/internal/httpx"
|
|
)
|
|
|
|
func TestClientIPWithoutProxy(t *testing.T) {
|
|
r := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
r.RemoteAddr = "203.0.113.9:1234"
|
|
r.Header.Set("X-Forwarded-For", "198.51.100.1")
|
|
ip := httpx.ClientIP(r, nil)
|
|
if ip != "203.0.113.9" {
|
|
t.Fatalf("got %q", ip)
|
|
}
|
|
}
|
|
|
|
func TestParseCIDRsAndTrustedXFF(t *testing.T) {
|
|
trusted := httpx.ParseCIDRs([]string{"127.0.0.1/32"})
|
|
r := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
r.RemoteAddr = "127.0.0.1:9999"
|
|
r.Header.Set("X-Forwarded-For", "198.51.100.7, 127.0.0.1")
|
|
ip := httpx.ClientIP(r, trusted)
|
|
if ip != "198.51.100.7" {
|
|
t.Fatalf("got %q", ip)
|
|
}
|
|
r.Header.Set("X-Forwarded-Proto", "https")
|
|
if !httpx.IsHTTPS(r, trusted) {
|
|
t.Fatal("expected https via proxy")
|
|
}
|
|
}
|