route-status.t 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. use t::APISIX 'no_plan';
  18. repeat_each(1);
  19. log_level('info');
  20. worker_connections(256);
  21. no_root_location();
  22. no_shuffle();
  23. run_tests();
  24. __DATA__
  25. === TEST 1: default enable route(id: 1) with uri match
  26. --- config
  27. location /t {
  28. content_by_lua_block {
  29. local t = require("lib.test_admin").test
  30. local code, body = t('/apisix/admin/routes/1',
  31. ngx.HTTP_PUT,
  32. [[{
  33. "uri": "/hello",
  34. "upstream": {
  35. "nodes": {
  36. "127.0.0.1:1980": 1
  37. },
  38. "type": "roundrobin"
  39. }
  40. }]]
  41. )
  42. if code >= 300 then
  43. ngx.status = code
  44. end
  45. ngx.say(body)
  46. }
  47. }
  48. --- request
  49. GET /t
  50. --- response_body
  51. passed
  52. === TEST 2: hit route
  53. --- request
  54. GET /hello
  55. --- response_body
  56. hello world
  57. === TEST 3: disable route
  58. --- config
  59. location /t {
  60. content_by_lua_block {
  61. local core = require("apisix.core")
  62. local t = require("lib.test_admin").test
  63. local data = {status = 0}
  64. local code, body = t('/apisix/admin/routes/1',
  65. ngx.HTTP_PATCH,
  66. core.json.encode(data)
  67. )
  68. if code >= 300 then
  69. ngx.status = code
  70. end
  71. ngx.say(body)
  72. }
  73. }
  74. --- request
  75. GET /t
  76. --- response_body
  77. passed
  78. === TEST 4: route not found, failed by disable
  79. --- request
  80. GET /hello
  81. --- error_code: 404
  82. --- response_body
  83. {"error_msg":"404 Route Not Found"}
  84. === TEST 5: default enable route(id: 1) with host_uri match
  85. --- config
  86. location /t {
  87. content_by_lua_block {
  88. local t = require("lib.test_admin").test
  89. local code, body = t('/apisix/admin/routes/1',
  90. ngx.HTTP_PUT,
  91. [[{
  92. "uri": "/hello",
  93. "host": "foo.com",
  94. "upstream": {
  95. "nodes": {
  96. "127.0.0.1:1980": 1
  97. },
  98. "type": "roundrobin"
  99. }
  100. }]]
  101. )
  102. if code >= 300 then
  103. ngx.status = code
  104. end
  105. ngx.say(body)
  106. }
  107. }
  108. --- request
  109. GET /t
  110. --- response_body
  111. passed
  112. === TEST 6: hit route
  113. --- request
  114. GET /hello
  115. --- more_headers
  116. Host: foo.com
  117. --- response_body
  118. hello world
  119. === TEST 7: disable route
  120. --- config
  121. location /t {
  122. content_by_lua_block {
  123. local core = require("apisix.core")
  124. local t = require("lib.test_admin").test
  125. local data = {status = 0}
  126. local code, body = t('/apisix/admin/routes/1',
  127. ngx.HTTP_PATCH,
  128. core.json.encode(data)
  129. )
  130. if code >= 300 then
  131. ngx.status = code
  132. end
  133. ngx.say(body)
  134. }
  135. }
  136. --- request
  137. GET /t
  138. --- response_body
  139. passed
  140. === TEST 8: route not found, failed by disable
  141. --- request
  142. GET /hello
  143. --- more_headers
  144. Host: foo.com
  145. --- error_code: 404
  146. --- response_body
  147. {"error_msg":"404 Route Not Found"}
  148. === TEST 9: specify an invalid status value
  149. --- config
  150. location /t {
  151. content_by_lua_block {
  152. local t = require("lib.test_admin").test
  153. local code, body = t('/apisix/admin/routes/1',
  154. ngx.HTTP_PUT,
  155. [[{
  156. "uri": "/hello",
  157. "status": 100,
  158. "upstream": {
  159. "nodes": {
  160. "127.0.0.1:1980": 1
  161. },
  162. "type": "roundrobin"
  163. }
  164. }]]
  165. )
  166. if code >= 300 then
  167. ngx.status = code
  168. end
  169. ngx.say(body)
  170. }
  171. }
  172. --- request
  173. GET /t
  174. --- error_code: 400
  175. --- response_body eval
  176. qr/\{"error_msg":"invalid configuration: property \\"status\\" validation failed: matches none of the enum values"\}/
  177. === TEST 10: compatible with old route data in etcd which not has status
  178. --- config
  179. location /t {
  180. content_by_lua_block {
  181. local core = require("apisix.core")
  182. local res, err = core.etcd.set("/routes/1", core.json.decode([[{
  183. "uri": "/hello",
  184. "priority": 0,
  185. "id": "1",
  186. "upstream": {
  187. "hash_on": "vars",
  188. "pass_host": "pass",
  189. "nodes": {
  190. "127.0.0.1:1980": 1
  191. },
  192. "type": "roundrobin"
  193. }
  194. }]])) ---mock old route data in etcd
  195. if res.status >= 300 then
  196. res.status = code
  197. end
  198. ngx.print(require("toolkit.json").encode(res.body))
  199. ngx.sleep(1)
  200. }
  201. }
  202. --- request
  203. GET /t
  204. --- response_body_unlike eval
  205. qr/status/
  206. === TEST 11: hit route(old route data in etcd)
  207. --- request
  208. GET /hello
  209. --- response_body
  210. hello world