data_server_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2022-2023 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 httpserver
  15. import (
  16. "bytes"
  17. "fmt"
  18. "net/http"
  19. "testing"
  20. "time"
  21. "github.com/lf-edge/ekuiper/internal/testx"
  22. )
  23. var body = []byte(`{
  24. "title": "Post title",
  25. "body": "Post description",
  26. "userId": 1
  27. }`)
  28. func TestEndpoints(t *testing.T) {
  29. testx.InitEnv()
  30. endpoints := []string{
  31. "/ee1", "/eb2", "/ec3",
  32. }
  33. RegisterEndpoint(endpoints[0], "POST", "application/json")
  34. RegisterEndpoint(endpoints[1], "PUT", "application/json")
  35. RegisterEndpoint(endpoints[2], "POST", "application/json")
  36. if server == nil || router == nil {
  37. t.Error("server or router is nil after registering")
  38. return
  39. }
  40. if refCount != 3 {
  41. t.Error("refCount is not 3 after registering")
  42. return
  43. }
  44. UnregisterEndpoint(endpoints[0])
  45. UnregisterEndpoint(endpoints[1])
  46. UnregisterEndpoint(endpoints[2])
  47. if refCount != 0 {
  48. t.Error("refCount is not 0 after unregistering")
  49. return
  50. }
  51. if server != nil || router != nil {
  52. t.Error("server or router is not nil after unregistering")
  53. return
  54. }
  55. urlPrefix := "http://localhost:10081"
  56. client := &http.Client{}
  57. RegisterEndpoint(endpoints[0], "POST", "application/json")
  58. _, _, err := RegisterEndpoint(endpoints[0], "PUT", "application/json")
  59. if err != nil {
  60. t.Error("RegisterEndpoint should not return error for same endpoint")
  61. }
  62. RegisterEndpoint(endpoints[1], "PUT", "application/json")
  63. // wait for http server start
  64. for i := 0; i < 3; i++ {
  65. err = testHttp(client, urlPrefix+endpoints[0], "POST")
  66. if err == nil {
  67. break
  68. }
  69. time.Sleep(time.Millisecond * 500)
  70. }
  71. if err != nil {
  72. t.Error("httptest still fails after 3 times :", err.Error())
  73. }
  74. err = testHttp(client, urlPrefix+endpoints[1], "PUT")
  75. if err != nil {
  76. t.Error(err)
  77. }
  78. RegisterEndpoint(endpoints[2], "POST", "application/json")
  79. err = testHttp(client, urlPrefix+endpoints[2], "POST")
  80. if err != nil {
  81. t.Error(err)
  82. }
  83. }
  84. func testHttp(client *http.Client, url string, method string) error {
  85. r, err := http.NewRequest(method, url, bytes.NewBuffer(body))
  86. if err != nil {
  87. return err
  88. }
  89. resp, err := client.Do(r)
  90. if err != nil {
  91. return err
  92. }
  93. if resp.StatusCode != http.StatusOK {
  94. return fmt.Errorf("status code is not 200 for %s", url)
  95. }
  96. return nil
  97. }