data_server_test.go 2.6 KB

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