route_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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", func() {
  24. DescribeTable("test route create and update",
  25. func(tc base.HttpTestCase) {
  26. base.RunTestCase(tc)
  27. },
  28. Entry("create route1 success", 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. "upstream": {
  36. "nodes": {
  37. "` + base.UpstreamIp + `:1980": 1
  38. },
  39. "type": "roundrobin"
  40. }
  41. }`,
  42. Headers: map[string]string{"Authorization": base.GetToken()},
  43. ExpectStatus: http.StatusOK,
  44. }),
  45. Entry("create route2 success", base.HttpTestCase{
  46. Object: base.ManagerApiExpect(),
  47. Method: http.MethodPut,
  48. Path: "/apisix/admin/routes/r2",
  49. Body: `{
  50. "name": "route2",
  51. "uri": "/hello_",
  52. "upstream": {
  53. "nodes": {
  54. "` + base.UpstreamIp + `:1980": 1
  55. },
  56. "type": "roundrobin"
  57. }
  58. }`,
  59. Headers: map[string]string{"Authorization": base.GetToken()},
  60. ExpectStatus: http.StatusOK,
  61. }),
  62. Entry("create long name route3 success", base.HttpTestCase{
  63. Object: base.ManagerApiExpect(),
  64. Method: http.MethodPut,
  65. Path: "/apisix/admin/routes/r3",
  66. Body: `{
  67. "name": "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
  68. "uri": "/hello_",
  69. "upstream": {
  70. "nodes": {
  71. "` + base.UpstreamIp + `:1980": 1
  72. },
  73. "type": "roundrobin"
  74. }
  75. }`,
  76. Headers: map[string]string{"Authorization": base.GetToken()},
  77. ExpectStatus: http.StatusOK,
  78. }),
  79. Entry("create route failed, name existed", base.HttpTestCase{
  80. Object: base.ManagerApiExpect(),
  81. Method: http.MethodPost,
  82. Path: "/apisix/admin/routes",
  83. Body: `{
  84. "name": "route2",
  85. "uri": "/hello_",
  86. "upstream": {
  87. "nodes": {
  88. "` + base.UpstreamIp + `:1980": 1
  89. },
  90. "type": "roundrobin"
  91. }
  92. }`,
  93. Headers: map[string]string{"Authorization": base.GetToken()},
  94. ExpectStatus: http.StatusBadRequest,
  95. ExpectBody: `route name exists`,
  96. Sleep: base.SleepTime,
  97. }),
  98. Entry("update route2 failed, name existed", base.HttpTestCase{
  99. Object: base.ManagerApiExpect(),
  100. Method: http.MethodPut,
  101. Path: "/apisix/admin/routes/r2",
  102. Body: `{
  103. "name": "route1",
  104. "uri": "/hello_",
  105. "upstream": {
  106. "nodes": {
  107. "` + base.UpstreamIp + `:1980": 1
  108. },
  109. "type": "roundrobin"
  110. }
  111. }`,
  112. Headers: map[string]string{"Authorization": base.GetToken()},
  113. ExpectStatus: http.StatusBadRequest,
  114. ExpectBody: `route name exists`,
  115. }),
  116. Entry("update route2 success, name not change", base.HttpTestCase{
  117. Object: base.ManagerApiExpect(),
  118. Method: http.MethodPut,
  119. Path: "/apisix/admin/routes/r2",
  120. Body: `{
  121. "name": "route2",
  122. "uri": "/hello",
  123. "upstream": {
  124. "nodes": {
  125. "` + base.UpstreamIp + `:1980": 1
  126. },
  127. "type": "roundrobin"
  128. }
  129. }`,
  130. Headers: map[string]string{"Authorization": base.GetToken()},
  131. ExpectStatus: http.StatusOK,
  132. }),
  133. Entry("hit route1", base.HttpTestCase{
  134. Object: base.APISIXExpect(),
  135. Method: http.MethodGet,
  136. Path: "/hello_",
  137. ExpectStatus: http.StatusOK,
  138. ExpectBody: "hello world",
  139. Sleep: base.SleepTime,
  140. }),
  141. Entry("hit route2", base.HttpTestCase{
  142. Object: base.APISIXExpect(),
  143. Method: http.MethodGet,
  144. Path: "/hello",
  145. ExpectStatus: http.StatusOK,
  146. ExpectBody: "hello world",
  147. }),
  148. Entry("delete route1", base.HttpTestCase{
  149. Object: base.ManagerApiExpect(),
  150. Method: http.MethodDelete,
  151. Path: "/apisix/admin/routes/r1",
  152. Headers: map[string]string{"Authorization": base.GetToken()},
  153. ExpectStatus: http.StatusOK,
  154. }),
  155. Entry("delete route2", base.HttpTestCase{
  156. Object: base.ManagerApiExpect(),
  157. Method: http.MethodDelete,
  158. Path: "/apisix/admin/routes/r2",
  159. Headers: map[string]string{"Authorization": base.GetToken()},
  160. ExpectStatus: http.StatusOK,
  161. }),
  162. Entry("delete route3", base.HttpTestCase{
  163. Object: base.ManagerApiExpect(),
  164. Method: http.MethodDelete,
  165. Path: "/apisix/admin/routes/r3",
  166. Headers: map[string]string{"Authorization": base.GetToken()},
  167. ExpectStatus: http.StatusOK,
  168. }),
  169. Entry("hit route1 that just deleted", base.HttpTestCase{
  170. Object: base.APISIXExpect(),
  171. Method: http.MethodGet,
  172. Path: "/hello_",
  173. ExpectStatus: http.StatusNotFound,
  174. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}",
  175. Sleep: base.SleepTime,
  176. }),
  177. Entry("hit route2 that just deleted", base.HttpTestCase{
  178. Object: base.APISIXExpect(),
  179. Method: http.MethodGet,
  180. Path: "/hello",
  181. ExpectStatus: http.StatusNotFound,
  182. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}",
  183. Sleep: base.SleepTime,
  184. }),
  185. )
  186. DescribeTable("test route patch",
  187. func(tc base.HttpTestCase) {
  188. base.RunTestCase(tc)
  189. },
  190. Entry("make sure the route not exists", base.HttpTestCase{
  191. Object: base.APISIXExpect(),
  192. Method: http.MethodGet,
  193. Path: "/hello",
  194. ExpectStatus: http.StatusNotFound,
  195. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  196. }),
  197. Entry("create route", base.HttpTestCase{
  198. Object: base.ManagerApiExpect(),
  199. Method: http.MethodPut,
  200. Path: "/apisix/admin/routes/r1",
  201. Body: `{
  202. "name": "route1",
  203. "uri": "/hello",
  204. "upstream": {
  205. "nodes": {
  206. "` + base.UpstreamIp + `:1980": 1
  207. },
  208. "type": "roundrobin"
  209. }
  210. }`,
  211. Headers: map[string]string{"Authorization": base.GetToken()},
  212. ExpectStatus: http.StatusOK,
  213. }),
  214. Entry("hit the route just created", base.HttpTestCase{
  215. Object: base.APISIXExpect(),
  216. Method: http.MethodGet,
  217. Path: "/hello",
  218. ExpectStatus: http.StatusOK,
  219. ExpectBody: "hello world",
  220. Sleep: base.SleepTime,
  221. }),
  222. Entry("route patch for update status(route offline)", base.HttpTestCase{
  223. Object: base.ManagerApiExpect(),
  224. Method: http.MethodPatch,
  225. Path: "/apisix/admin/routes/r1",
  226. Body: `{"status":0}`,
  227. Headers: map[string]string{"Authorization": base.GetToken()},
  228. ExpectStatus: http.StatusOK,
  229. }),
  230. Entry("make sure the route has been offline", 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\"}\n",
  236. Sleep: base.SleepTime,
  237. }),
  238. Entry("route patch for update status (route online)", base.HttpTestCase{
  239. Object: base.ManagerApiExpect(),
  240. Method: http.MethodPatch,
  241. Path: "/apisix/admin/routes/r1/status",
  242. Body: "1",
  243. Headers: map[string]string{
  244. "Authorization": base.GetToken(),
  245. "Content-Type": "text/plain",
  246. },
  247. ExpectStatus: http.StatusOK,
  248. }),
  249. Entry("make sure the route has been online", base.HttpTestCase{
  250. Object: base.APISIXExpect(),
  251. Method: http.MethodGet,
  252. Path: "/hello",
  253. ExpectStatus: http.StatusOK,
  254. ExpectBody: "hello world",
  255. Sleep: base.SleepTime,
  256. }),
  257. Entry("delete route", base.HttpTestCase{
  258. Object: base.ManagerApiExpect(),
  259. Method: http.MethodDelete,
  260. Path: "/apisix/admin/routes/r1",
  261. Headers: map[string]string{"Authorization": base.GetToken()},
  262. ExpectStatus: http.StatusOK,
  263. }),
  264. Entry("hit the route just deleted", base.HttpTestCase{
  265. Object: base.APISIXExpect(),
  266. Method: http.MethodGet,
  267. Path: "/hello",
  268. ExpectStatus: http.StatusNotFound,
  269. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  270. Sleep: base.SleepTime,
  271. }),
  272. )
  273. DescribeTable("test route create via POST",
  274. func(tc base.HttpTestCase) {
  275. base.RunTestCase(tc)
  276. },
  277. Entry("hit route that not exist", base.HttpTestCase{
  278. Object: base.APISIXExpect(),
  279. Method: http.MethodGet,
  280. Path: "/hello_",
  281. Headers: map[string]string{"Host": "foo.com"},
  282. ExpectStatus: http.StatusNotFound,
  283. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  284. }),
  285. Entry("create route via HTTP POST", base.HttpTestCase{
  286. Object: base.ManagerApiExpect(),
  287. Method: http.MethodPost,
  288. Path: "/apisix/admin/routes",
  289. Body: `{
  290. "id": "r1",
  291. "name": "route1",
  292. "uri": "/hello_",
  293. "hosts": ["foo.com", "*.bar.com"],
  294. "upstream": {
  295. "nodes": {
  296. "` + base.UpstreamIp + `:1980": 1
  297. },
  298. "type": "roundrobin"
  299. }
  300. }`,
  301. Headers: map[string]string{"Authorization": base.GetToken()},
  302. ExpectStatus: http.StatusOK,
  303. ExpectBody: "\"id\":\"r1\"",
  304. }),
  305. Entry("hit the route just created", base.HttpTestCase{
  306. Object: base.APISIXExpect(),
  307. Method: http.MethodGet,
  308. Path: "/hello_",
  309. Headers: map[string]string{"Host": "foo.com"},
  310. ExpectStatus: http.StatusOK,
  311. ExpectBody: "hello world\n",
  312. }),
  313. Entry("delete the route just created", base.HttpTestCase{
  314. Object: base.ManagerApiExpect(),
  315. Method: http.MethodDelete,
  316. Path: "/apisix/admin/routes/r1",
  317. Headers: map[string]string{"Authorization": base.GetToken()},
  318. ExpectStatus: http.StatusOK,
  319. }),
  320. Entry("hit the route just deleted", base.HttpTestCase{
  321. Object: base.APISIXExpect(),
  322. Method: http.MethodGet,
  323. Path: "/hello_",
  324. Headers: map[string]string{"Host": "bar.com"},
  325. ExpectStatus: http.StatusNotFound,
  326. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  327. Sleep: base.SleepTime,
  328. }),
  329. )
  330. })