@@ -20,52 +20,10 @@ package v2
2020import (
2121 "testing"
2222
23- "github.com/google/cel-go/cel"
2423 "github.com/stretchr/testify/assert"
2524 "github.com/stretchr/testify/require"
2625)
2726
28- // celSubjectRule is the CEL expression embedded via +kubebuilder:validation:XValidation
29- // on ApisixRouteHTTPMatchExprSubject. This test validates its correctness.
30- const celSubjectRule = "self.scope == 'Path' || self.name != ''"
31-
32- // evalCELSubjectRule evaluates celSubjectRule against a fake subject object.
33- func evalCELSubjectRule (t * testing.T , scope , name string ) bool {
34- t .Helper ()
35- env , err := cel .NewEnv (
36- cel .Variable ("self" , cel .MapType (cel .StringType , cel .StringType )),
37- )
38- require .NoError (t , err )
39-
40- ast , issues := env .Compile (celSubjectRule )
41- require .NoError (t , issues .Err ())
42-
43- prg , err := env .Program (ast )
44- require .NoError (t , err )
45-
46- out , _ , err := prg .Eval (map [string ]any {
47- "self" : map [string ]any {"scope" : scope , "name" : name },
48- })
49- require .NoError (t , err )
50- return out .Value ().(bool )
51- }
52-
53- func TestCEL_SubjectRule_ValidScopes (t * testing.T ) {
54- // All non-Path scopes with a non-empty name must pass.
55- for _ , scope := range []string {ScopeHeader , ScopeQuery , ScopeCookie , ScopeVariable , ScopeBody } {
56- assert .True (t , evalCELSubjectRule (t , scope , "field" ), "scope=%s with name should pass" , scope )
57- }
58- // Path scope with empty name must pass (name is ignored for Path).
59- assert .True (t , evalCELSubjectRule (t , ScopePath , "" ), "Path with empty name should pass" )
60- }
61-
62- func TestCEL_SubjectRule_InvalidEmptyName (t * testing.T ) {
63- // Non-Path scopes with empty name must fail.
64- for _ , scope := range []string {ScopeHeader , ScopeQuery , ScopeCookie , ScopeVariable , ScopeBody } {
65- assert .False (t , evalCELSubjectRule (t , scope , "" ), "scope=%s with empty name should fail" , scope )
66- }
67- }
68-
6927func strPtr (s string ) * string { return & s }
7028
7129func TestToVars_ScopeBody_SimpleField (t * testing.T ) {
0 commit comments