remote-addr.t 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. no_root_location();
  19. run_tests();
  20. __DATA__
  21. === TEST 1: set route: remote addr = 127.0.0.1
  22. --- config
  23. location /t {
  24. content_by_lua_block {
  25. local t = require("lib.test_admin").test
  26. local code, body = t('/apisix/admin/routes/1',
  27. ngx.HTTP_PUT,
  28. [[{
  29. "remote_addr": "127.0.0.1",
  30. "upstream": {
  31. "nodes": {
  32. "127.0.0.1:1980": 1
  33. },
  34. "type": "roundrobin"
  35. },
  36. "uri": "/hello"
  37. }]]
  38. )
  39. if code >= 300 then
  40. ngx.status = code
  41. end
  42. ngx.say(body)
  43. }
  44. }
  45. --- request
  46. GET /t
  47. --- response_body
  48. passed
  49. === TEST 2: /not_found
  50. --- request
  51. GET /not_found
  52. --- error_code: 404
  53. --- response_body
  54. {"error_msg":"404 Route Not Found"}
  55. === TEST 3: hit route
  56. --- request
  57. GET /hello
  58. --- response_body
  59. hello world
  60. === TEST 4: set route: remote addr = 127.0.0.2
  61. --- config
  62. location /t {
  63. content_by_lua_block {
  64. local t = require("lib.test_admin").test
  65. local code, body = t('/apisix/admin/routes/1',
  66. ngx.HTTP_PUT,
  67. [[{
  68. "remote_addr": "127.0.0.2",
  69. "upstream": {
  70. "nodes": {
  71. "127.0.0.1:1980": 1
  72. },
  73. "type": "roundrobin"
  74. },
  75. "uri": "/hello"
  76. }]]
  77. )
  78. if code >= 300 then
  79. ngx.status = code
  80. end
  81. ngx.say(body)
  82. }
  83. }
  84. --- request
  85. GET /t
  86. --- response_body
  87. passed
  88. === TEST 5: not hit route: 127.0.0.2 =~ 127.0.0.1
  89. --- request
  90. GET /hello
  91. --- error_code: 404
  92. --- response_body
  93. {"error_msg":"404 Route Not Found"}
  94. === TEST 6: remote addr: 127.0.0.3/24 =~ 127.0.0.1
  95. --- config
  96. location /t {
  97. content_by_lua_block {
  98. local t = require("lib.test_admin").test
  99. local code, body = t('/apisix/admin/routes/1',
  100. ngx.HTTP_PUT,
  101. [[{
  102. "remote_addr": "127.0.0.3/24",
  103. "upstream": {
  104. "nodes": {
  105. "127.0.0.1:1980": 1
  106. },
  107. "type": "roundrobin"
  108. },
  109. "uri": "/hello"
  110. }]]
  111. )
  112. if code >= 300 then
  113. ngx.status = code
  114. end
  115. ngx.say(body)
  116. }
  117. }
  118. --- request
  119. GET /t
  120. --- response_body
  121. passed
  122. === TEST 7: hit route
  123. --- request
  124. GET /hello
  125. --- response_body
  126. hello world