route_with_methods_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 methods", func() {
  24. DescribeTable("test route with methods",
  25. func(tc base.HttpTestCase) {
  26. base.RunTestCase(tc)
  27. },
  28. Entry("add route with invalid method", 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. "methods": ["TEST"],
  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.StatusBadRequest,
  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.StatusNotFound,
  52. Sleep: base.SleepTime,
  53. }),
  54. Entry("add route with valid method", base.HttpTestCase{
  55. Object: base.ManagerApiExpect(),
  56. Method: http.MethodPut,
  57. Path: "/apisix/admin/routes/r1",
  58. Body: `{
  59. "name": "route1",
  60. "uri": "/hello",
  61. "methods": ["GET"],
  62. "upstream": {
  63. "type": "roundrobin",
  64. "nodes": {
  65. "` + base.UpstreamIp + `:1980": 1
  66. }
  67. }
  68. }`,
  69. Headers: map[string]string{"Authorization": base.GetToken()},
  70. ExpectStatus: http.StatusOK,
  71. }),
  72. Entry("verify route", base.HttpTestCase{
  73. Object: base.APISIXExpect(),
  74. Method: http.MethodGet,
  75. Path: "/hello",
  76. Headers: map[string]string{"Authorization": base.GetToken()},
  77. ExpectStatus: http.StatusOK,
  78. ExpectBody: "hello world",
  79. Sleep: base.SleepTime,
  80. }),
  81. Entry("update same route path", base.HttpTestCase{
  82. Object: base.ManagerApiExpect(),
  83. Method: http.MethodPut,
  84. Path: "/apisix/admin/routes/r1",
  85. Body: `{
  86. "name": "route1",
  87. "uri": "/hello_",
  88. "methods": ["GET"],
  89. "upstream": {
  90. "type": "roundrobin",
  91. "nodes": {
  92. "` + base.UpstreamIp + `:1980": 1
  93. }
  94. }
  95. }`,
  96. Headers: map[string]string{"Authorization": base.GetToken()},
  97. ExpectStatus: http.StatusOK,
  98. }),
  99. Entry("verify old route updated", base.HttpTestCase{
  100. Object: base.APISIXExpect(),
  101. Method: http.MethodGet,
  102. Path: "/hello",
  103. Headers: map[string]string{"Authorization": base.GetToken()},
  104. ExpectStatus: http.StatusNotFound,
  105. Sleep: base.SleepTime,
  106. }),
  107. Entry("verify new update applied", base.HttpTestCase{
  108. Object: base.APISIXExpect(),
  109. Method: http.MethodGet,
  110. Path: "/hello_",
  111. Headers: map[string]string{"Authorization": base.GetToken()},
  112. ExpectStatus: http.StatusOK,
  113. ExpectBody: "hello world",
  114. Sleep: base.SleepTime,
  115. }),
  116. Entry("delete route", base.HttpTestCase{
  117. Object: base.ManagerApiExpect(),
  118. Method: http.MethodDelete,
  119. Path: "/apisix/admin/routes/r1",
  120. Headers: map[string]string{"Authorization": base.GetToken()},
  121. ExpectStatus: http.StatusOK,
  122. }),
  123. Entry("add route with valid methods", base.HttpTestCase{
  124. Object: base.ManagerApiExpect(),
  125. Method: http.MethodPut,
  126. Path: "/apisix/admin/routes/r1",
  127. Body: `{
  128. "name": "route1",
  129. "uri": "/hello",
  130. "methods": ["GET", "POST", "PUT", "DELETE", "PATCH"],
  131. "upstream": {
  132. "type": "roundrobin",
  133. "nodes": {
  134. "` + base.UpstreamIp + `:1980": 1
  135. }
  136. }
  137. }`,
  138. Headers: map[string]string{"Authorization": base.GetToken()},
  139. ExpectStatus: http.StatusOK,
  140. }),
  141. Entry("verify route by post", base.HttpTestCase{
  142. Object: base.APISIXExpect(),
  143. Method: http.MethodPost,
  144. Path: "/hello",
  145. Body: `test=test`,
  146. Headers: map[string]string{"Authorization": base.GetToken()},
  147. ExpectStatus: http.StatusOK,
  148. ExpectBody: "hello world",
  149. Sleep: base.SleepTime,
  150. }),
  151. Entry("verify route by put", base.HttpTestCase{
  152. Object: base.APISIXExpect(),
  153. Method: http.MethodPut,
  154. Path: "/hello",
  155. Body: `test=test`,
  156. Headers: map[string]string{"Authorization": base.GetToken()},
  157. ExpectStatus: http.StatusOK,
  158. ExpectBody: "hello world",
  159. Sleep: base.SleepTime,
  160. }),
  161. Entry("verify route by get", base.HttpTestCase{
  162. Object: base.APISIXExpect(),
  163. Method: http.MethodGet,
  164. Path: "/hello",
  165. Headers: map[string]string{"Authorization": base.GetToken()},
  166. ExpectStatus: http.StatusOK,
  167. ExpectBody: "hello world",
  168. Sleep: base.SleepTime,
  169. }),
  170. Entry("verify route by delete", base.HttpTestCase{
  171. Object: base.APISIXExpect(),
  172. Method: http.MethodDelete,
  173. Path: "/hello",
  174. Headers: map[string]string{"Authorization": base.GetToken()},
  175. ExpectStatus: http.StatusOK,
  176. ExpectBody: "hello world",
  177. Sleep: base.SleepTime,
  178. }),
  179. Entry("verify route by patch", base.HttpTestCase{
  180. Object: base.APISIXExpect(),
  181. Method: http.MethodPatch,
  182. Path: "/hello",
  183. Body: `test=test`,
  184. Headers: map[string]string{"Authorization": base.GetToken()},
  185. ExpectStatus: http.StatusOK,
  186. ExpectBody: "hello world",
  187. Sleep: base.SleepTime,
  188. }),
  189. Entry("update route methods to GET only", base.HttpTestCase{
  190. Object: base.ManagerApiExpect(),
  191. Method: http.MethodPut,
  192. Path: "/apisix/admin/routes/r1",
  193. Body: `{
  194. "name": "route1",
  195. "uri": "/hello",
  196. "methods": ["GET"],
  197. "upstream": {
  198. "type": "roundrobin",
  199. "nodes": {
  200. "` + base.UpstreamIp + `:1980": 1
  201. }
  202. }
  203. }`,
  204. Headers: map[string]string{"Authorization": base.GetToken()},
  205. ExpectStatus: http.StatusOK,
  206. }),
  207. Entry("verify post method isn't working now", base.HttpTestCase{
  208. Object: base.APISIXExpect(),
  209. Method: http.MethodPost,
  210. Path: "/hello",
  211. Headers: map[string]string{"Authorization": base.GetToken()},
  212. ExpectStatus: http.StatusNotFound,
  213. Sleep: base.SleepTime,
  214. }),
  215. Entry("verify PUT method isn't working now", base.HttpTestCase{
  216. Object: base.APISIXExpect(),
  217. Method: http.MethodPut,
  218. Path: "/hello",
  219. Headers: map[string]string{"Authorization": base.GetToken()},
  220. ExpectStatus: http.StatusNotFound,
  221. Sleep: base.SleepTime,
  222. }),
  223. Entry("verify route by GET only", base.HttpTestCase{
  224. Object: base.APISIXExpect(),
  225. Method: http.MethodGet,
  226. Path: "/hello",
  227. Headers: map[string]string{"Authorization": base.GetToken()},
  228. ExpectStatus: http.StatusOK,
  229. ExpectBody: "hello world",
  230. Sleep: base.SleepTime,
  231. }),
  232. Entry("delete route", base.HttpTestCase{
  233. Object: base.ManagerApiExpect(),
  234. Method: http.MethodDelete,
  235. Path: "/apisix/admin/routes/r1",
  236. Headers: map[string]string{"Authorization": base.GetToken()},
  237. ExpectStatus: http.StatusOK,
  238. }),
  239. Entry("add route with lower case methods", base.HttpTestCase{
  240. Object: base.ManagerApiExpect(),
  241. Method: http.MethodPut,
  242. Path: "/apisix/admin/routes/r1",
  243. Body: `{
  244. "name": "route1",
  245. "uri": "/hello",
  246. "methods": ["GET", "post"],
  247. "upstream": {
  248. "type": "roundrobin",
  249. "nodes": {
  250. "` + base.UpstreamIp + `:1980": 1
  251. }
  252. }
  253. }`,
  254. Headers: map[string]string{"Authorization": base.GetToken()},
  255. ExpectStatus: http.StatusBadRequest,
  256. }),
  257. Entry("verify route", base.HttpTestCase{
  258. Object: base.APISIXExpect(),
  259. Method: http.MethodGet,
  260. Path: "/hello",
  261. Headers: map[string]string{"Authorization": base.GetToken()},
  262. ExpectStatus: http.StatusNotFound,
  263. Sleep: base.SleepTime,
  264. }),
  265. Entry("add route with methods GET", base.HttpTestCase{
  266. Object: base.ManagerApiExpect(),
  267. Method: http.MethodPut,
  268. Path: "/apisix/admin/routes/r1",
  269. Body: `{
  270. "name": "route1",
  271. "uri": "/hello",
  272. "methods": ["GET"],
  273. "upstream": {
  274. "type": "roundrobin",
  275. "nodes": {
  276. "` + base.UpstreamIp + `:1980": 1
  277. }
  278. }
  279. }`,
  280. Headers: map[string]string{"Authorization": base.GetToken()},
  281. ExpectStatus: http.StatusOK,
  282. }),
  283. Entry("verify route by get", base.HttpTestCase{
  284. Object: base.APISIXExpect(),
  285. Method: http.MethodGet,
  286. Path: "/hello",
  287. Headers: map[string]string{"Authorization": base.GetToken()},
  288. ExpectStatus: http.StatusOK,
  289. ExpectBody: "hello world",
  290. Sleep: base.SleepTime,
  291. }),
  292. Entry("verify route by post", base.HttpTestCase{
  293. Object: base.APISIXExpect(),
  294. Method: http.MethodPost,
  295. Path: "/hello",
  296. Body: `test=test`,
  297. Headers: map[string]string{"Authorization": base.GetToken()},
  298. ExpectStatus: http.StatusNotFound,
  299. Sleep: base.SleepTime,
  300. }),
  301. Entry("update route methods to POST", base.HttpTestCase{
  302. Object: base.ManagerApiExpect(),
  303. Method: http.MethodPut,
  304. Path: "/apisix/admin/routes/r1",
  305. Body: `{
  306. "name": "route1",
  307. "uri": "/hello",
  308. "methods": ["POST"],
  309. "upstream": {
  310. "type": "roundrobin",
  311. "nodes": {
  312. "` + base.UpstreamIp + `:1980": 1
  313. }
  314. }
  315. }`,
  316. Headers: map[string]string{"Authorization": base.GetToken()},
  317. ExpectStatus: http.StatusOK,
  318. }),
  319. Entry("verify route by get", base.HttpTestCase{
  320. Object: base.APISIXExpect(),
  321. Method: http.MethodGet,
  322. Path: "/hello",
  323. Headers: map[string]string{"Authorization": base.GetToken()},
  324. ExpectStatus: http.StatusNotFound,
  325. Sleep: base.SleepTime,
  326. }),
  327. Entry("verify route by post", base.HttpTestCase{
  328. Object: base.APISIXExpect(),
  329. Method: http.MethodPost,
  330. Path: "/hello",
  331. Body: `test=test`,
  332. Headers: map[string]string{"Authorization": base.GetToken()},
  333. ExpectStatus: http.StatusOK,
  334. ExpectBody: "hello world",
  335. Sleep: base.SleepTime,
  336. }),
  337. Entry("delete route", base.HttpTestCase{
  338. Object: base.ManagerApiExpect(),
  339. Method: http.MethodDelete,
  340. Path: "/apisix/admin/routes/r1",
  341. Headers: map[string]string{"Authorization": base.GetToken()},
  342. ExpectStatus: http.StatusOK,
  343. Sleep: base.SleepTime,
  344. }),
  345. )
  346. })