route_with_remote_addr_test.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 _ = Describe("route with valid remote_addr remote_addrs", func() {
  24. DescribeTable("test route with valid remote_addr remote_addrs",
  25. func(tc base.HttpTestCase) {
  26. base.RunTestCase(tc)
  27. },
  28. Entry("add route with valid remote_addr", base.HttpTestCase{
  29. Object: base.ManagerApiExpect(),
  30. Method: http.MethodPut,
  31. Path: "/apisix/admin/routes/r1",
  32. Body: `{
  33. "name": "route1",
  34. "uri": "/hello",
  35. "remote_addr": "172.16.238.1",
  36. "upstream": {
  37. "type": "roundrobin",
  38. "nodes": {
  39. "` + base.UpstreamIp + `:1980": 1
  40. }
  41. }
  42. }`,
  43. Headers: map[string]string{"Authorization": base.GetToken()},
  44. ExpectStatus: http.StatusOK,
  45. }),
  46. Entry("verify route", base.HttpTestCase{
  47. Object: base.APISIXExpect(),
  48. Method: http.MethodGet,
  49. Path: "/hello",
  50. Headers: map[string]string{"Authorization": base.GetToken()},
  51. ExpectStatus: http.StatusOK,
  52. ExpectBody: "hello world",
  53. Sleep: base.SleepTime,
  54. }),
  55. Entry("update route with valid remote_addr (CIDR)", base.HttpTestCase{
  56. Object: base.ManagerApiExpect(),
  57. Method: http.MethodPut,
  58. Path: "/apisix/admin/routes/r1",
  59. Body: `{
  60. "name": "route1",
  61. "uri": "/hello",
  62. "remote_addr": "172.16.238.1/24",
  63. "upstream": {
  64. "type": "roundrobin",
  65. "nodes": {
  66. "` + base.UpstreamIp + `:1980": 1
  67. }
  68. }
  69. }`,
  70. Headers: map[string]string{"Authorization": base.GetToken()},
  71. ExpectStatus: http.StatusOK,
  72. }),
  73. Entry("verify route", base.HttpTestCase{
  74. Object: base.APISIXExpect(),
  75. Method: http.MethodGet,
  76. Path: "/hello",
  77. Headers: map[string]string{"Authorization": base.GetToken()},
  78. ExpectStatus: http.StatusOK,
  79. ExpectBody: "hello world",
  80. Sleep: base.SleepTime,
  81. }),
  82. Entry("update route with valid remote_addrs", base.HttpTestCase{
  83. Object: base.ManagerApiExpect(),
  84. Method: http.MethodPut,
  85. Path: "/apisix/admin/routes/r1",
  86. Body: `{
  87. "name": "route1",
  88. "uri": "/hello",
  89. "remote_addrs": ["172.16.238.1","192.168.0.2/24"],
  90. "upstream": {
  91. "type": "roundrobin",
  92. "nodes": {
  93. "` + base.UpstreamIp + `:1980": 1
  94. }
  95. }
  96. }`,
  97. Headers: map[string]string{"Authorization": base.GetToken()},
  98. ExpectStatus: http.StatusOK,
  99. }),
  100. Entry("verify route", base.HttpTestCase{
  101. Object: base.APISIXExpect(),
  102. Method: http.MethodGet,
  103. Path: "/hello",
  104. Headers: map[string]string{"Authorization": base.GetToken()},
  105. ExpectStatus: http.StatusOK,
  106. ExpectBody: "hello world",
  107. Sleep: base.SleepTime,
  108. }),
  109. Entry("update remote_addr to not be hit", base.HttpTestCase{
  110. Object: base.ManagerApiExpect(),
  111. Method: http.MethodPut,
  112. Path: "/apisix/admin/routes/r1",
  113. Body: `{
  114. "name": "route1",
  115. "uri": "/hello",
  116. "remote_addr": "10.10.10.10",
  117. "upstream": {
  118. "type": "roundrobin",
  119. "nodes": {
  120. "` + base.UpstreamIp + `:1980": 1
  121. }
  122. }
  123. }`,
  124. Headers: map[string]string{"Authorization": base.GetToken()},
  125. ExpectStatus: http.StatusOK,
  126. }),
  127. Entry("verify route not found", base.HttpTestCase{
  128. Object: base.APISIXExpect(),
  129. Method: http.MethodGet,
  130. Path: "/hello",
  131. Headers: map[string]string{"Authorization": base.GetToken()},
  132. ExpectStatus: http.StatusNotFound,
  133. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  134. Sleep: base.SleepTime,
  135. }),
  136. Entry("update remote_addrs to not be hit", base.HttpTestCase{
  137. Object: base.ManagerApiExpect(),
  138. Method: http.MethodPut,
  139. Path: "/apisix/admin/routes/r1",
  140. Body: `{
  141. "name": "route1",
  142. "uri": "/hello",
  143. "remote_addrs": ["10.10.10.10","11.11.11.1/24"],
  144. "upstream": {
  145. "type": "roundrobin",
  146. "nodes": {
  147. "` + base.UpstreamIp + `:1980": 1
  148. }
  149. }
  150. }`,
  151. Headers: map[string]string{"Authorization": base.GetToken()},
  152. ExpectStatus: http.StatusOK,
  153. }),
  154. Entry("verify route not found", base.HttpTestCase{
  155. Object: base.APISIXExpect(),
  156. Method: http.MethodGet,
  157. Path: "/hello",
  158. Headers: map[string]string{"Authorization": base.GetToken()},
  159. ExpectStatus: http.StatusNotFound,
  160. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  161. Sleep: base.SleepTime,
  162. }),
  163. Entry("delete route", base.HttpTestCase{
  164. Object: base.ManagerApiExpect(),
  165. Method: http.MethodDelete,
  166. Path: "/apisix/admin/routes/r1",
  167. Headers: map[string]string{"Authorization": base.GetToken()},
  168. ExpectStatus: http.StatusOK,
  169. Sleep: base.SleepTime,
  170. }),
  171. Entry("verify it again after deleting the route", base.HttpTestCase{
  172. Object: base.APISIXExpect(),
  173. Method: http.MethodGet,
  174. Path: "/hello",
  175. Headers: map[string]string{"Authorization": base.GetToken()},
  176. ExpectStatus: http.StatusNotFound,
  177. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  178. Sleep: base.SleepTime,
  179. }),
  180. )
  181. })
  182. var _ = Describe("route with invalid remote_addr", func() {
  183. DescribeTable("route with remote_addr",
  184. func(tc base.HttpTestCase) {
  185. base.RunTestCase(tc)
  186. },
  187. Entry("config route with invalid remote_addr", base.HttpTestCase{
  188. Object: base.ManagerApiExpect(),
  189. Method: http.MethodPut,
  190. Path: "/apisix/admin/routes/r1",
  191. Body: `{
  192. "name": "route1",
  193. "uri": "/hello",
  194. "remote_addr": "127.0.0.",
  195. "upstream": {
  196. "type": "roundrobin",
  197. "nodes": [{
  198. "host": "` + base.UpstreamIp + `",
  199. "port": 1980,
  200. "weight": 1
  201. }]
  202. }
  203. }`,
  204. Headers: map[string]string{"Authorization": base.GetToken()},
  205. ExpectStatus: http.StatusBadRequest,
  206. ExpectBody: "{\"code\":10000,\"message\":\"schema validate failed: remote_addr: Must validate at least one schema (anyOf)\\nremote_addr: Does not match format 'ipv4'\"}",
  207. }),
  208. Entry("verify route", base.HttpTestCase{
  209. Object: base.APISIXExpect(),
  210. Method: http.MethodGet,
  211. Path: "/hello",
  212. Headers: map[string]string{"Authorization": base.GetToken()},
  213. ExpectStatus: http.StatusNotFound,
  214. Sleep: base.SleepTime,
  215. }),
  216. Entry("config route with invalid remote_addr", base.HttpTestCase{
  217. Object: base.ManagerApiExpect(),
  218. Method: http.MethodPut,
  219. Path: "/apisix/admin/routes/r1",
  220. Body: `{
  221. "name": "route1",
  222. "uri": "/hello",
  223. "remote_addr": "127.0.0.aa",
  224. "upstream": {
  225. "type": "roundrobin",
  226. "nodes": [{
  227. "host": "` + base.UpstreamIp + `",
  228. "port": 1980,
  229. "weight": 1
  230. }]
  231. }
  232. }`,
  233. Headers: map[string]string{"Authorization": base.GetToken()},
  234. ExpectStatus: http.StatusBadRequest,
  235. ExpectBody: "{\"code\":10000,\"message\":\"schema validate failed: remote_addr: Must validate at least one schema (anyOf)\\nremote_addr: Does not match format 'ipv4'\"}",
  236. }),
  237. Entry("verify route", base.HttpTestCase{
  238. Object: base.APISIXExpect(),
  239. Method: http.MethodGet,
  240. Path: "/hello",
  241. Headers: map[string]string{"Authorization": base.GetToken()},
  242. ExpectStatus: http.StatusNotFound,
  243. Sleep: base.SleepTime,
  244. }),
  245. Entry("config route with invalid remote_addrs", base.HttpTestCase{
  246. Object: base.ManagerApiExpect(),
  247. Method: http.MethodPut,
  248. Path: "/apisix/admin/routes/r1",
  249. Body: `{
  250. "name": "route1",
  251. "uri": "/hello",
  252. "remote_addrs": ["127.0.0.1","192.168.0."],
  253. "upstream": {
  254. "type": "roundrobin",
  255. "nodes": [{
  256. "host": "` + base.UpstreamIp + `",
  257. "port": 1980,
  258. "weight": 1
  259. }]
  260. }
  261. }`,
  262. Headers: map[string]string{"Authorization": base.GetToken()},
  263. ExpectStatus: http.StatusBadRequest,
  264. ExpectBody: "{\"code\":10000,\"message\":\"schema validate failed: remote_addrs.1: Must validate at least one schema (anyOf)\\nremote_addrs.1: Does not match format 'ipv4'\"}",
  265. }),
  266. Entry("verify route", base.HttpTestCase{
  267. Object: base.APISIXExpect(),
  268. Method: http.MethodGet,
  269. Path: "/hello",
  270. Headers: map[string]string{"Authorization": base.GetToken()},
  271. ExpectStatus: http.StatusNotFound,
  272. Sleep: base.SleepTime,
  273. }),
  274. )
  275. })