util_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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`,
  30. Method: `put`,
  31. },
  32. exp: false,
  33. },
  34. {
  35. cmd: command{
  36. Url: `/rules`,
  37. Method: `post`,
  38. Data: struct {
  39. id string
  40. sql string
  41. actions []struct{ log struct{} }
  42. }{
  43. id: `ruler1`,
  44. sql: `SELECT * FROM stream1`,
  45. },
  46. },
  47. exp: true,
  48. },
  49. {
  50. cmd: command{
  51. Url: `/rules`,
  52. Method: `get`,
  53. },
  54. exp: true,
  55. },
  56. {
  57. cmd: command{
  58. Url: `/rules/rule1`,
  59. Method: `get`,
  60. },
  61. exp: true,
  62. },
  63. {
  64. cmd: command{
  65. Url: `/rules/rule2`,
  66. Method: `delete`,
  67. },
  68. exp: true,
  69. },
  70. {
  71. cmd: command{
  72. Url: `/rules/rule1/stop`,
  73. Method: `post`,
  74. },
  75. exp: true,
  76. },
  77. {
  78. cmd: command{
  79. Url: `/rules/rule1/start`,
  80. Method: `post`,
  81. },
  82. exp: true,
  83. },
  84. }
  85. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  86. }))
  87. defer ts.Close()
  88. for _, v := range tests {
  89. ret := v.cmd.call(ts.URL)
  90. if v.exp != ret {
  91. t.Errorf("url:%s method:%s log:%s\n", v.cmd.Url, v.cmd.Method, v.cmd.getLog())
  92. }
  93. }
  94. }