jsonextract_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package exprhelpers
  2. import (
  3. "log"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestJsonExtract(t *testing.T) {
  8. if err := Init(nil); err != nil {
  9. log.Fatalf(err.Error())
  10. }
  11. err := FileInit(TestFolder, "test_data_re.txt", "regex")
  12. if err != nil {
  13. log.Fatalf(err.Error())
  14. }
  15. tests := []struct {
  16. name string
  17. jsonBlob string
  18. targetField string
  19. expectResult string
  20. }{
  21. {
  22. name: "basic json extract",
  23. jsonBlob: `{"test" : "1234"}`,
  24. targetField: "test",
  25. expectResult: "1234",
  26. },
  27. {
  28. name: "basic json extract with non existing field",
  29. jsonBlob: `{"test" : "1234"}`,
  30. targetField: "non_existing_field",
  31. expectResult: "",
  32. },
  33. {
  34. name: "extract subfield",
  35. jsonBlob: `{"test" : {"a": "b"}}`,
  36. targetField: "test.a",
  37. expectResult: "b",
  38. },
  39. }
  40. for _, test := range tests {
  41. result := JsonExtract(test.jsonBlob, test.targetField)
  42. isOk := assert.Equal(t, test.expectResult, result)
  43. if !isOk {
  44. t.Fatalf("test '%s' failed", test.name)
  45. }
  46. log.Printf("test '%s' : OK", test.name)
  47. }
  48. }
  49. func TestJsonExtractUnescape(t *testing.T) {
  50. if err := Init(nil); err != nil {
  51. log.Fatalf(err.Error())
  52. }
  53. err := FileInit(TestFolder, "test_data_re.txt", "regex")
  54. if err != nil {
  55. log.Fatalf(err.Error())
  56. }
  57. tests := []struct {
  58. name string
  59. jsonBlob string
  60. targetField string
  61. expectResult string
  62. }{
  63. {
  64. name: "basic json extract",
  65. jsonBlob: `{"log" : "\"GET /JBNwtQ6i.blt HTTP/1.1\" 200 13 \"-\" \"Craftbot\""}`,
  66. targetField: "log",
  67. expectResult: "\"GET /JBNwtQ6i.blt HTTP/1.1\" 200 13 \"-\" \"Craftbot\"",
  68. },
  69. {
  70. name: "basic json extract with non existing field",
  71. jsonBlob: `{"test" : "1234"}`,
  72. targetField: "non_existing_field",
  73. expectResult: "",
  74. },
  75. }
  76. for _, test := range tests {
  77. result := JsonExtractUnescape(test.jsonBlob, test.targetField)
  78. isOk := assert.Equal(t, test.expectResult, result)
  79. if !isOk {
  80. t.Fatalf("test '%s' failed", test.name)
  81. }
  82. log.Printf("test '%s' : OK", test.name)
  83. }
  84. }
  85. func TestJsonExtractSlice(t *testing.T) {
  86. if err := Init(nil); err != nil {
  87. log.Fatalf(err.Error())
  88. }
  89. err := FileInit(TestFolder, "test_data_re.txt", "regex")
  90. if err != nil {
  91. log.Fatalf(err.Error())
  92. }
  93. tests := []struct {
  94. name string
  95. jsonBlob string
  96. targetField string
  97. expectResult []interface{}
  98. }{
  99. {
  100. name: "try to extract a string as a slice",
  101. jsonBlob: `{"test" : "1234"}`,
  102. targetField: "test",
  103. expectResult: nil,
  104. },
  105. {
  106. name: "basic json slice extract",
  107. jsonBlob: `{"test" : ["1234"]}`,
  108. targetField: "test",
  109. expectResult: []interface{}{"1234"},
  110. },
  111. {
  112. name: "extract with complex expression",
  113. jsonBlob: `{"test": {"foo": [{"a":"b"}]}}`,
  114. targetField: "test.foo",
  115. expectResult: []interface{}{map[string]interface{}{"a": "b"}},
  116. },
  117. {
  118. name: "extract non-existing key",
  119. jsonBlob: `{"test: "11234"}`,
  120. targetField: "foo",
  121. expectResult: nil,
  122. },
  123. }
  124. for _, test := range tests {
  125. t.Run(test.name, func(t *testing.T) {
  126. result := JsonExtractSlice(test.jsonBlob, test.targetField)
  127. assert.Equal(t, test.expectResult, result)
  128. })
  129. }
  130. }
  131. func TestJsonExtractObject(t *testing.T) {
  132. if err := Init(nil); err != nil {
  133. log.Fatalf(err.Error())
  134. }
  135. err := FileInit(TestFolder, "test_data_re.txt", "regex")
  136. if err != nil {
  137. log.Fatalf(err.Error())
  138. }
  139. tests := []struct {
  140. name string
  141. jsonBlob string
  142. targetField string
  143. expectResult map[string]interface{}
  144. }{
  145. {
  146. name: "try to extract a string as an object",
  147. jsonBlob: `{"test" : "1234"}`,
  148. targetField: "test",
  149. expectResult: nil,
  150. },
  151. {
  152. name: "basic json object extract",
  153. jsonBlob: `{"test" : {"1234": {"foo": "bar"}}}`,
  154. targetField: "test",
  155. expectResult: map[string]interface{}{"1234": map[string]interface{}{"foo": "bar"}},
  156. },
  157. {
  158. name: "extract with complex expression",
  159. jsonBlob: `{"test": {"foo": [{"a":"b"}]}}`,
  160. targetField: "test.foo[0]",
  161. expectResult: map[string]interface{}{"a": "b"},
  162. },
  163. }
  164. for _, test := range tests {
  165. t.Run(test.name, func(t *testing.T) {
  166. result := JsonExtractObject(test.jsonBlob, test.targetField)
  167. assert.Equal(t, test.expectResult, result)
  168. })
  169. }
  170. }
  171. func TestToJson(t *testing.T) {
  172. tests := []struct {
  173. name string
  174. obj interface{}
  175. expectResult string
  176. }{
  177. {
  178. name: "convert int",
  179. obj: 42,
  180. expectResult: "42",
  181. },
  182. {
  183. name: "convert slice",
  184. obj: []string{"foo", "bar"},
  185. expectResult: `["foo","bar"]`,
  186. },
  187. {
  188. name: "convert map",
  189. obj: map[string]string{"foo": "bar"},
  190. expectResult: `{"foo":"bar"}`,
  191. },
  192. {
  193. name: "convert struct",
  194. obj: struct{ Foo string }{"bar"},
  195. expectResult: `{"Foo":"bar"}`,
  196. },
  197. {
  198. name: "convert complex struct",
  199. obj: struct {
  200. Foo string
  201. Bar struct {
  202. Baz string
  203. }
  204. Bla []string
  205. }{
  206. Foo: "bar",
  207. Bar: struct {
  208. Baz string
  209. }{
  210. Baz: "baz",
  211. },
  212. Bla: []string{"foo", "bar"},
  213. },
  214. expectResult: `{"Foo":"bar","Bar":{"Baz":"baz"},"Bla":["foo","bar"]}`,
  215. },
  216. {
  217. name: "convert invalid type",
  218. obj: func() {},
  219. expectResult: "",
  220. },
  221. }
  222. for _, test := range tests {
  223. t.Run(test.name, func(t *testing.T) {
  224. result := ToJson(test.obj)
  225. assert.Equal(t, test.expectResult, result)
  226. })
  227. }
  228. }