util_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2021 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package util
  15. import (
  16. "net/http"
  17. "net/http/httptest"
  18. "testing"
  19. )
  20. func TestCall(t *testing.T) {
  21. tests := []struct {
  22. cmd command
  23. exp bool
  24. }{
  25. {
  26. cmd: command{
  27. Url: `/streams`,
  28. Method: `post`,
  29. Data: struct{ sql string }{sql: `create stream stream1 (id bigint, name string, score float) WITH ( datasource = \"topic/temperature\", FORMAT = \"json\", KEY = \"id\");`},
  30. },
  31. exp: true,
  32. },
  33. {
  34. cmd: command{
  35. Url: `/streams`,
  36. Method: `get`,
  37. },
  38. exp: true,
  39. },
  40. {
  41. cmd: command{
  42. Url: `/streams/stream1`,
  43. Method: `put`,
  44. Data: struct{ sql string }{sql: `create stream stream1 (id bigint, name string) WITH ( datasource = \"topic/temperature\", FORMAT = \"json\", KEY = \"id\");`},
  45. },
  46. exp: true,
  47. },
  48. {
  49. cmd: command{
  50. Url: `/rules`,
  51. Method: `post`,
  52. Data: struct {
  53. id string
  54. sql string
  55. actions []struct{ log struct{} }
  56. }{
  57. id: `ruler1`,
  58. sql: `SELECT * FROM stream1`,
  59. },
  60. },
  61. exp: true,
  62. },
  63. {
  64. cmd: command{
  65. Url: `/rules`,
  66. Method: `get`,
  67. },
  68. exp: true,
  69. },
  70. {
  71. cmd: command{
  72. Url: `/rules/rule1`,
  73. Method: `get`,
  74. },
  75. exp: true,
  76. },
  77. {
  78. cmd: command{
  79. Url: `/rules/rule2`,
  80. Method: `delete`,
  81. },
  82. exp: true,
  83. },
  84. {
  85. cmd: command{
  86. Url: `/rules/rule1/stop`,
  87. Method: `post`,
  88. },
  89. exp: true,
  90. },
  91. {
  92. cmd: command{
  93. Url: `/rules/rule1/start`,
  94. Method: `post`,
  95. },
  96. exp: true,
  97. },
  98. }
  99. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  100. }))
  101. defer ts.Close()
  102. for _, v := range tests {
  103. ret := v.cmd.call(ts.URL)
  104. if v.exp != ret {
  105. t.Errorf("url:%s method:%s log:%s\n", v.cmd.Url, v.cmd.Method, v.cmd.getLog())
  106. }
  107. }
  108. }