url_values_test.go 1017 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package expressions
  2. import (
  3. "net/url"
  4. "testing"
  5. "github.com/google/cel-go/common/types"
  6. )
  7. func TestURLValues(t *testing.T) {
  8. headers := URLValues{
  9. Values: url.Values{
  10. "format": {"json"},
  11. },
  12. }
  13. t.Run("contains-existing-key", func(t *testing.T) {
  14. resp := headers.Contains(types.String("format"))
  15. if !resp.(types.Bool) {
  16. t.Fatal("headers does not contain User-Agent")
  17. }
  18. })
  19. t.Run("not-contains-missing-key", func(t *testing.T) {
  20. resp := headers.Contains(types.String("not-there"))
  21. if resp.(types.Bool) {
  22. t.Fatal("headers does not contain User-Agent")
  23. }
  24. })
  25. t.Run("get-existing-key", func(t *testing.T) {
  26. val := headers.Get(types.String("format"))
  27. switch val.(type) {
  28. case types.String:
  29. // ok
  30. default:
  31. t.Fatalf("result was wrong type %T", val)
  32. }
  33. })
  34. t.Run("not-get-missing-key", func(t *testing.T) {
  35. val := headers.Get(types.String("not-there"))
  36. switch val.(type) {
  37. case *types.Err:
  38. // ok
  39. default:
  40. t.Fatalf("result was wrong type %T", val)
  41. }
  42. })
  43. }