util_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package util
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "testing"
  6. )
  7. func TestCall(t *testing.T) {
  8. var tests = []struct {
  9. cmd command
  10. exp bool
  11. }{
  12. {
  13. cmd: command{
  14. Url: `/streams`,
  15. Method: `post`,
  16. Data: struct{ sql string }{sql: `create stream stream1 (id bigint, name string, score float) WITH ( datasource = \"topic/temperature\", FORMAT = \"json\", KEY = \"id\");`},
  17. },
  18. exp: true,
  19. },
  20. {
  21. cmd: command{
  22. Url: `/streams`,
  23. Method: `get`,
  24. },
  25. exp: true,
  26. },
  27. {
  28. cmd: command{
  29. Url: `/streams/stream1`,
  30. Method: `put`,
  31. Data: struct{ sql string }{sql: `create stream stream1 (id bigint, name string) WITH ( datasource = \"topic/temperature\", FORMAT = \"json\", KEY = \"id\");`},
  32. },
  33. exp: true,
  34. },
  35. {
  36. cmd: command{
  37. Url: `/rules`,
  38. Method: `post`,
  39. Data: struct {
  40. id string
  41. sql string
  42. actions []struct{ log struct{} }
  43. }{
  44. id: `ruler1`,
  45. sql: `SELECT * FROM stream1`,
  46. },
  47. },
  48. exp: true,
  49. },
  50. {
  51. cmd: command{
  52. Url: `/rules`,
  53. Method: `get`,
  54. },
  55. exp: true,
  56. },
  57. {
  58. cmd: command{
  59. Url: `/rules/rule1`,
  60. Method: `get`,
  61. },
  62. exp: true,
  63. },
  64. {
  65. cmd: command{
  66. Url: `/rules/rule2`,
  67. Method: `delete`,
  68. },
  69. exp: true,
  70. },
  71. {
  72. cmd: command{
  73. Url: `/rules/rule1/stop`,
  74. Method: `post`,
  75. },
  76. exp: true,
  77. },
  78. {
  79. cmd: command{
  80. Url: `/rules/rule1/start`,
  81. Method: `post`,
  82. },
  83. exp: true,
  84. },
  85. }
  86. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  87. }))
  88. defer ts.Close()
  89. for _, v := range tests {
  90. ret := v.cmd.call(ts.URL)
  91. if v.exp != ret {
  92. t.Errorf("url:%s method:%s log:%s\n", v.cmd.Url, v.cmd.Method, v.cmd.getLog())
  93. }
  94. }
  95. }