2
0

credential-plugin-set-request-header.t 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with 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,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. use t::APISIX 'no_plan';
  18. repeat_each(1);
  19. no_long_string();
  20. no_root_location();
  21. run_tests;
  22. __DATA__
  23. === TEST 1: enable key-auth on the route /echo
  24. --- config
  25. location /t {
  26. content_by_lua_block {
  27. local t = require("lib.test_admin").test
  28. local code, body = t('/apisix/admin/routes/1',
  29. ngx.HTTP_PUT,
  30. [[{
  31. "plugins": {
  32. "key-auth": {}
  33. },
  34. "upstream": {
  35. "nodes": {
  36. "127.0.0.1:1980": 1
  37. },
  38. "type": "roundrobin"
  39. },
  40. "uri": "/echo"
  41. }]]
  42. )
  43. if code >= 300 then
  44. ngx.status = code
  45. end
  46. ngx.say(body)
  47. }
  48. }
  49. --- request
  50. GET /t
  51. --- response_body
  52. passed
  53. === TEST 2: create consumer
  54. --- config
  55. location /t {
  56. content_by_lua_block {
  57. local t = require("lib.test_admin").test
  58. local code, body = t('/apisix/admin/consumers',
  59. ngx.HTTP_PUT,
  60. [[{
  61. "username": "jack"
  62. }]]
  63. )
  64. if code >= 300 then
  65. ngx.status = code
  66. end
  67. ngx.say(body)
  68. }
  69. }
  70. --- request
  71. GET /t
  72. --- response_body
  73. passed
  74. === TEST 3: create a credential with key-auth plugin enabled and 'custom_id' label for the consumer
  75. --- config
  76. location /t {
  77. content_by_lua_block {
  78. local t = require("lib.test_admin").test
  79. local code, body = t('/apisix/admin/consumers/jack/credentials/34010989-ce4e-4d61-9493-b54cca8edb31',
  80. ngx.HTTP_PUT,
  81. [[{
  82. "plugins": {
  83. "key-auth": {"key": "p7a3k6r4t9"}
  84. },
  85. "labels": {
  86. "custom_id": "271fc4a264bb"
  87. }
  88. }]],
  89. [[{
  90. "value":{
  91. "id":"34010989-ce4e-4d61-9493-b54cca8edb31",
  92. "plugins":{
  93. "key-auth": {"key": "fsFPtg7BtXMXkvSnS9e1zw=="}
  94. },
  95. "labels": {
  96. "custom_id": "271fc4a264bb"
  97. }
  98. },
  99. "key":"/apisix/consumers/jack/credentials/34010989-ce4e-4d61-9493-b54cca8edb31"
  100. }]]
  101. )
  102. ngx.status = code
  103. ngx.say(body)
  104. }
  105. }
  106. --- request
  107. GET /t
  108. --- response_body
  109. passed
  110. === TEST 4: request the route: 'x-consumer-username' and 'x-credential-identifier' is in response headers and 'x-consumer-custom-id' is not
  111. --- request
  112. GET /echo HTTP/1.1
  113. --- more_headers
  114. apikey: p7a3k6r4t9
  115. --- response_headers
  116. x-consumer-username: jack
  117. x-credential-identifier: 34010989-ce4e-4d61-9493-b54cca8edb31
  118. !x-consumer-custom-id
  119. === TEST 5: update the consumer add label "custom_id"
  120. --- config
  121. location /t {
  122. content_by_lua_block {
  123. local t = require("lib.test_admin").test
  124. local code, body = t('/apisix/admin/consumers',
  125. ngx.HTTP_PUT,
  126. [[{
  127. "username": "jack",
  128. "labels": {
  129. "custom_id": "495aec6a"
  130. }
  131. }]]
  132. )
  133. if code >= 300 then
  134. ngx.status = code
  135. end
  136. ngx.say(body)
  137. }
  138. }
  139. --- request
  140. GET /t
  141. --- response_body
  142. passed
  143. === TEST 6: request the route: the value of 'x-consumer-custom-id' come from the consumer but not the credential or downstream
  144. --- request
  145. GET /echo HTTP/1.1
  146. --- more_headers
  147. apikey: p7a3k6r4t9
  148. x-consumer-custom-id: 271fc4a264bb
  149. --- response_headers
  150. x-consumer-username: jack
  151. x-credential-identifier: 34010989-ce4e-4d61-9493-b54cca8edb31
  152. x-consumer-custom-id: 495aec6a
  153. === TEST 7: delete the credential
  154. --- config
  155. location /t {
  156. content_by_lua_block {
  157. local t = require("lib.test_admin").test
  158. local code, body = t('/apisix/admin/consumers/jack/credentials/34010989-ce4e-4d61-9493-b54cca8edb31', ngx.HTTP_DELETE)
  159. assert(code == 200)
  160. ngx.status = code
  161. }
  162. }
  163. --- request
  164. GET /t
  165. --- response_body
  166. === TEST 8: update the consumer to enable a key-auth plugin
  167. --- config
  168. location /t {
  169. content_by_lua_block {
  170. local t = require("lib.test_admin").test
  171. local code, body = t('/apisix/admin/consumers',
  172. ngx.HTTP_PUT,
  173. [[{
  174. "username": "jack",
  175. "plugins": {
  176. "key-auth": {
  177. "key": "p7a3k6r4t9"
  178. }
  179. }
  180. }]],
  181. [[{
  182. "value": {
  183. "username": "jack",
  184. "plugins": {
  185. "key-auth": {
  186. "key": "fsFPtg7BtXMXkvSnS9e1zw=="
  187. }
  188. }
  189. },
  190. "key": "/apisix/consumers/jack"
  191. }]]
  192. )
  193. ngx.status = code
  194. ngx.say(body)
  195. }
  196. }
  197. --- request
  198. GET /t
  199. --- response_body
  200. passed
  201. === TEST 9: request the route with headers x-credential-identifier and x-consumer-custom-id: these headers will be removed
  202. --- request
  203. GET /echo HTTP/1.1
  204. --- more_headers
  205. apikey: p7a3k6r4t9
  206. x-credential-identifier: 34010989-ce4e-4d61-9493-b54cca8edb31
  207. x-consumer-custom-id: 271fc4a264bb
  208. --- response_headers
  209. x-consumer-username: jack
  210. !x-credential-identifier
  211. !x-consumer-custom-id