client_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 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 http
  15. import (
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. mockContext "github.com/lf-edge/ekuiper/internal/io/mock/context"
  20. )
  21. func TestHeaderConf(t *testing.T) {
  22. tests := []struct {
  23. name string
  24. data map[string]interface{}
  25. props map[string]interface{}
  26. err error
  27. headers map[string]string
  28. }{
  29. {
  30. name: "template",
  31. data: map[string]interface{}{
  32. "id": 1234,
  33. "temperature": 20,
  34. },
  35. props: map[string]interface{}{
  36. "url": "http://localhost:9090/",
  37. "headers": "{\"custom\":\"{{.id}}\",\"sourceCode\":\"1090\",\"serviceCode\":\"1090109107\" }",
  38. },
  39. headers: map[string]string{
  40. "custom": "1234",
  41. "sourceCode": "1090",
  42. "serviceCode": "1090109107",
  43. },
  44. },
  45. {
  46. name: "wrong template",
  47. data: map[string]interface{}{
  48. "id": 1234,
  49. "temperature": 20,
  50. },
  51. props: map[string]interface{}{
  52. "url": "http://localhost:9090/",
  53. "headers": "{\"custom\":\"{{{.idd}}\",\"sourceCode\":\"1090\",\"serviceCode\":\"1090109107\" }",
  54. },
  55. err: fmt.Errorf("fail to parse the header template {\"custom\":\"{{{.idd}}\",\"sourceCode\":\"1090\",\"serviceCode\":\"1090109107\" }: template: sink:1: unexpected \"{\" in command"),
  56. },
  57. {
  58. name: "wrong parsed template",
  59. data: map[string]interface{}{
  60. "id": 1234,
  61. "temperature": 20,
  62. },
  63. props: map[string]interface{}{
  64. "url": "http://localhost:9090/",
  65. "headers": "{{.id}}",
  66. },
  67. err: fmt.Errorf("parsed header template is not json: 1234"),
  68. },
  69. {
  70. name: "wrong header entry template",
  71. data: map[string]interface{}{
  72. "id": 1234,
  73. "temperature": 20,
  74. },
  75. props: map[string]interface{}{
  76. "url": "http://localhost:9090/",
  77. "headers": map[string]interface{}{
  78. "custom": "{{{.id}}",
  79. },
  80. },
  81. err: fmt.Errorf("fail to parse the header entry custom: template: sink:1: unexpected \"{\" in command"),
  82. },
  83. }
  84. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  85. ctx := mockContext.NewMockContext("none", "httppull_init")
  86. for i, tt := range tests {
  87. t.Run(fmt.Sprintf("Test %d: %s", i, tt.name), func(t *testing.T) {
  88. r := &ClientConf{}
  89. err := r.InitConf("", tt.props)
  90. if err != nil {
  91. t.Errorf("Unexpected error: %v", err)
  92. return
  93. }
  94. headers, err := r.parseHeaders(ctx, tt.data)
  95. if err != nil {
  96. if tt.err == nil {
  97. t.Errorf("Expected error: %v", err)
  98. } else {
  99. if err.Error() != tt.err.Error() {
  100. t.Errorf("Error mismatch\nexp\t%v\ngot\t%v", tt.err, err)
  101. }
  102. }
  103. return
  104. }
  105. if !reflect.DeepEqual(headers, tt.headers) {
  106. t.Errorf("Parsed headers mismatch\nexp\t%+v\ngot\t%+v", tt.headers, headers)
  107. }
  108. })
  109. }
  110. }