route_with_plugin_orchestration_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. "io/ioutil"
  20. "net/http"
  21. . "github.com/onsi/ginkgo/v2"
  22. . "github.com/onsi/gomega"
  23. "github.com/tidwall/gjson"
  24. "github.com/apisix/manager-api/test/e2e/base"
  25. )
  26. var _ = Describe("route with plugin orchestration", func() {
  27. bytes, err := ioutil.ReadFile("../../testdata/dag-conf.json")
  28. It("panics if readfile dag-conf.json error", func() {
  29. Expect(err).To(BeNil())
  30. })
  31. dagConf := string(bytes)
  32. // invalid dag config that not specified root node
  33. bytes, err = ioutil.ReadFile("../../testdata/invalid-dag-conf.json")
  34. It("panics if readfile invalid-dag-conf.json error", func() {
  35. Expect(err).To(BeNil())
  36. })
  37. invalidDagConf := string(bytes)
  38. DescribeTable("test route with plugin orchestration",
  39. func(tc base.HttpTestCase) {
  40. base.RunTestCase(tc)
  41. },
  42. Entry("make sure the route is not created", base.HttpTestCase{
  43. Object: base.APISIXExpect(),
  44. Method: http.MethodGet,
  45. Path: "/hello",
  46. ExpectStatus: http.StatusNotFound,
  47. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  48. }),
  49. Entry("create route with invalid dag config", base.HttpTestCase{
  50. Object: base.ManagerApiExpect(),
  51. Method: http.MethodPut,
  52. Path: "/apisix/admin/routes/r1",
  53. Body: invalidDagConf,
  54. Headers: map[string]string{"Authorization": base.GetToken()},
  55. ExpectStatus: http.StatusBadRequest,
  56. }),
  57. Entry("make sure the route created failed", base.HttpTestCase{
  58. Object: base.APISIXExpect(),
  59. Method: http.MethodGet,
  60. Path: "/hello",
  61. ExpectStatus: http.StatusNotFound,
  62. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  63. Sleep: base.SleepTime,
  64. }),
  65. Entry("create route with correct dag config", base.HttpTestCase{
  66. Object: base.ManagerApiExpect(),
  67. Method: http.MethodPut,
  68. Path: "/apisix/admin/routes/r1",
  69. Body: dagConf,
  70. Headers: map[string]string{"Authorization": base.GetToken()},
  71. ExpectStatus: http.StatusOK,
  72. }),
  73. Entry("verify the route(should be blocked)", base.HttpTestCase{
  74. Object: base.APISIXExpect(),
  75. Method: http.MethodGet,
  76. Path: "/hello",
  77. Query: "t=root.exe",
  78. ExpectStatus: http.StatusForbidden,
  79. ExpectBody: `blocked`,
  80. Sleep: base.SleepTime,
  81. }),
  82. Entry("verify the route(should not be blocked)", base.HttpTestCase{
  83. Object: base.APISIXExpect(),
  84. Method: http.MethodGet,
  85. Path: "/hello",
  86. ExpectStatus: http.StatusOK,
  87. ExpectBody: `hello world`,
  88. }),
  89. Entry("delete route", base.HttpTestCase{
  90. Object: base.ManagerApiExpect(),
  91. Method: http.MethodDelete,
  92. Path: "/apisix/admin/routes/r1",
  93. Headers: map[string]string{"Authorization": base.GetToken()},
  94. ExpectStatus: http.StatusOK,
  95. }),
  96. Entry("hit the route just deleted", base.HttpTestCase{
  97. Object: base.APISIXExpect(),
  98. Method: http.MethodGet,
  99. Path: "/hello",
  100. ExpectStatus: http.StatusNotFound,
  101. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  102. Sleep: base.SleepTime,
  103. }),
  104. )
  105. DescribeTable("test route with plugin orchestration (post method)",
  106. func(tc base.HttpTestCase) {
  107. base.RunTestCase(tc)
  108. },
  109. Entry("make sure the route is not created", base.HttpTestCase{
  110. Object: base.APISIXExpect(),
  111. Method: http.MethodGet,
  112. Path: "/hello",
  113. ExpectStatus: http.StatusNotFound,
  114. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  115. }),
  116. )
  117. var routeID string
  118. It("create route with correct dag config by post", func() {
  119. resp, code, err := base.HttpPost(base.ManagerAPIHost+"/apisix/admin/routes",
  120. map[string]string{"Authorization": base.GetToken()}, dagConf)
  121. Expect(err).To(BeNil())
  122. Expect(code).Should(Equal(200))
  123. routeID = gjson.Get(string(resp), "data.id").String()
  124. })
  125. It("test the route with plugin orchestration", func() {
  126. base.RunTestCase(base.HttpTestCase{
  127. Desc: "verify the route (should be blocked)",
  128. Object: base.APISIXExpect(),
  129. Method: http.MethodGet,
  130. Path: "/hello",
  131. Query: "t=root.exe",
  132. ExpectStatus: http.StatusForbidden,
  133. ExpectBody: `blocked`,
  134. Sleep: base.SleepTime,
  135. })
  136. base.RunTestCase(base.HttpTestCase{
  137. Desc: "verify the route (should not be blocked)",
  138. Object: base.APISIXExpect(),
  139. Method: http.MethodGet,
  140. Path: "/hello",
  141. ExpectStatus: http.StatusOK,
  142. ExpectBody: `hello world`,
  143. })
  144. base.RunTestCase(base.HttpTestCase{
  145. Desc: "delete the route by routeID",
  146. Object: base.ManagerApiExpect(),
  147. Method: http.MethodDelete,
  148. Path: "/apisix/admin/routes/" + routeID,
  149. Headers: map[string]string{"Authorization": base.GetToken()},
  150. ExpectStatus: http.StatusOK,
  151. })
  152. base.RunTestCase(base.HttpTestCase{
  153. Desc: "make sure the route has been deleted",
  154. Object: base.APISIXExpect(),
  155. Method: http.MethodGet,
  156. Path: "/hello",
  157. ExpectStatus: http.StatusNotFound,
  158. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  159. Sleep: base.SleepTime,
  160. })
  161. })
  162. })