route_with_plugin_uri_blocker_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 route_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("route with plugin uri blocker",
  24. func(tc base.HttpTestCase) {
  25. base.RunTestCase(tc)
  26. },
  27. Entry("make sure the route is not created", base.HttpTestCase{
  28. Object: base.APISIXExpect(),
  29. Method: http.MethodGet,
  30. Path: "/*",
  31. ExpectStatus: http.StatusNotFound,
  32. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  33. }),
  34. Entry("create route1", base.HttpTestCase{
  35. Object: base.ManagerApiExpect(),
  36. Method: http.MethodPut,
  37. Path: "/apisix/admin/routes/r1",
  38. Body: `{
  39. "name": "route1",
  40. "uri": "/*",
  41. "upstream": {
  42. "type": "roundrobin",
  43. "nodes": {
  44. "` + base.UpstreamIp + `:1982": 1
  45. }
  46. }
  47. }`,
  48. Headers: map[string]string{"Authorization": base.GetToken()},
  49. ExpectStatus: http.StatusOK,
  50. }),
  51. Entry("make sure the plugin uri blocker is not worked", base.HttpTestCase{
  52. Object: base.APISIXExpect(),
  53. Method: http.MethodGet,
  54. Path: "/root.exe",
  55. ExpectStatus: http.StatusNotFound,
  56. }),
  57. Entry("update route with uri blocker", base.HttpTestCase{
  58. Object: base.ManagerApiExpect(),
  59. Method: http.MethodPut,
  60. Path: "/apisix/admin/routes/r1",
  61. Body: `{
  62. "name": "route1",
  63. "uri": "/*",
  64. "plugins": {
  65. "uri-blocker": {
  66. "block_rules": ["hello"]
  67. }
  68. },
  69. "upstream": {
  70. "type": "roundrobin",
  71. "nodes": {
  72. "` + base.UpstreamIp + `:1980": 1
  73. }
  74. }
  75. }`,
  76. Headers: map[string]string{"Authorization": base.GetToken()},
  77. ExpectStatus: http.StatusOK,
  78. }),
  79. Entry("verify route that block uri", base.HttpTestCase{
  80. Object: base.APISIXExpect(),
  81. Method: http.MethodGet,
  82. Path: "/hello",
  83. ExpectStatus: http.StatusForbidden,
  84. Sleep: base.SleepTime,
  85. }),
  86. Entry("update route with uri blocker", base.HttpTestCase{
  87. Object: base.ManagerApiExpect(),
  88. Method: http.MethodPut,
  89. Path: "/apisix/admin/routes/r1",
  90. Body: `{
  91. "name": "route1",
  92. "uri": "/*",
  93. "plugins": {
  94. "uri-blocker": {
  95. "block_rules": ["robots.txt"]
  96. }
  97. },
  98. "upstream": {
  99. "type": "roundrobin",
  100. "nodes": {
  101. "` + base.UpstreamIp + `:1980": 1
  102. }
  103. }
  104. }`,
  105. Headers: map[string]string{"Authorization": base.GetToken()},
  106. ExpectStatus: http.StatusOK,
  107. }),
  108. Entry("verify route that old block uri rule", base.HttpTestCase{
  109. Object: base.APISIXExpect(),
  110. Method: http.MethodGet,
  111. Path: "/hello",
  112. ExpectStatus: http.StatusOK,
  113. Sleep: base.SleepTime,
  114. }),
  115. Entry("verify route that block uri", base.HttpTestCase{
  116. Object: base.APISIXExpect(),
  117. Method: http.MethodGet,
  118. Path: "/robots.txt",
  119. ExpectStatus: http.StatusForbidden,
  120. Sleep: base.SleepTime,
  121. }),
  122. Entry("delete route", base.HttpTestCase{
  123. Object: base.ManagerApiExpect(),
  124. Method: http.MethodDelete,
  125. Path: "/apisix/admin/routes/r1",
  126. Headers: map[string]string{"Authorization": base.GetToken()},
  127. ExpectStatus: http.StatusOK,
  128. Sleep: base.SleepTime,
  129. }),
  130. Entry("make sure the route deleted", base.HttpTestCase{
  131. Object: base.APISIXExpect(),
  132. Method: http.MethodGet,
  133. Path: "/hello",
  134. ExpectStatus: http.StatusNotFound,
  135. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  136. Sleep: base.SleepTime,
  137. }),
  138. )