plugin_config_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package plugin_config_test
  18. import (
  19. "net/http"
  20. . "github.com/onsi/ginkgo/v2"
  21. "github.com/apisix/manager-api/test/e2e/base"
  22. )
  23. var _ = DescribeTable("Plugin Config",
  24. func(tc base.HttpTestCase) {
  25. base.RunTestCase(tc)
  26. },
  27. Entry("make sure the route doesn't exist", base.HttpTestCase{
  28. Object: base.APISIXExpect(),
  29. Method: http.MethodGet,
  30. Path: "/hello",
  31. ExpectStatus: http.StatusNotFound,
  32. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  33. }),
  34. Entry("create plugin config", base.HttpTestCase{
  35. Object: base.ManagerApiExpect(),
  36. Path: "/apisix/admin/plugin_configs/1",
  37. Method: http.MethodPut,
  38. Body: `{
  39. "plugins": {
  40. "response-rewrite": {
  41. "headers": {
  42. "X-VERSION":"1.0"
  43. }
  44. },
  45. "uri-blocker": {
  46. "block_rules": ["select.+(from|limit)", "(?:(union(.*?)select))"]
  47. }
  48. },
  49. "labels": {
  50. "version": "v1",
  51. "build": "16"
  52. }
  53. }`,
  54. Headers: map[string]string{"Authorization": base.GetToken()},
  55. ExpectStatus: http.StatusOK,
  56. }),
  57. Entry("create plugin config by Post", base.HttpTestCase{
  58. Object: base.ManagerApiExpect(),
  59. Path: "/apisix/admin/plugin_configs",
  60. Method: http.MethodPost,
  61. Body: `{
  62. "id": "2",
  63. "plugins": {
  64. "response-rewrite": {
  65. "headers": {
  66. "X-VERSION":"22.0"
  67. }
  68. }
  69. },
  70. "labels": {
  71. "version": "v2",
  72. "build": "17",
  73. "extra": "test"
  74. }
  75. }`,
  76. Headers: map[string]string{"Authorization": base.GetToken()},
  77. ExpectStatus: http.StatusOK,
  78. }),
  79. Entry("get plugin config", base.HttpTestCase{
  80. Object: base.ManagerApiExpect(),
  81. Path: "/apisix/admin/plugin_configs/1",
  82. Method: http.MethodGet,
  83. Headers: map[string]string{"Authorization": base.GetToken()},
  84. ExpectStatus: http.StatusOK,
  85. ExpectBody: `"plugins":{"response-rewrite":{"headers":{"X-VERSION":"1.0"}},"uri-blocker":{"block_rules":["select.+(from|limit)","(?:(union(.*?)select))"]}}`,
  86. Sleep: base.SleepTime,
  87. }),
  88. Entry("search plugin_config list by label ", base.HttpTestCase{
  89. Object: base.ManagerApiExpect(),
  90. Path: "/apisix/admin/plugin_configs",
  91. Query: "label=build:16",
  92. Method: http.MethodGet,
  93. Headers: map[string]string{"Authorization": base.GetToken()},
  94. ExpectStatus: http.StatusOK,
  95. ExpectBody: `"labels":{"build":"16","version":"v1"}`,
  96. Sleep: base.SleepTime,
  97. }),
  98. Entry("search plugin_config list by label (only key)", base.HttpTestCase{
  99. Object: base.ManagerApiExpect(),
  100. Path: "/apisix/admin/plugin_configs",
  101. Query: "label=extra",
  102. Method: http.MethodGet,
  103. Headers: map[string]string{"Authorization": base.GetToken()},
  104. ExpectStatus: http.StatusOK,
  105. ExpectBody: `"labels":{"build":"17","extra":"test","version":"v2"}`,
  106. Sleep: base.SleepTime,
  107. }),
  108. Entry("create route with the plugin config created before", base.HttpTestCase{
  109. Object: base.ManagerApiExpect(),
  110. Method: http.MethodPut,
  111. Path: "/apisix/admin/routes/r1",
  112. Body: `{
  113. "name": "route1",
  114. "uri": "/hello",
  115. "plugin_config_id": "1",
  116. "upstream": {
  117. "type": "roundrobin",
  118. "nodes": [{
  119. "host": "` + base.UpstreamIp + `",
  120. "port": 1981,
  121. "weight": 1
  122. }]
  123. }
  124. }`,
  125. Headers: map[string]string{"Authorization": base.GetToken()},
  126. ExpectStatus: http.StatusOK,
  127. }),
  128. Entry("verify route with header", base.HttpTestCase{
  129. Object: base.APISIXExpect(),
  130. Method: http.MethodGet,
  131. Path: "/hello",
  132. ExpectStatus: http.StatusOK,
  133. ExpectBody: "hello world",
  134. ExpectHeaders: map[string]string{"X-VERSION": "1.0"},
  135. Sleep: base.SleepTime,
  136. }),
  137. Entry("verify route that should be blocked", base.HttpTestCase{
  138. Object: base.APISIXExpect(),
  139. Method: http.MethodGet,
  140. Path: "/hello",
  141. Query: "name=%3Bselect%20from%20sys",
  142. ExpectStatus: http.StatusForbidden,
  143. ExpectHeaders: map[string]string{"X-VERSION": "1.0"},
  144. Sleep: base.SleepTime,
  145. }),
  146. Entry("update plugin config by patch", base.HttpTestCase{
  147. Object: base.ManagerApiExpect(),
  148. Path: "/apisix/admin/plugin_configs/1",
  149. Method: http.MethodPatch,
  150. Body: `{
  151. "plugins": {
  152. "response-rewrite": {
  153. "headers": {
  154. "X-VERSION":"2.0"
  155. }
  156. },
  157. "uri-blocker": {
  158. "block_rules": ["none"]
  159. }
  160. }
  161. }`,
  162. Headers: map[string]string{"Authorization": base.GetToken()},
  163. ExpectStatus: http.StatusOK,
  164. }),
  165. Entry("verify patch update", base.HttpTestCase{
  166. Object: base.APISIXExpect(),
  167. Method: http.MethodGet,
  168. Path: "/hello",
  169. ExpectStatus: http.StatusOK,
  170. ExpectBody: "hello world",
  171. ExpectHeaders: map[string]string{"X-VERSION": "2.0"},
  172. Sleep: base.SleepTime,
  173. }),
  174. Entry("verify patch update(should not block)", base.HttpTestCase{
  175. Object: base.APISIXExpect(),
  176. Method: http.MethodGet,
  177. Path: "/hello",
  178. Query: "name=%3Bselect%20from%20sys",
  179. ExpectStatus: http.StatusOK,
  180. ExpectBody: "hello world",
  181. ExpectHeaders: map[string]string{"X-VERSION": "2.0"},
  182. }),
  183. Entry("update plugin config by sub path patch", base.HttpTestCase{
  184. Object: base.ManagerApiExpect(),
  185. Path: "/apisix/admin/plugin_configs/1/plugins",
  186. Method: http.MethodPatch,
  187. Body: `{
  188. "response-rewrite": {
  189. "headers": {
  190. "X-VERSION":"3.0"
  191. }
  192. }
  193. }`,
  194. Headers: map[string]string{"Authorization": base.GetToken()},
  195. ExpectStatus: http.StatusOK,
  196. }),
  197. Entry("verify patch (sub path)", base.HttpTestCase{
  198. Object: base.APISIXExpect(),
  199. Method: http.MethodGet,
  200. Path: "/hello",
  201. ExpectStatus: http.StatusOK,
  202. ExpectBody: "hello world",
  203. ExpectHeaders: map[string]string{"X-VERSION": "3.0"},
  204. Sleep: base.SleepTime,
  205. }),
  206. Entry("delete route", base.HttpTestCase{
  207. Object: base.ManagerApiExpect(),
  208. Method: http.MethodDelete,
  209. Path: "/apisix/admin/routes/r1",
  210. Headers: map[string]string{"Authorization": base.GetToken()},
  211. ExpectStatus: http.StatusOK,
  212. }),
  213. Entry("delete plugin config", base.HttpTestCase{
  214. Object: base.ManagerApiExpect(),
  215. Method: http.MethodDelete,
  216. Path: "/apisix/admin/plugin_configs/1",
  217. Headers: map[string]string{"Authorization": base.GetToken()},
  218. ExpectStatus: http.StatusOK,
  219. Sleep: base.SleepTime,
  220. }),
  221. Entry("make sure the plugin config has been deleted", base.HttpTestCase{
  222. Object: base.ManagerApiExpect(),
  223. Method: http.MethodGet,
  224. Path: "/apisix/admin/plugin_configs/1",
  225. Headers: map[string]string{"Authorization": base.GetToken()},
  226. ExpectStatus: http.StatusNotFound,
  227. ExpectBody: `{"code":10001,"message":"data not found"`,
  228. Sleep: base.SleepTime,
  229. }),
  230. Entry("make sure the route has been deleted", base.HttpTestCase{
  231. Object: base.APISIXExpect(),
  232. Method: http.MethodGet,
  233. Path: "/hello",
  234. ExpectStatus: http.StatusNotFound,
  235. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  236. Sleep: base.SleepTime,
  237. }),
  238. )