17
17
package io .grpc .xds .internal .matchers ;
18
18
19
19
import static com .google .common .truth .Truth .assertThat ;
20
+ import static org .junit .Assert .assertThrows ;
20
21
21
22
import dev .cel .common .CelAbstractSyntaxTree ;
23
+ import dev .cel .common .CelErrorCode ;
24
+ import dev .cel .common .CelValidationException ;
22
25
import dev .cel .common .CelValidationResult ;
23
26
import dev .cel .common .types .SimpleType ;
24
27
import dev .cel .compiler .CelCompiler ;
25
28
import dev .cel .compiler .CelCompilerFactory ;
29
+ import dev .cel .parser .CelStandardMacro ;
26
30
import dev .cel .runtime .CelEvaluationException ;
27
31
import io .grpc .Metadata ;
28
32
import io .grpc .MethodDescriptor ;
29
33
import io .grpc .MethodDescriptor .MethodType ;
30
34
import io .grpc .NoopServerCall ;
31
35
import io .grpc .ServerCall ;
36
+ import io .grpc .Status ;
37
+ import io .grpc .Status .Code ;
38
+ import io .grpc .StatusRuntimeException ;
32
39
import io .grpc .StringMarshaller ;
33
40
import org .junit .Before ;
34
41
import org .junit .Test ;
@@ -43,9 +50,10 @@ public class CelMatcherTest {
43
50
CelCompilerFactory .standardCelCompilerBuilder ()
44
51
.addVar ("request" , SimpleType .ANY )
45
52
.setResultType (SimpleType .BOOL )
53
+ .setStandardMacros (CelStandardMacro .STANDARD_MACROS )
46
54
.build ();
47
55
private static final CelValidationResult celProg1 =
48
- CEL_COMPILER .compile ("request.method == \" POST\" " );
56
+ CEL_COMPILER .compile ("request.method == ' POST' " );
49
57
50
58
CelAbstractSyntaxTree ast1 ;
51
59
CelMatcher matcher1 ;
@@ -92,4 +100,34 @@ public void construct() throws CelEvaluationException {
92
100
public void testProgTrue () {
93
101
assertThat (matcher1 .test (fakeInput )).isTrue ();
94
102
}
103
+
104
+ @ Test
105
+ public void macros_comprehensionsDisabled () throws Exception {
106
+ CelMatcher matcherWithComprehensions = newMatcher (
107
+ "size(['foo', 'bar'].map(x, [request.headers[x], request.headers[x]])) == 1" );
108
+
109
+ Status status = assertThrows (StatusRuntimeException .class ,
110
+ () -> matcherWithComprehensions .test (fakeInput )).getStatus ();
111
+
112
+ assertThat (status .getCode ()).isEqualTo (Code .UNKNOWN );
113
+ assertThat (status .getCause ()).isInstanceOf (CelEvaluationException .class );
114
+
115
+ // Verify CelErrorCode is ITERATION_BUDGET_EXCEEDED.
116
+ CelEvaluationException cause = (CelEvaluationException ) status .getCause ();
117
+ assertThat (cause .getErrorCode ()).isEqualTo (CelErrorCode .ITERATION_BUDGET_EXCEEDED );
118
+ }
119
+
120
+ @ Test
121
+ public void macros_hasEnabled () throws Exception {
122
+ boolean result = newMatcher ("has(request.path)" ).test (fakeInput );
123
+ assertThat (result ).isTrue ();
124
+ }
125
+
126
+ private CelMatcher newMatcher (String expr ) throws CelValidationException , CelEvaluationException {
127
+ return CelMatcher .create (celAst (expr ));
128
+ }
129
+
130
+ private CelAbstractSyntaxTree celAst (String expr ) throws CelValidationException {
131
+ return CEL_COMPILER .compile (expr ).getAst ();
132
+ }
95
133
}
0 commit comments