http_headers_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package expressions
  2. import (
  3. "net/http"
  4. "testing"
  5. "github.com/google/cel-go/common/types"
  6. )
  7. func TestHTTPHeaders(t *testing.T) {
  8. headers := HTTPHeaders{
  9. Header: http.Header{
  10. "Content-Type": {"application/json"},
  11. "Cf-Worker": {"true"},
  12. "User-Agent": {"Go-http-client/2"},
  13. },
  14. }
  15. t.Run("contains-existing-header", func(t *testing.T) {
  16. resp := headers.Contains(types.String("User-Agent"))
  17. if !resp.(types.Bool) {
  18. t.Fatal("headers does not contain User-Agent")
  19. }
  20. })
  21. t.Run("not-contains-missing-header", func(t *testing.T) {
  22. resp := headers.Contains(types.String("Xxx-Random-Header"))
  23. if resp.(types.Bool) {
  24. t.Fatal("headers does not contain User-Agent")
  25. }
  26. })
  27. t.Run("get-existing-header", func(t *testing.T) {
  28. val := headers.Get(types.String("User-Agent"))
  29. switch val.(type) {
  30. case types.String:
  31. // ok
  32. default:
  33. t.Fatalf("result was wrong type %T", val)
  34. }
  35. })
  36. t.Run("not-get-missing-header", func(t *testing.T) {
  37. val := headers.Get(types.String("Xxx-Random-Header"))
  38. switch val.(type) {
  39. case *types.Err:
  40. // ok
  41. default:
  42. t.Fatalf("result was wrong type %T", val)
  43. }
  44. })
  45. }