rest_sink_test.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. s := &RestSink{}
  173. tt.config["url"] = ts.URL
  174. s.Configure(tt.config)
  175. s.Open(ctx)
  176. input, err := json.Marshal(tt.data)
  177. if err != nil {
  178. t.Errorf("Failed to parse the input into []byte]")
  179. continue
  180. }
  181. s.Collect(ctx, input)
  182. s.Close(ctx)
  183. if !reflect.DeepEqual(tt.result, requests) {
  184. t.Errorf("%d \tresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.result, requests)
  185. }
  186. }
  187. }
  188. func TestRestSinkTemplate_Apply(t *testing.T) {
  189. var tests = []struct {
  190. config map[string]interface{}
  191. data []map[string]interface{}
  192. result []request
  193. }{
  194. {
  195. config: map[string]interface{}{
  196. "method": "post",
  197. //"url": "http://localhost/test", //set dynamically to the test server
  198. "sendSingle": true,
  199. "dataTemplate": `{"wrapper":"w1","content":{{json .}},"ab":"{{.ab}}"}`,
  200. },
  201. data: []map[string]interface{}{{
  202. "ab": "hello1",
  203. }, {
  204. "ab": "hello2",
  205. }},
  206. result: []request{{
  207. Method: "POST",
  208. Body: `{"wrapper":"w1","content":{"ab":"hello1"},"ab":"hello1"}`,
  209. ContentType: "application/json",
  210. }, {
  211. Method: "POST",
  212. Body: `{"wrapper":"w1","content":{"ab":"hello2"},"ab":"hello2"}`,
  213. ContentType: "application/json",
  214. }},
  215. }, {
  216. config: map[string]interface{}{
  217. "method": "post",
  218. //"url": "http://localhost/test", //set dynamically to the test server
  219. "dataTemplate": `{"wrapper":"arr","content":{{json .}},"content0":{{json (index . 0)}},ab0":"{{index . 0 "ab"}}"}`,
  220. },
  221. data: []map[string]interface{}{{
  222. "ab": "hello1",
  223. }, {
  224. "ab": "hello2",
  225. }},
  226. result: []request{{
  227. Method: "POST",
  228. Body: `{"wrapper":"arr","content":[{"ab":"hello1"},{"ab":"hello2"}],"content0":{"ab":"hello1"},ab0":"hello1"}`,
  229. ContentType: "application/json",
  230. }},
  231. }, {
  232. config: map[string]interface{}{
  233. "method": "get",
  234. //"url": "http://localhost/test", //set dynamically to the test server
  235. "dataTemplate": `{"wrapper":"w1","content":{{json .}},"ab":"{{.ab}}"}`,
  236. },
  237. data: []map[string]interface{}{{
  238. "ab": "hello1",
  239. }, {
  240. "ab": "hello2",
  241. }},
  242. result: []request{{
  243. Method: "GET",
  244. ContentType: "",
  245. }},
  246. }, {
  247. config: map[string]interface{}{
  248. "method": "put",
  249. //"url": "http://localhost/test", //set dynamically to the test server
  250. "bodyType": "html",
  251. "dataTemplate": `<div>results</div><ul>{{range .}}<li>{{.ab}}</li>{{end}}</ul>`,
  252. },
  253. data: []map[string]interface{}{{
  254. "ab": "hello1",
  255. }, {
  256. "ab": "hello2",
  257. }},
  258. result: []request{{
  259. Method: "PUT",
  260. ContentType: "text/html",
  261. Body: `<div>results</div><ul><li>hello1</li><li>hello2</li></ul>`,
  262. }},
  263. }, {
  264. config: map[string]interface{}{
  265. "method": "post",
  266. //"url": "http://localhost/test", //set dynamically to the test server
  267. "bodyType": "form",
  268. "dataTemplate": `{"content":{{json .}}}`,
  269. },
  270. data: []map[string]interface{}{{
  271. "ab": "hello1",
  272. }, {
  273. "ab": "hello2",
  274. }},
  275. result: []request{{
  276. Method: "POST",
  277. ContentType: "application/x-www-form-urlencoded;param=value",
  278. Body: `content=%5B%7B%22ab%22%3A%22hello1%22%7D%2C%7B%22ab%22%3A%22hello2%22%7D%5D`,
  279. }},
  280. }, {
  281. config: map[string]interface{}{
  282. "method": "post",
  283. //"url": "http://localhost/test", //set dynamically to the test server
  284. "bodyType": "form",
  285. "sendSingle": true,
  286. "dataTemplate": `{"newab":"{{.ab}}"}`,
  287. },
  288. data: []map[string]interface{}{{
  289. "ab": "hello1",
  290. }, {
  291. "ab": "hello2",
  292. }},
  293. result: []request{{
  294. Method: "POST",
  295. ContentType: "application/x-www-form-urlencoded;param=value",
  296. Body: `newab=hello1`,
  297. }, {
  298. Method: "POST",
  299. ContentType: "application/x-www-form-urlencoded;param=value",
  300. Body: `newab=hello2`,
  301. }},
  302. }, {
  303. config: map[string]interface{}{
  304. "method": "post",
  305. //"url": "http://localhost/test", //set dynamically to the test server
  306. "bodyType": "json",
  307. "sendSingle": true,
  308. "timeout": float64(1000),
  309. "dataTemplate": `{"newab":"{{.ab}}"}`,
  310. },
  311. data: []map[string]interface{}{{
  312. "ab": "hello1",
  313. }, {
  314. "ab": "hello2",
  315. }},
  316. result: []request{{
  317. Method: "POST",
  318. Body: `{"newab":"hello1"}`,
  319. ContentType: "application/json",
  320. }, {
  321. Method: "POST",
  322. Body: `{"newab":"hello2"}`,
  323. ContentType: "application/json",
  324. }},
  325. },
  326. }
  327. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  328. contextLogger := common.Log.WithField("rule", "TestRestSink_Apply")
  329. ctx := contexts.WithValue(contexts.Background(), contexts.LoggerKey, contextLogger)
  330. var requests []request
  331. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  332. body, err := ioutil.ReadAll(r.Body)
  333. if err != nil {
  334. fmt.Printf("Error reading body: %v", err)
  335. http.Error(w, "can't read body", http.StatusBadRequest)
  336. return
  337. }
  338. requests = append(requests, request{
  339. Method: r.Method,
  340. Body: string(body),
  341. ContentType: r.Header.Get("Content-Type"),
  342. })
  343. contextLogger.Debugf(string(body))
  344. fmt.Fprintf(w, string(body))
  345. }))
  346. defer ts.Close()
  347. for i, tt := range tests {
  348. requests = nil
  349. s := &RestSink{}
  350. tt.config["url"] = ts.URL
  351. s.Configure(tt.config)
  352. s.Open(ctx)
  353. input, err := json.Marshal(tt.data)
  354. if err != nil {
  355. t.Errorf("Failed to parse the input into []byte]")
  356. continue
  357. }
  358. s.Collect(ctx, input)
  359. s.Close(ctx)
  360. if !reflect.DeepEqual(tt.result, requests) {
  361. t.Errorf("%d \tresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.result, requests)
  362. }
  363. }
  364. }