|
20 | 20 | import static org.junit.Assert.fail; |
21 | 21 |
|
22 | 22 | import com.google.common.io.BaseEncoding; |
| 23 | +import dev.cel.common.CelAbstractSyntaxTree; |
23 | 24 | import dev.cel.common.CelValidationException; |
| 25 | +import dev.cel.common.types.SimpleType; |
| 26 | +import dev.cel.compiler.CelCompiler; |
| 27 | +import dev.cel.compiler.CelCompilerFactory; |
| 28 | +import dev.cel.runtime.CelEvaluationException; |
| 29 | +import dev.cel.runtime.CelRuntime; |
24 | 30 | import io.grpc.Metadata; |
25 | 31 | import java.util.Map; |
26 | 32 | import java.util.Optional; |
|
33 | 39 | @RunWith(JUnit4.class) |
34 | 40 | public final class CelEnvironmentTest { |
35 | 41 |
|
| 42 | + private static final CelCompiler LENIENT_COMPILER = |
| 43 | + CelCompilerFactory.standardCelCompilerBuilder() |
| 44 | + .addVar("request", SimpleType.DYN) |
| 45 | + .build(); |
| 46 | + |
| 47 | + private static CelAbstractSyntaxTree compileLenientAst(String expression) |
| 48 | + throws CelValidationException { |
| 49 | + return LENIENT_COMPILER.compile(expression).getAst(); |
| 50 | + } |
| 51 | + |
36 | 52 | @Test |
37 | 53 | public void headersWrapper_resolvesPseudoHeaders() { |
38 | 54 | MatchContext context = MatchContext.newBuilder() |
@@ -192,6 +208,42 @@ public void celEnvironment_disabledFeatures_throwsValidationException() { |
192 | 208 | } |
193 | 209 | } |
194 | 210 |
|
| 211 | + @Test |
| 212 | + public void celEnvironment_runtime_disabledFeatures_throwsException() throws Exception { |
| 213 | + MatchContext context = MatchContext.newBuilder().build(); |
| 214 | + GrpcCelEnvironment resolver = new GrpcCelEnvironment(context); |
| 215 | + |
| 216 | + // String concatenation fails at runtime evaluation (missing overload) |
| 217 | + CelAbstractSyntaxTree stringConcatAst = compileLenientAst("'a' + 'b'"); |
| 218 | + CelRuntime.Program stringConcatProgram = CelCommon.RUNTIME.createProgram(stringConcatAst); |
| 219 | + try { |
| 220 | + stringConcatProgram.eval(resolver); |
| 221 | + Assert.fail("String concatenation evaluation should fail"); |
| 222 | + } catch (CelEvaluationException e) { |
| 223 | + assertThat(e).hasMessageThat().contains("No matching overload"); |
| 224 | + } |
| 225 | + |
| 226 | + // List concatenation fails at runtime evaluation (missing overload) |
| 227 | + CelAbstractSyntaxTree listConcatAst = compileLenientAst("[1] + [2]"); |
| 228 | + CelRuntime.Program listConcatProgram = CelCommon.RUNTIME.createProgram(listConcatAst); |
| 229 | + try { |
| 230 | + listConcatProgram.eval(resolver); |
| 231 | + Assert.fail("List concatenation evaluation should fail"); |
| 232 | + } catch (CelEvaluationException e) { |
| 233 | + assertThat(e).hasMessageThat().contains("No matching overload"); |
| 234 | + } |
| 235 | + |
| 236 | + // String conversion fails at runtime evaluation (missing overload/function) |
| 237 | + CelAbstractSyntaxTree stringConvAst = compileLenientAst("string(1)"); |
| 238 | + CelRuntime.Program stringConvProgram = CelCommon.RUNTIME.createProgram(stringConvAst); |
| 239 | + try { |
| 240 | + stringConvProgram.eval(resolver); |
| 241 | + Assert.fail("String conversion evaluation should fail"); |
| 242 | + } catch (CelEvaluationException e) { |
| 243 | + assertThat(e).hasMessageThat().contains("No matching overload"); |
| 244 | + } |
| 245 | + } |
| 246 | + |
195 | 247 | @Test |
196 | 248 | public void celEnvironment_method_fallback() { |
197 | 249 | MatchContext context = MatchContext.newBuilder().build(); |
|
0 commit comments