jsonpath_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package webhooks
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. )
  7. const jsonPathTestPayload = `{
  8. "repository": {"name": "OliveTin", "private": false, "stars": 5, "topics": null},
  9. "commits": [{"id": "a1"}, {"id": "b2"}],
  10. "ref": "refs/heads/main",
  11. "key with space": "spaced"
  12. }`
  13. func newTestJSONMatcher(t *testing.T) *JSONMatcher {
  14. t.Helper()
  15. matcher, err := NewJSONMatcher([]byte(jsonPathTestPayload))
  16. require.NoError(t, err)
  17. return matcher
  18. }
  19. func TestExtractValue(t *testing.T) {
  20. tests := []struct {
  21. path string
  22. expected string
  23. }{
  24. {"$.repository.name", "OliveTin"},
  25. {"$.ref", "refs/heads/main"},
  26. {"$.repository.private", "false"},
  27. {"$.repository.stars", "5"},
  28. {"$.repository.topics", "null"},
  29. {"$.commits[0].id", "a1"},
  30. {"$.commits[*].id", `["a1","b2"]`},
  31. {"$..id", `["a1","b2"]`},
  32. {"$['key with space']", "spaced"},
  33. {"repository.name", "OliveTin"},
  34. }
  35. matcher := newTestJSONMatcher(t)
  36. for _, tt := range tests {
  37. t.Run(tt.path, func(t *testing.T) {
  38. value, err := matcher.ExtractValue(tt.path)
  39. require.NoError(t, err)
  40. assert.Equal(t, tt.expected, value)
  41. })
  42. }
  43. }
  44. func TestExtractValueErrorsWhenPathIsAbsentOrInvalid(t *testing.T) {
  45. matcher := newTestJSONMatcher(t)
  46. for _, path := range []string{"$.missing", "$.repository.missing", "$.commits[5]", "$.ref.deeper", "", "$["} {
  47. t.Run(path, func(t *testing.T) {
  48. _, err := matcher.ExtractValue(path)
  49. assert.Error(t, err)
  50. })
  51. }
  52. }
  53. func TestMatchPath(t *testing.T) {
  54. matcher := newTestJSONMatcher(t)
  55. matches, err := matcher.MatchPath("$.repository.name", "OliveTin")
  56. require.NoError(t, err)
  57. assert.True(t, matches)
  58. matches, err = matcher.MatchPath("$.repository.private", "false")
  59. require.NoError(t, err)
  60. assert.True(t, matches)
  61. matches, err = matcher.MatchPath("$.repository.name", "Other")
  62. require.NoError(t, err)
  63. assert.False(t, matches)
  64. _, err = matcher.MatchPath("$.missing", "x")
  65. assert.Error(t, err)
  66. }
  67. func TestEmptyCollectionIsExtractedAndMatchedAsAnEmptyList(t *testing.T) {
  68. matcher, err := NewJSONMatcher([]byte(`{"commits": []}`))
  69. require.NoError(t, err)
  70. value, err := matcher.ExtractValue("$.commits[*].id")
  71. require.NoError(t, err)
  72. assert.Equal(t, "[]", value)
  73. matched, err := matcher.MatchPath("$.commits[*].id", "[]")
  74. require.NoError(t, err)
  75. assert.True(t, matched)
  76. found, err := matcher.HasValue("$.commits[*].id")
  77. require.NoError(t, err)
  78. assert.False(t, found, "an empty collection has no value for an existence check")
  79. }
  80. func TestHasValue(t *testing.T) {
  81. matcher := newTestJSONMatcher(t)
  82. cases := map[string]bool{
  83. "$.repository.name": true,
  84. "$.repository.topics": true,
  85. "$.commits[*].id": true,
  86. "$.missing": false,
  87. "$.commits[5]": false,
  88. }
  89. for path, expected := range cases {
  90. t.Run(path, func(t *testing.T) {
  91. found, err := matcher.HasValue(path)
  92. require.NoError(t, err)
  93. assert.Equal(t, expected, found)
  94. })
  95. }
  96. }