rest_sink_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. package sinks
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/emqx/kuiper/common"
  6. "github.com/emqx/kuiper/xstream/contexts"
  7. "io/ioutil"
  8. "net/http"
  9. "net/http/httptest"
  10. "reflect"
  11. "testing"
  12. )
  13. type request struct {
  14. Method string
  15. Body string
  16. ContentType string
  17. }
  18. func TestRestSink_Apply(t *testing.T) {
  19. var tests = []struct {
  20. config map[string]interface{}
  21. data []map[string]interface{}
  22. result []request
  23. }{
  24. {
  25. config: map[string]interface{}{
  26. "method": "post",
  27. //"url": "http://localhost/test", //set dynamically to the test server
  28. "sendSingle": true,
  29. },
  30. data: []map[string]interface{}{{
  31. "ab": "hello1",
  32. }, {
  33. "ab": "hello2",
  34. }},
  35. result: []request{{
  36. Method: "POST",
  37. Body: `{"ab":"hello1"}`,
  38. ContentType: "application/json",
  39. }, {
  40. Method: "POST",
  41. Body: `{"ab":"hello2"}`,
  42. ContentType: "application/json",
  43. }},
  44. }, {
  45. config: map[string]interface{}{
  46. "method": "post",
  47. //"url": "http://localhost/test", //set dynamically to the test server
  48. },
  49. data: []map[string]interface{}{{
  50. "ab": "hello1",
  51. }, {
  52. "ab": "hello2",
  53. }},
  54. result: []request{{
  55. Method: "POST",
  56. Body: `[{"ab":"hello1"},{"ab":"hello2"}]`,
  57. ContentType: "application/json",
  58. }},
  59. }, {
  60. config: map[string]interface{}{
  61. "method": "get",
  62. //"url": "http://localhost/test", //set dynamically to the test server
  63. },
  64. data: []map[string]interface{}{{
  65. "ab": "hello1",
  66. }, {
  67. "ab": "hello2",
  68. }},
  69. result: []request{{
  70. Method: "GET",
  71. ContentType: "",
  72. }},
  73. }, {
  74. config: map[string]interface{}{
  75. "method": "put",
  76. //"url": "http://localhost/test", //set dynamically to the test server
  77. "bodyType": "text",
  78. },
  79. data: []map[string]interface{}{{
  80. "ab": "hello1",
  81. }, {
  82. "ab": "hello2",
  83. }},
  84. result: []request{{
  85. Method: "PUT",
  86. ContentType: "text/plain",
  87. Body: `[{"ab":"hello1"},{"ab":"hello2"}]`,
  88. }},
  89. }, {
  90. config: map[string]interface{}{
  91. "method": "post",
  92. //"url": "http://localhost/test", //set dynamically to the test server
  93. "bodyType": "form",
  94. },
  95. data: []map[string]interface{}{{
  96. "ab": "hello1",
  97. }, {
  98. "ab": "hello2",
  99. }},
  100. result: []request{{
  101. Method: "POST",
  102. ContentType: "application/x-www-form-urlencoded;param=value",
  103. Body: `result=%5B%7B%22ab%22%3A%22hello1%22%7D%2C%7B%22ab%22%3A%22hello2%22%7D%5D`,
  104. }},
  105. }, {
  106. config: map[string]interface{}{
  107. "method": "post",
  108. //"url": "http://localhost/test", //set dynamically to the test server
  109. "bodyType": "form",
  110. "sendSingle": true,
  111. },
  112. data: []map[string]interface{}{{
  113. "ab": "hello1",
  114. }, {
  115. "ab": "hello2",
  116. }},
  117. result: []request{{
  118. Method: "POST",
  119. ContentType: "application/x-www-form-urlencoded;param=value",
  120. Body: `ab=hello1`,
  121. }, {
  122. Method: "POST",
  123. ContentType: "application/x-www-form-urlencoded;param=value",
  124. Body: `ab=hello2`,
  125. }},
  126. }, {
  127. config: map[string]interface{}{
  128. "method": "post",
  129. //"url": "http://localhost/test", //set dynamically to the test server
  130. "bodyType": "json",
  131. "sendSingle": true,
  132. "timeout": float64(1000),
  133. },
  134. data: []map[string]interface{}{{
  135. "ab": "hello1",
  136. }, {
  137. "ab": "hello2",
  138. }},
  139. result: []request{{
  140. Method: "POST",
  141. Body: `{"ab":"hello1"}`,
  142. ContentType: "application/json",
  143. }, {
  144. Method: "POST",
  145. Body: `{"ab":"hello2"}`,
  146. ContentType: "application/json",
  147. }},
  148. },
  149. }
  150. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  151. contextLogger := common.Log.WithField("rule", "TestRestSink_Apply")
  152. ctx := contexts.WithValue(contexts.Background(), contexts.LoggerKey, contextLogger)
  153. var requests []request
  154. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  155. body, err := ioutil.ReadAll(r.Body)
  156. if err != nil {
  157. fmt.Printf("Error reading body: %v", err)
  158. http.Error(w, "can't read body", http.StatusBadRequest)
  159. return
  160. }
  161. requests = append(requests, request{
  162. Method: r.Method,
  163. Body: string(body),
  164. ContentType: r.Header.Get("Content-Type"),
  165. })
  166. contextLogger.Debugf(string(body))
  167. fmt.Fprintf(w, string(body))
  168. }))
  169. defer ts.Close()
  170. for i, tt := range tests {
  171. requests = nil
  172. ss, ok := tt.config["sendSingle"]
  173. if !ok {
  174. ss = false
  175. }
  176. s := &RestSink{}
  177. tt.config["url"] = ts.URL
  178. s.Configure(tt.config)
  179. s.Open(ctx)
  180. if ss.(bool) {
  181. for _, d := range tt.data {
  182. input, err := json.Marshal(d)
  183. if err != nil {
  184. t.Errorf("Failed to parse the input into []byte]")
  185. continue
  186. }
  187. s.Collect(ctx, input)
  188. }
  189. } else {
  190. input, err := json.Marshal(tt.data)
  191. if err != nil {
  192. t.Errorf("Failed to parse the input into []byte]")
  193. continue
  194. }
  195. s.Collect(ctx, input)
  196. }
  197. s.Close(ctx)
  198. if !reflect.DeepEqual(tt.result, requests) {
  199. t.Errorf("%d \tresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.result, requests)
  200. }
  201. }
  202. }
  203. func TestRestSinkTemplate_Apply(t *testing.T) {
  204. var tests = []struct {
  205. config map[string]interface{}
  206. data [][]byte
  207. result []request
  208. }{
  209. {
  210. config: map[string]interface{}{
  211. "method": "post",
  212. //"url": "http://localhost/test", //set dynamically to the test server
  213. "sendSingle": true,
  214. "dataTemplate": `{"wrapper":"w1","content":{{json .}},"ab":"{{.ab}}"}`,
  215. },
  216. data: [][]byte{[]byte(`{"wrapper":"w1","content":{"ab":"hello1"},"ab":"hello1"}`), []byte(`{"wrapper":"w1","content":{"ab":"hello2"},"ab":"hello2"}`)},
  217. result: []request{{
  218. Method: "POST",
  219. Body: `{"wrapper":"w1","content":{"ab":"hello1"},"ab":"hello1"}`,
  220. ContentType: "application/json",
  221. }, {
  222. Method: "POST",
  223. Body: `{"wrapper":"w1","content":{"ab":"hello2"},"ab":"hello2"}`,
  224. ContentType: "application/json",
  225. }},
  226. }, {
  227. config: map[string]interface{}{
  228. "method": "post",
  229. //"url": "http://localhost/test", //set dynamically to the test server
  230. "dataTemplate": `{"wrapper":"arr","content":{{json .}},"content0":{{json (index . 0)}},ab0":"{{index . 0 "ab"}}"}`,
  231. },
  232. data: [][]byte{[]byte(`{"wrapper":"arr","content":[{"ab":"hello1"},{"ab":"hello2"}],"content0":{"ab":"hello1"},ab0":"hello1"}`)},
  233. result: []request{{
  234. Method: "POST",
  235. Body: `{"wrapper":"arr","content":[{"ab":"hello1"},{"ab":"hello2"}],"content0":{"ab":"hello1"},ab0":"hello1"}`,
  236. ContentType: "application/json",
  237. }},
  238. }, {
  239. config: map[string]interface{}{
  240. "method": "get",
  241. //"url": "http://localhost/test", //set dynamically to the test server
  242. "dataTemplate": `{"wrapper":"w1","content":{{json .}},"ab":"{{.ab}}"}`,
  243. },
  244. data: [][]byte{[]byte(`{"wrapper":"w1","content":{"ab":"hello1"},"ab":"hello1"}`)},
  245. result: []request{{
  246. Method: "GET",
  247. ContentType: "",
  248. }},
  249. }, {
  250. config: map[string]interface{}{
  251. "method": "put",
  252. //"url": "http://localhost/test", //set dynamically to the test server
  253. "bodyType": "html",
  254. "dataTemplate": `<div>results</div><ul>{{range .}}<li>{{.ab}}</li>{{end}}</ul>`,
  255. },
  256. data: [][]byte{[]byte(`<div>results</div><ul><li>hello1</li><li>hello2</li></ul>`)},
  257. result: []request{{
  258. Method: "PUT",
  259. ContentType: "text/html",
  260. Body: `<div>results</div><ul><li>hello1</li><li>hello2</li></ul>`,
  261. }},
  262. }, {
  263. config: map[string]interface{}{
  264. "method": "post",
  265. //"url": "http://localhost/test", //set dynamically to the test server
  266. "bodyType": "form",
  267. "dataTemplate": `{"content":{{json .}}}`,
  268. },
  269. data: [][]byte{[]byte(`{"content":[{"ab":"hello1"},{"ab":"hello2"}]}`)},
  270. result: []request{{
  271. Method: "POST",
  272. ContentType: "application/x-www-form-urlencoded;param=value",
  273. Body: `content=%5B%7B%22ab%22%3A%22hello1%22%7D%2C%7B%22ab%22%3A%22hello2%22%7D%5D`,
  274. }},
  275. }, {
  276. config: map[string]interface{}{
  277. "method": "post",
  278. //"url": "http://localhost/test", //set dynamically to the test server
  279. "bodyType": "form",
  280. "sendSingle": true,
  281. "dataTemplate": `{"newab":"{{.ab}}"}`,
  282. },
  283. data: [][]byte{[]byte(`{"newab":"hello1"}`), []byte(`{"newab":"hello2"}`)},
  284. result: []request{{
  285. Method: "POST",
  286. ContentType: "application/x-www-form-urlencoded;param=value",
  287. Body: `newab=hello1`,
  288. }, {
  289. Method: "POST",
  290. ContentType: "application/x-www-form-urlencoded;param=value",
  291. Body: `newab=hello2`,
  292. }},
  293. }, {
  294. config: map[string]interface{}{
  295. "method": "post",
  296. //"url": "http://localhost/test", //set dynamically to the test server
  297. "bodyType": "json",
  298. "sendSingle": true,
  299. "timeout": float64(1000),
  300. "dataTemplate": `{"newab":"{{.ab}}"}`,
  301. },
  302. data: [][]byte{[]byte(`{"newab":"hello1"}`), []byte(`{"newab":"hello2"}`)},
  303. result: []request{{
  304. Method: "POST",
  305. Body: `{"newab":"hello1"}`,
  306. ContentType: "application/json",
  307. }, {
  308. Method: "POST",
  309. Body: `{"newab":"hello2"}`,
  310. ContentType: "application/json",
  311. }},
  312. },
  313. }
  314. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  315. contextLogger := common.Log.WithField("rule", "TestRestSink_Apply")
  316. ctx := contexts.WithValue(contexts.Background(), contexts.LoggerKey, contextLogger)
  317. var requests []request
  318. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  319. body, err := ioutil.ReadAll(r.Body)
  320. if err != nil {
  321. fmt.Printf("Error reading body: %v", err)
  322. http.Error(w, "can't read body", http.StatusBadRequest)
  323. return
  324. }
  325. requests = append(requests, request{
  326. Method: r.Method,
  327. Body: string(body),
  328. ContentType: r.Header.Get("Content-Type"),
  329. })
  330. contextLogger.Debugf(string(body))
  331. fmt.Fprintf(w, string(body))
  332. }))
  333. defer ts.Close()
  334. for i, tt := range tests {
  335. requests = nil
  336. s := &RestSink{}
  337. tt.config["url"] = ts.URL
  338. s.Configure(tt.config)
  339. s.Open(ctx)
  340. for _, d := range tt.data {
  341. s.Collect(ctx, d)
  342. }
  343. s.Close(ctx)
  344. if !reflect.DeepEqual(tt.result, requests) {
  345. t.Errorf("%d \tresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.result, requests)
  346. }
  347. }
  348. }