global_rule_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 global_rule_test
  18. import (
  19. "net/http"
  20. . "github.com/onsi/ginkgo/v2"
  21. "github.com/apisix/manager-api/test/e2e/base"
  22. )
  23. var _ = Describe("Global Rule", func() {
  24. var _ = DescribeTable("test global rule curd",
  25. func(tc base.HttpTestCase) {
  26. base.RunTestCase(tc)
  27. },
  28. Entry("create global rule", base.HttpTestCase{
  29. Object: base.ManagerApiExpect(),
  30. Method: http.MethodPut,
  31. Path: "/apisix/admin/global_rules/1",
  32. Body: `{
  33. "plugins": {
  34. "response-rewrite": {
  35. "headers": {
  36. "X-VERSION":"1.0"
  37. }
  38. }
  39. }
  40. }`,
  41. Headers: map[string]string{"Authorization": base.GetToken()},
  42. ExpectStatus: http.StatusOK,
  43. ExpectBody: "\"X-VERSION\":\"1.0\"",
  44. }),
  45. Entry("full update global rule", base.HttpTestCase{
  46. Object: base.ManagerApiExpect(),
  47. Method: http.MethodPut,
  48. Path: "/apisix/admin/global_rules/1",
  49. Body: `{
  50. "plugins": {
  51. "response-rewrite": {
  52. "headers": {
  53. "X-TEST":"1.0"
  54. }
  55. }
  56. }
  57. }`,
  58. Headers: map[string]string{"Authorization": base.GetToken()},
  59. ExpectStatus: http.StatusOK,
  60. ExpectBody: "\"X-TEST\":\"1.0\"",
  61. }),
  62. Entry("partial update global rule", base.HttpTestCase{
  63. Object: base.ManagerApiExpect(),
  64. Method: http.MethodPatch,
  65. Path: "/apisix/admin/global_rules/1/plugins",
  66. Body: `{
  67. "response-rewrite": {
  68. "headers": {
  69. "X-VERSION":"1.0"
  70. }
  71. },
  72. "key-auth": {}
  73. }`,
  74. Headers: map[string]string{"Authorization": base.GetToken()},
  75. ExpectStatus: http.StatusOK,
  76. ExpectBody: "\"key-auth\":{}",
  77. }),
  78. Entry("delete global rule", base.HttpTestCase{
  79. Object: base.ManagerApiExpect(),
  80. Method: http.MethodDelete,
  81. Path: "/apisix/admin/global_rules/1",
  82. Headers: map[string]string{"Authorization": base.GetToken()},
  83. ExpectStatus: http.StatusOK,
  84. }),
  85. )
  86. var _ = DescribeTable("test global rule integration",
  87. func(tc base.HttpTestCase) {
  88. base.RunTestCase(tc)
  89. },
  90. Entry("create route", base.HttpTestCase{
  91. Object: base.ManagerApiExpect(),
  92. Method: http.MethodPut,
  93. Path: "/apisix/admin/routes/r1",
  94. Body: `{
  95. "name": "route1",
  96. "uri": "/hello",
  97. "upstream": {
  98. "type": "roundrobin",
  99. "nodes": [{
  100. "host": "` + base.UpstreamIp + `",
  101. "port": 1981,
  102. "weight": 1
  103. }]
  104. }
  105. }`,
  106. Headers: map[string]string{"Authorization": base.GetToken()},
  107. ExpectStatus: http.StatusOK,
  108. }),
  109. Entry("create global rule", base.HttpTestCase{
  110. Object: base.ManagerApiExpect(),
  111. Method: http.MethodPut,
  112. Path: "/apisix/admin/global_rules/1",
  113. Body: `{
  114. "plugins": {
  115. "response-rewrite": {
  116. "headers": {
  117. "X-VERSION":"1.0"
  118. }
  119. },
  120. "uri-blocker": {
  121. "block_rules": ["select.+(from|limit)", "(?:(union(.*?)select))"]
  122. }
  123. }
  124. }`,
  125. Headers: map[string]string{"Authorization": base.GetToken()},
  126. ExpectStatus: http.StatusOK,
  127. ExpectBody: "\"X-VERSION\":\"1.0\"",
  128. }),
  129. Entry("verify route with header", base.HttpTestCase{
  130. Object: base.APISIXExpect(),
  131. Method: http.MethodGet,
  132. Path: "/hello",
  133. ExpectStatus: http.StatusOK,
  134. ExpectBody: "hello world",
  135. ExpectHeaders: map[string]string{"X-VERSION": "1.0"},
  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. }),
  145. Entry("update route with same plugin response-rewrite", base.HttpTestCase{
  146. Object: base.ManagerApiExpect(),
  147. Method: http.MethodPut,
  148. Path: "/apisix/admin/routes/r1",
  149. Body: `{
  150. "name": "route1",
  151. "uri": "/hello",
  152. "plugins": {
  153. "response-rewrite": {
  154. "headers": {
  155. "X-VERSION":"2.0"
  156. }
  157. }
  158. },
  159. "upstream": {
  160. "type": "roundrobin",
  161. "nodes": [{
  162. "host": "` + base.UpstreamIp + `",
  163. "port": 1981,
  164. "weight": 1
  165. }]
  166. }
  167. }`,
  168. Headers: map[string]string{"Authorization": base.GetToken()},
  169. ExpectStatus: http.StatusOK,
  170. ExpectBody: "\"X-VERSION\":\"2.0\"",
  171. }),
  172. Entry("verify route that header should override", base.HttpTestCase{
  173. Object: base.APISIXExpect(),
  174. Method: http.MethodGet,
  175. Path: "/hello",
  176. ExpectStatus: http.StatusOK,
  177. ExpectBody: "hello world",
  178. ExpectHeaders: map[string]string{"X-VERSION": "2.0"},
  179. }),
  180. Entry("delete global rule", base.HttpTestCase{
  181. Object: base.ManagerApiExpect(),
  182. Method: http.MethodDelete,
  183. Path: "/apisix/admin/global_rules/1",
  184. Headers: map[string]string{"Authorization": base.GetToken()},
  185. ExpectStatus: http.StatusOK,
  186. }),
  187. Entry("delete route", base.HttpTestCase{
  188. Object: base.ManagerApiExpect(),
  189. Method: http.MethodDelete,
  190. Path: "/apisix/admin/routes/r1",
  191. Headers: map[string]string{"Authorization": base.GetToken()},
  192. ExpectStatus: http.StatusOK,
  193. }),
  194. )
  195. })