merge-route.t 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. worker_connections(256);
  19. no_root_location();
  20. run_tests();
  21. __DATA__
  22. === TEST 1: set service(id: 1)
  23. --- config
  24. location /t {
  25. content_by_lua_block {
  26. local t = require("lib.test_admin").test
  27. local code, body = t('/apisix/admin/services/1',
  28. ngx.HTTP_PUT,
  29. [[{
  30. "plugins": {
  31. "limit-count": {
  32. "count": 2,
  33. "time_window": 60,
  34. "rejected_code": 503,
  35. "key": "remote_addr"
  36. }
  37. },
  38. "upstream": {
  39. "nodes": {
  40. "127.0.0.1:1980": 1
  41. },
  42. "type": "roundrobin"
  43. }
  44. }]]
  45. )
  46. if code >= 300 then
  47. ngx.status = code
  48. end
  49. ngx.say(body)
  50. }
  51. }
  52. --- request
  53. GET /t
  54. --- response_body
  55. passed
  56. === TEST 2: set route (different upstream)
  57. --- config
  58. location /t {
  59. content_by_lua_block {
  60. ngx.sleep(0.2)
  61. local t = require("lib.test_admin").test
  62. local code, body = t('/apisix/admin/routes/1',
  63. ngx.HTTP_PUT,
  64. [[{
  65. "upstream": {
  66. "nodes": {
  67. "127.0.0.1:1981": 1
  68. },
  69. "type": "roundrobin"
  70. },
  71. "uri": "/server_port",
  72. "service_id": 1
  73. }]]
  74. )
  75. if code >= 300 then
  76. ngx.status = code
  77. end
  78. ngx.say(body)
  79. }
  80. }
  81. --- request
  82. GET /t
  83. --- response_body
  84. passed
  85. === TEST 3: /not_found
  86. --- request
  87. GET /not_found
  88. --- error_code: 404
  89. --- response_body
  90. {"error_msg":"404 Route Not Found"}
  91. === TEST 4: hit routes
  92. --- request
  93. GET /server_port
  94. --- response_headers
  95. X-RateLimit-Limit: 2
  96. X-RateLimit-Remaining: 1
  97. --- response_body eval
  98. qr/1981/
  99. === TEST 5: set route with empty plugins, should do nothing
  100. --- config
  101. location /t {
  102. content_by_lua_block {
  103. ngx.sleep(0.2)
  104. local t = require("lib.test_admin").test
  105. local code, body = t('/apisix/admin/routes/1',
  106. ngx.HTTP_PUT,
  107. [[{
  108. "plugins": {},
  109. "uri": "/server_port",
  110. "service_id": 1
  111. }]]
  112. )
  113. if code >= 300 then
  114. ngx.status = code
  115. end
  116. ngx.say(body)
  117. }
  118. }
  119. --- request
  120. GET /t
  121. --- response_body
  122. passed
  123. === TEST 6: hit routes
  124. --- request
  125. GET /server_port
  126. --- response_headers
  127. X-RateLimit-Limit: 2
  128. X-RateLimit-Remaining: 1
  129. --- response_body eval
  130. qr/1980/
  131. === TEST 7: disable plugin `limit-count`
  132. --- config
  133. location /t {
  134. content_by_lua_block {
  135. ngx.sleep(0.2)
  136. local t = require("lib.test_admin").test
  137. local code, body = t('/apisix/admin/routes/1',
  138. ngx.HTTP_PUT,
  139. [[{
  140. "plugins": {
  141. "limit-count": {
  142. "count": 2,
  143. "time_window": 60,
  144. "rejected_code": 503,
  145. "key": "remote_addr",
  146. "_meta": {
  147. "disable": true
  148. }
  149. }
  150. },
  151. "uri": "/server_port",
  152. "service_id": 1
  153. }]]
  154. )
  155. if code >= 300 then
  156. ngx.status = code
  157. end
  158. ngx.say(body)
  159. }
  160. }
  161. --- request
  162. GET /t
  163. --- response_body
  164. passed
  165. === TEST 8: hit routes
  166. --- request
  167. GET /server_port
  168. --- raw_response_headers_unlike eval
  169. qr/X-RateLimit-Limit/
  170. --- response_body eval
  171. qr/1980/
  172. === TEST 9: hit routes two times, checker service configuration
  173. --- config
  174. location /t {
  175. content_by_lua_block {
  176. ngx.sleep(0.5)
  177. local t = require("lib.test_admin").test
  178. local code, body = t('/server_port',
  179. ngx.HTTP_GET
  180. )
  181. ngx.say(body)
  182. code, body = t('/server_port',
  183. ngx.HTTP_GET
  184. )
  185. ngx.say(body)
  186. }
  187. }
  188. --- request
  189. GET /t
  190. --- error_log eval
  191. [qr/merge_service_route.*"time_window":60/,
  192. qr/merge_service_route.*"time_window":60/]
  193. === TEST 10: set service(only upstream with host)
  194. --- config
  195. location /t {
  196. content_by_lua_block {
  197. local t = require("lib.test_admin").test
  198. local code, body = t('/apisix/admin/services/1',
  199. ngx.HTTP_PUT,
  200. [[{
  201. "upstream": {
  202. "scheme": "http",
  203. "type": "roundrobin",
  204. "nodes": {
  205. "test.com:1980": 1
  206. }
  207. }
  208. }]]
  209. )
  210. if code >= 300 then
  211. ngx.status = code
  212. end
  213. ngx.say(body)
  214. }
  215. }
  216. --- request
  217. GET /t
  218. --- response_body
  219. passed
  220. === TEST 11: set route(bind service 1)
  221. --- config
  222. location /t {
  223. content_by_lua_block {
  224. local t = require("lib.test_admin").test
  225. local code, body = t('/apisix/admin/routes/1',
  226. ngx.HTTP_PUT,
  227. [[{
  228. "uri": "/fake",
  229. "host": "test.com",
  230. "plugins": {
  231. "proxy-rewrite": {
  232. "uri": "/echo"
  233. }
  234. },
  235. "service_id": "1"
  236. }]]
  237. )
  238. if code >= 300 then
  239. ngx.status = code
  240. end
  241. ngx.say(body)
  242. }
  243. }
  244. --- request
  245. GET /t
  246. --- response_body
  247. passed
  248. === TEST 12: hit route
  249. --- request
  250. GET /fake
  251. --- more_headers
  252. host: test.com
  253. --- response_headers
  254. host: test.com
  255. === TEST 13: not hit route
  256. --- request
  257. GET /fake
  258. --- more_headers
  259. host: test.comxxx
  260. --- error_code: 404
  261. === TEST 14: enabled websocket in service
  262. --- config
  263. location /t {
  264. content_by_lua_block {
  265. local t = require("lib.test_admin").test
  266. local code, body = t('/apisix/admin/services/1',
  267. ngx.HTTP_PUT,
  268. [[{
  269. "enable_websocket": true,
  270. "upstream": {
  271. "nodes": {
  272. "127.0.0.1:1980": 1
  273. },
  274. "type": "roundrobin"
  275. }
  276. }]]
  277. )
  278. if code >= 300 then
  279. ngx.status = code
  280. end
  281. ngx.say(body)
  282. }
  283. }
  284. --- request
  285. GET /t
  286. --- response_body
  287. passed
  288. === TEST 15: set route(bind service 1)
  289. --- config
  290. location /t {
  291. content_by_lua_block {
  292. local t = require("lib.test_admin").test
  293. local code, body = t('/apisix/admin/routes/33',
  294. ngx.HTTP_PUT,
  295. [[{
  296. "uri": "/uri",
  297. "service_id": "1"
  298. }]]
  299. )
  300. if code >= 300 then
  301. ngx.status = code
  302. end
  303. ngx.say(body)
  304. }
  305. }
  306. --- request
  307. GET /t
  308. --- response_body
  309. passed
  310. === TEST 16: hit route
  311. --- request
  312. GET /uri
  313. --- response_body
  314. uri: /uri
  315. connection: close
  316. host: localhost
  317. x-real-ip: 127.0.0.1
  318. --- error_log
  319. enabled websocket for route: 33
  320. === TEST 17: delete rout
  321. --- config
  322. location /t {
  323. content_by_lua_block {
  324. local t = require("lib.test_admin").test
  325. local code, body = t('/apisix/admin/routes/33',
  326. ngx.HTTP_DELETE
  327. )
  328. if code >= 300 then
  329. ngx.status = code
  330. end
  331. ngx.say(body)
  332. }
  333. }
  334. --- request
  335. GET /t
  336. --- response_body
  337. passed
  338. === TEST 18: labels exist if only route
  339. --- config
  340. location /t {
  341. content_by_lua_block {
  342. local t = require("lib.test_admin").test
  343. code, body = t('/apisix/admin/routes/1',
  344. ngx.HTTP_PUT,
  345. [[{
  346. "upstream": {
  347. "nodes": {
  348. "127.0.0.1:1980": 1
  349. },
  350. "type": "roundrobin"
  351. },
  352. "plugins": {
  353. "serverless-pre-function": {
  354. "phase": "rewrite",
  355. "functions" : ["return function(conf, ctx)
  356. local core = require(\"apisix.core\");
  357. ngx.say(core.json.encode(ctx.matched_route.value.labels));
  358. end"]
  359. }
  360. },
  361. "labels": {
  362. "version": "v2"
  363. },
  364. "uri": "/hello"
  365. }]]
  366. )
  367. if code >= 300 then
  368. ngx.status = code
  369. end
  370. ngx.say(body)
  371. }
  372. }
  373. --- request
  374. GET /t
  375. --- response_body
  376. passed
  377. === TEST 19: hit routes
  378. --- request
  379. GET /hello
  380. --- response_body
  381. {"version":"v2"}
  382. === TEST 20: labels exist if merge route and service
  383. --- config
  384. location /t {
  385. content_by_lua_block {
  386. local t = require("lib.test_admin").test
  387. local code, body = t('/apisix/admin/services/1',
  388. ngx.HTTP_PUT,
  389. [[{
  390. "upstream": {
  391. "nodes": {
  392. "127.0.0.1:1980": 1
  393. },
  394. "type": "roundrobin"
  395. }
  396. }]]
  397. )
  398. if code >= 300 then
  399. ngx.status = code
  400. end
  401. ngx.sleep(0.6) -- wait for sync
  402. code, body = t('/apisix/admin/routes/1',
  403. ngx.HTTP_PUT,
  404. [[{
  405. "plugins": {
  406. "serverless-pre-function": {
  407. "phase": "rewrite",
  408. "functions" : ["return function(conf, ctx)
  409. local core = require(\"apisix.core\");
  410. ngx.say(core.json.encode(ctx.matched_route.value.labels));
  411. end"]
  412. }
  413. },
  414. "labels": {
  415. "version": "v2"
  416. },
  417. "uri": "/hello",
  418. "service_id": 1
  419. }]]
  420. )
  421. if code >= 300 then
  422. ngx.status = code
  423. end
  424. ngx.say(body)
  425. }
  426. }
  427. --- request
  428. GET /t
  429. --- response_body
  430. passed
  431. === TEST 21: hit routes
  432. --- request
  433. GET /hello
  434. --- response_body
  435. {"version":"v2"}