consumer-group.t 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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('warn');
  20. no_root_location();
  21. no_shuffle();
  22. add_block_preprocessor(sub {
  23. my ($block) = @_;
  24. if (!$block->request) {
  25. $block->set_value("request", "GET /t");
  26. }
  27. if (!$block->error_log && !$block->no_error_log) {
  28. $block->set_value("no_error_log", "[error]");
  29. }
  30. });
  31. run_tests();
  32. __DATA__
  33. === TEST 1: consumer group usage
  34. --- config
  35. location /t {
  36. content_by_lua_block {
  37. local t = require("lib.test_admin").test
  38. local code, err = t('/apisix/admin/consumer_groups/bar',
  39. ngx.HTTP_PUT,
  40. [[{
  41. "plugins": {
  42. "response-rewrite": {
  43. "body": "hello"
  44. }
  45. }
  46. }]]
  47. )
  48. if code > 300 then
  49. ngx.log(ngx.ERR, err)
  50. return
  51. end
  52. local code, body = t('/apisix/admin/consumers',
  53. ngx.HTTP_PUT,
  54. [[{
  55. "username": "foo",
  56. "group_id": "bar",
  57. "plugins": {
  58. "basic-auth": {
  59. "username": "foo",
  60. "password": "bar"
  61. }
  62. }
  63. }]]
  64. )
  65. if code >= 300 then
  66. ngx.status = code
  67. ngx.say(body)
  68. return
  69. end
  70. local code, err = t('/apisix/admin/routes/1',
  71. ngx.HTTP_PUT,
  72. [[{
  73. "uri": "/hello",
  74. "upstream": {
  75. "nodes": {
  76. "127.0.0.1:1980": 1
  77. },
  78. "type": "roundrobin"
  79. },
  80. "plugins": {
  81. "basic-auth": {}
  82. }
  83. }]]
  84. )
  85. if code > 300 then
  86. ngx.log(ngx.ERR, err)
  87. return
  88. end
  89. ngx.sleep(0.5)
  90. local http = require "resty.http"
  91. local httpc = http.new()
  92. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  93. .. "/hello"
  94. local headers = {
  95. ["Authorization"] = "Basic Zm9vOmJhcg=="
  96. }
  97. local res, err = httpc:request_uri(uri, {headers = headers})
  98. ngx.say(res.body)
  99. local code, err = t('/apisix/admin/consumer_groups/bar',
  100. ngx.HTTP_PATCH,
  101. [[{
  102. "plugins": {
  103. "response-rewrite": {
  104. "body": "world"
  105. }
  106. }
  107. }]]
  108. )
  109. if code > 300 then
  110. ngx.log(ngx.ERR, err)
  111. return
  112. end
  113. ngx.sleep(0.1)
  114. local res, err = httpc:request_uri(uri, {headers = headers})
  115. ngx.say(res.body)
  116. }
  117. }
  118. --- response_body
  119. hello
  120. world
  121. === TEST 2: validated plugins configuration via incremental sync (malformed data)
  122. --- config
  123. location /t {
  124. content_by_lua_block {
  125. local http = require "resty.http"
  126. local core = require("apisix.core")
  127. assert(core.etcd.set("/consumer_groups/bar",
  128. {id = "bar", plugins = { ["uri-blocker"] = { block_rules = 1 }}}
  129. ))
  130. -- wait for sync
  131. ngx.sleep(0.6)
  132. assert(core.etcd.delete("/consumer_groups/bar"))
  133. }
  134. }
  135. --- error_log
  136. property "block_rules" validation failed
  137. === TEST 3: don't override the plugin in the consumer
  138. --- config
  139. location /t {
  140. content_by_lua_block {
  141. local t = require("lib.test_admin").test
  142. local code, err = t('/apisix/admin/consumer_groups/bar',
  143. ngx.HTTP_PUT,
  144. [[{
  145. "plugins": {
  146. "response-rewrite": {
  147. "body": "hello"
  148. }
  149. }
  150. }]]
  151. )
  152. if code > 300 then
  153. ngx.log(ngx.ERR, err)
  154. return
  155. end
  156. local code, body = t('/apisix/admin/consumers',
  157. ngx.HTTP_PUT,
  158. [[{
  159. "username": "foo",
  160. "group_id": "bar",
  161. "plugins": {
  162. "basic-auth": {
  163. "username": "foo",
  164. "password": "bar"
  165. },
  166. "response-rewrite": {
  167. "body": "world"
  168. }
  169. }
  170. }]]
  171. )
  172. if code >= 300 then
  173. ngx.status = code
  174. ngx.say(body)
  175. return
  176. end
  177. local code, err = t('/apisix/admin/routes/1',
  178. ngx.HTTP_PUT,
  179. [[{
  180. "uri": "/hello",
  181. "upstream": {
  182. "nodes": {
  183. "127.0.0.1:1980": 1
  184. },
  185. "type": "roundrobin"
  186. },
  187. "plugins": {
  188. "basic-auth": {}
  189. }
  190. }]]
  191. )
  192. if code > 300 then
  193. ngx.log(ngx.ERR, err)
  194. return
  195. end
  196. ngx.sleep(0.1)
  197. local http = require "resty.http"
  198. local httpc = http.new()
  199. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  200. .. "/hello"
  201. local headers = {
  202. ["Authorization"] = "Basic Zm9vOmJhcg=="
  203. }
  204. local res, err = httpc:request_uri(uri, {headers = headers})
  205. ngx.say(res.body)
  206. }
  207. }
  208. --- response_body
  209. world
  210. === TEST 4: check consumer_group_id var
  211. --- config
  212. location /t {
  213. content_by_lua_block {
  214. local t = require("lib.test_admin").test
  215. local code, err = t('/apisix/admin/consumer_groups/bar',
  216. ngx.HTTP_PUT,
  217. [[{
  218. "plugins": {
  219. "serverless-post-function": {
  220. "phase": "access",
  221. "functions" : ["return function(_, ctx) ngx.say(ctx.var.consumer_group_id); ngx.exit(200); end"]
  222. }
  223. }
  224. }]]
  225. )
  226. if code > 300 then
  227. ngx.log(ngx.ERR, err)
  228. return
  229. end
  230. local code, body = t('/apisix/admin/consumers',
  231. ngx.HTTP_PUT,
  232. [[{
  233. "username": "foo",
  234. "group_id": "bar",
  235. "plugins": {
  236. "basic-auth": {
  237. "username": "foo",
  238. "password": "bar"
  239. }
  240. }
  241. }]]
  242. )
  243. if code >= 300 then
  244. ngx.status = code
  245. ngx.say(body)
  246. return
  247. end
  248. local code, err = t('/apisix/admin/routes/1',
  249. ngx.HTTP_PUT,
  250. [[{
  251. "uri": "/hello",
  252. "upstream": {
  253. "nodes": {
  254. "127.0.0.1:1980": 1
  255. },
  256. "type": "roundrobin"
  257. },
  258. "plugins": {
  259. "basic-auth": {}
  260. }
  261. }]]
  262. )
  263. if code > 300 then
  264. ngx.log(ngx.ERR, err)
  265. return
  266. end
  267. ngx.sleep(0.5)
  268. local http = require "resty.http"
  269. local httpc = http.new()
  270. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  271. .. "/hello"
  272. local headers = {
  273. ["Authorization"] = "Basic Zm9vOmJhcg=="
  274. }
  275. local res, err = httpc:request_uri(uri, {headers = headers})
  276. ngx.print(res.body)
  277. }
  278. }
  279. --- response_body
  280. bar