| Default Header Tests | | |
uses X-Forwarded-For | Multiple IPs in X-Forwarded-For header | Extracts first IP (203.0.113.195) from comma-separated list |
uses X-Real-IP | Single IP in X-Real-IP header | Returns the IP from X-Real-IP (198.51.100.178) |
uses CF-Connecting-IP | Cloudflare connecting IP header | Returns the IP from CF-Connecting-IP (192.0.2.1) |
falls back to RemoteAddr | No proxy headers present | Uses RemoteAddr (192.168.1.100) |
| Trusted Proxy Tests | | |
trusts headers from trusted proxy | Request from 10.0.0.1 with X-Forwarded-For | Returns IP from header (203.0.113.195) |
ignores headers from untrusted source | Request from 192.168.1.1 (not in trusted range) | Ignores header, returns RemoteAddr (192.168.1.1) |
| Single IP Trust | | |
WithTrustedProxies single IP | Trust specific IP (10.0.0.1) | Accepts headers from exact IP match |
| Custom Headers | | |
WithOptions custom headers | Configure X-Custom-IP as trusted header | Extracts IP from custom header (5.6.7.8) |
| API Compatibility | | |
Get() alias test | FromContext vs Get methods | Both return identical IP values |
| Helper Function Tests | | |
extractFirstIP - single IP | Parse β203.0.113.195β | Returns 203.0.113.195 |
extractFirstIP - comma-separated | Parse β203.0.113.195, 70.41.3.18, 150.172.238.178β | Returns first IP (203.0.113.195) |
extractFirstIP - whitespace | Parse β 203.0.113.195 β | Trims and returns 203.0.113.195 |
extractFirstIP - invalid | Parse βinvalidβ | Returns empty string |
extractFirstIP - empty | Parse "" | Returns empty string |
extractFirstIP - IPv6 | Parse β2001:db8::1β | Returns IPv6 address |
extractIP - with port | Parse β192.168.1.1:8080β | Returns IP without port (192.168.1.1) |
extractIP - without port | Parse β192.168.1.1β | Returns IP as-is (192.168.1.1) |
extractIP - IPv6 with port | Parse β[::1]:8080β | Returns IPv6 without port (::1) |
isTrusted - within CIDR | Check 10.0.0.1 in 10.0.0.0/8 | Returns true |
isTrusted - edge of CIDR | Check 10.255.255.255 in 10.0.0.0/8 | Returns true |
isTrusted - outside CIDR | Check 11.0.0.1 in 10.0.0.0/8 | Returns false |
isTrusted - multiple networks | Check 192.168.1.1 in [10.0.0.0/8, 192.168.0.0/16] | Returns true (matches second network) |