schemaHttp_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 service
  15. import (
  16. "net/http"
  17. "reflect"
  18. "testing"
  19. "github.com/lf-edge/ekuiper/internal/testx"
  20. )
  21. func TestBookstoreConvertHttpMapping(t *testing.T) {
  22. tests := []struct {
  23. method string
  24. params []interface{}
  25. result *httpConnMeta
  26. err string
  27. }{
  28. { // 0 create book
  29. method: "CreateBook",
  30. params: []interface{}{
  31. 1984,
  32. map[string]interface{}{
  33. "id": 20210519,
  34. "author": "Conan Doyle",
  35. "title": "Sherlock Holmes",
  36. },
  37. },
  38. // int64 will be marshaled to string!
  39. result: &httpConnMeta{
  40. Method: http.MethodPost,
  41. Uri: "/v1/shelves/1984/books",
  42. Body: []byte(`{"id":"20210519","author":"Conan Doyle","title":"Sherlock Holmes"}`),
  43. },
  44. }, { // 2 delete book
  45. method: "DeleteBook",
  46. params: []interface{}{
  47. 1984,
  48. 20210519,
  49. },
  50. result: &httpConnMeta{
  51. Method: http.MethodDelete,
  52. Uri: "/v1/shelves/1984/books/20210519",
  53. },
  54. }, { // 3 list shelves
  55. method: "ListShelves",
  56. params: []interface{}{},
  57. result: &httpConnMeta{
  58. Method: http.MethodGet,
  59. Uri: "/v1/shelves",
  60. },
  61. },
  62. }
  63. d, err := parse(PROTOBUFF, "http_bookstore.proto")
  64. if err != nil {
  65. panic(err)
  66. }
  67. for i, tt := range tests {
  68. r, err := d.(httpMapping).ConvertHttpMapping(tt.method, tt.params)
  69. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  70. t.Errorf("%d : interface error mismatch:\n exp=%s\n got=%s\n\n", i, tt.err, err)
  71. } else if tt.err == "" && !reflect.DeepEqual(tt.result, r) {
  72. t.Errorf("%d \n\ninterface result mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.result, r)
  73. }
  74. }
  75. }
  76. func TestMessagingConvertHttpMapping(t *testing.T) {
  77. tests := []struct {
  78. method string
  79. params []interface{}
  80. result *httpConnMeta
  81. err string
  82. }{
  83. { // 0 get message
  84. method: "GetMessage",
  85. params: []interface{}{
  86. "messages/123456",
  87. },
  88. // int64 will be marshaled to string!
  89. result: &httpConnMeta{
  90. Method: http.MethodGet,
  91. Uri: "/v1/messages/123456",
  92. },
  93. }, { // 1 get message prefix error
  94. method: "GetMessage",
  95. params: []interface{}{
  96. "message/123456",
  97. },
  98. err: "invalid field name(message/123456) as http option, must have prefix messages/",
  99. }, { // 2 search messages
  100. method: "SearchMessage",
  101. params: []interface{}{
  102. "123456",
  103. 2,
  104. map[string]interface{}{
  105. "subfield": "foo",
  106. },
  107. },
  108. result: &httpConnMeta{
  109. Method: http.MethodGet,
  110. Uri: "/v1/messages/filter/123456?revision=2&sub.subfield=foo",
  111. },
  112. }, { // 3 update message
  113. method: "UpdateMessage",
  114. params: []interface{}{
  115. "123456",
  116. map[string]interface{}{
  117. "text": "Hi!",
  118. },
  119. },
  120. result: &httpConnMeta{
  121. Method: http.MethodPut,
  122. Uri: "/v1/messages/123456",
  123. Body: []byte(`{"text":"Hi!"}`),
  124. },
  125. }, { // 4 patch message
  126. method: "PatchMessage",
  127. params: []interface{}{
  128. "123456",
  129. "Hi!",
  130. },
  131. result: &httpConnMeta{
  132. Method: http.MethodPatch,
  133. Uri: "/v1/messages/123456",
  134. Body: []byte(`{"text":"Hi!"}`),
  135. },
  136. },
  137. }
  138. d, err := parse(PROTOBUFF, "http_messaging.proto")
  139. if err != nil {
  140. panic(err)
  141. }
  142. for i, tt := range tests {
  143. r, err := d.(httpMapping).ConvertHttpMapping(tt.method, tt.params)
  144. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  145. t.Errorf("%d : interface error mismatch:\n exp=%s\n got=%s\n\n", i, tt.err, err)
  146. } else if tt.err == "" && !reflect.DeepEqual(tt.result, r) {
  147. t.Errorf("%d \n\ninterface result mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.result, r)
  148. }
  149. }
  150. }