vars.t 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. no_root_location();
  21. no_shuffle();
  22. run_tests();
  23. __DATA__
  24. === TEST 1: set route(only arg_k)
  25. --- config
  26. location /t {
  27. content_by_lua_block {
  28. local t = require("lib.test_admin").test
  29. local code, body = t('/apisix/admin/routes/1',
  30. ngx.HTTP_PUT,
  31. [[{
  32. "uri": "/hello",
  33. "vars": [ ["arg_k", "==", "v"] ],
  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: /not_found
  53. --- request
  54. GET /not_found
  55. --- error_code: 404
  56. --- response_body
  57. {"error_msg":"404 Route Not Found"}
  58. === TEST 3: /not_found
  59. --- request
  60. GET /hello?k=not-hit
  61. --- error_code: 404
  62. --- response_body
  63. {"error_msg":"404 Route Not Found"}
  64. === TEST 4: hit routes
  65. --- request
  66. GET /hello?k=v
  67. --- response_body
  68. hello world
  69. === TEST 5: set route(cookie)
  70. --- config
  71. location /t {
  72. content_by_lua_block {
  73. local t = require("lib.test_admin").test
  74. local code, body = t('/apisix/admin/routes/1',
  75. ngx.HTTP_PUT,
  76. [=[{
  77. "uri": "/hello",
  78. "vars": [["cookie_k", "==", "v"]],
  79. "upstream": {
  80. "nodes": {
  81. "127.0.0.1:1980": 1
  82. },
  83. "type": "roundrobin"
  84. }
  85. }]=]
  86. )
  87. if code >= 300 then
  88. ngx.status = code
  89. end
  90. ngx.say(body)
  91. }
  92. }
  93. --- request
  94. GET /t
  95. --- response_body
  96. passed
  97. === TEST 6: /not_found
  98. --- request
  99. GET /hello
  100. --- error_code: 404
  101. --- response_body
  102. {"error_msg":"404 Route Not Found"}
  103. === TEST 7: /not_found
  104. --- more_headers
  105. Cookie: k=not-hit; kkk=vvv;
  106. --- request
  107. GET /hello
  108. --- error_code: 404
  109. --- response_body
  110. {"error_msg":"404 Route Not Found"}
  111. === TEST 8: hit routes
  112. --- more_headers
  113. Cookie: k=v; kkk=vvv;
  114. --- request
  115. GET /hello
  116. --- response_body
  117. hello world
  118. === TEST 9: set route(header)
  119. --- config
  120. location /t {
  121. content_by_lua_block {
  122. local t = require("lib.test_admin").test
  123. local code, body = t('/apisix/admin/routes/1',
  124. ngx.HTTP_PUT,
  125. [=[{
  126. "uri": "/hello",
  127. "vars": [["http_k", "==", "v"]],
  128. "upstream": {
  129. "nodes": {
  130. "127.0.0.1:1980": 1
  131. },
  132. "type": "roundrobin"
  133. }
  134. }]=]
  135. )
  136. if code >= 300 then
  137. ngx.status = code
  138. end
  139. ngx.say(body)
  140. }
  141. }
  142. --- request
  143. GET /t
  144. --- response_body
  145. passed
  146. === TEST 10: /not_found
  147. --- request
  148. GET /hello
  149. --- error_code: 404
  150. --- response_body
  151. {"error_msg":"404 Route Not Found"}
  152. === TEST 11: /not_found
  153. --- more_headers
  154. k: not-hit
  155. --- request
  156. GET /hello
  157. --- error_code: 404
  158. --- response_body
  159. {"error_msg":"404 Route Not Found"}
  160. === TEST 12: hit routes
  161. --- more_headers
  162. k: v
  163. --- request
  164. GET /hello
  165. --- response_body
  166. hello world
  167. === TEST 13: set route(uri arg + header + cookie)
  168. --- config
  169. location /t {
  170. content_by_lua_block {
  171. local t = require("lib.test_admin").test
  172. local code, body = t('/apisix/admin/routes/1',
  173. ngx.HTTP_PUT,
  174. [=[{
  175. "uri": "/hello",
  176. "vars": [["http_k", "==", "header"], ["cookie_k", "==", "cookie"], ["arg_k", "==", "uri_arg"]],
  177. "upstream": {
  178. "nodes": {
  179. "127.0.0.1:1980": 1
  180. },
  181. "type": "roundrobin"
  182. }
  183. }]=]
  184. )
  185. if code >= 300 then
  186. ngx.status = code
  187. end
  188. ngx.say(body)
  189. }
  190. }
  191. --- request
  192. GET /t
  193. --- response_body
  194. passed
  195. === TEST 14: /not_found
  196. --- request
  197. GET /hello
  198. --- error_code: 404
  199. --- response_body
  200. {"error_msg":"404 Route Not Found"}
  201. === TEST 15: /not_found
  202. --- more_headers
  203. k: header
  204. --- request
  205. GET /hello
  206. --- error_code: 404
  207. --- response_body
  208. {"error_msg":"404 Route Not Found"}
  209. === TEST 16: hit routes
  210. --- more_headers
  211. Cookie: k=cookie
  212. k: header
  213. --- request
  214. GET /hello?k=uri_arg
  215. --- response_body
  216. hello world
  217. === TEST 17: set route(only post arg)
  218. --- config
  219. location /t {
  220. content_by_lua_block {
  221. local t = require("lib.test_admin").test
  222. local code, body = t('/apisix/admin/routes/1',
  223. ngx.HTTP_PUT,
  224. [=[{
  225. "uri": "/hello",
  226. "vars": [["post_arg_k", "==", "post_form"]],
  227. "upstream": {
  228. "nodes": {
  229. "127.0.0.1:1980": 1
  230. },
  231. "type": "roundrobin"
  232. }
  233. }]=]
  234. )
  235. if code >= 300 then
  236. ngx.status = code
  237. end
  238. ngx.say(body)
  239. }
  240. }
  241. --- request
  242. GET /t
  243. --- response_body
  244. passed
  245. === TEST 18: not_found (GET request)
  246. --- request
  247. GET /hello
  248. --- error_code: 404
  249. --- response_body
  250. {"error_msg":"404 Route Not Found"}
  251. === TEST 19: not_found (wrong request body)
  252. --- request
  253. POST /hello
  254. 123
  255. --- error_code: 404
  256. --- response_body
  257. {"error_msg":"404 Route Not Found"}
  258. === TEST 20: not_found (wrong content type)
  259. --- request
  260. POST /hello
  261. k=post_form
  262. --- more_headers
  263. Content-Type: multipart/form-data
  264. --- error_code: 404
  265. --- response_body
  266. {"error_msg":"404 Route Not Found"}
  267. === TEST 21: hit routes
  268. --- request
  269. POST /hello
  270. k=post_form
  271. --- more_headers
  272. Content-Type: application/x-www-form-urlencoded
  273. --- response_body
  274. hello world