package towel import ( "context" "testing" ) // TestClientIP pins plan 07's client address on the context (B-IN-04): the // value round-trips, an absent value answers false, and an empty address // is stored as given. func TestClientIP(t *testing.T) { t.Run("round trip", func(t *testing.T) { ip, ok := ClientIP(WithClientIP(context.Background(), "203.0.113.7")) if !ok || ip != "203.0.113.7" { t.Fatalf("ClientIP = %q, %v", ip, ok) } }) t.Run("absent", func(t *testing.T) { if ip, ok := ClientIP(context.Background()); ok || ip != "" { t.Fatalf("ClientIP(empty context) = %q, %v", ip, ok) } if ip, ok := ClientIP(WithLocale(context.Background(), "pl")); ok || ip != "" { t.Fatalf("ClientIP(other values only) = %q, %v", ip, ok) } }) t.Run("empty string", func(t *testing.T) { ip, ok := ClientIP(WithClientIP(context.Background(), "")) if !ok || ip != "" { t.Fatalf("ClientIP(empty address) = %q, %v", ip, ok) } }) }