option.t 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. use Test::Nginx::Socket;
  2. our $HttpConfig = <<'_EOC_';
  3. lua_package_path "lib/?.lua;/usr/local/share/lua/5.1/?.lua;;";
  4. _EOC_
  5. repeat_each(3);
  6. plan tests => repeat_each() * (blocks() * 3 + 1);
  7. run_tests();
  8. __DATA__
  9. === TEST 1: do_access nil option
  10. --- http_config eval: $::HttpConfig
  11. --- config
  12. location /t {
  13. access_by_lua_block {
  14. local t1k = require "resty.t1k"
  15. local ok, err, _ = t1k.do_access(nil)
  16. if not ok then
  17. ngx.log(ngx.ERR, err)
  18. return
  19. end
  20. }
  21. content_by_lua_block {
  22. ngx.say("passed")
  23. }
  24. }
  25. --- request
  26. GET /t/shell.php
  27. --- response_body
  28. passed
  29. --- no_error_log
  30. [debug]
  31. --- log_level: debug
  32. === TEST 2: do_access disabled
  33. --- http_config eval: $::HttpConfig
  34. --- config
  35. location /t {
  36. access_by_lua_block {
  37. local t1k = require "resty.t1k"
  38. local t = {
  39. mode = "off",
  40. }
  41. local ok, err, _ = t1k.do_access(t)
  42. if not ok then
  43. ngx.log(ngx.ERR, err)
  44. return
  45. end
  46. }
  47. content_by_lua_block {
  48. ngx.say("passed")
  49. }
  50. }
  51. --- request
  52. GET /t/shell.php
  53. --- response_body
  54. passed
  55. --- no_error_log
  56. [error]
  57. --- error_log
  58. lua-resty-t1k: t1k is not enabled
  59. --- log_level: debug
  60. === TEST 3: do_access invalid mode
  61. --- http_config eval: $::HttpConfig
  62. --- config
  63. location /t {
  64. access_by_lua_block {
  65. local t1k = require "resty.t1k"
  66. local t = {
  67. mode = "invalid",
  68. }
  69. local ok, err, _ = t1k.do_access(t)
  70. if not ok then
  71. ngx.log(ngx.ERR, err)
  72. return
  73. end
  74. }
  75. content_by_lua_block {
  76. ngx.say("passed")
  77. }
  78. }
  79. --- request
  80. GET /t/shell.php
  81. --- response_body
  82. passed
  83. --- error_log
  84. lua-resty-t1k: invalid t1k mode: invalid
  85. === TEST 4: do_access invalid host
  86. --- http_config eval: $::HttpConfig
  87. --- config
  88. location /t {
  89. access_by_lua_block {
  90. local t1k = require "resty.t1k"
  91. local t = {
  92. mode = "block"
  93. }
  94. local ok, err, _ = t1k.do_access(t)
  95. if not ok then
  96. ngx.log(ngx.ERR, err)
  97. return
  98. end
  99. }
  100. content_by_lua_block {
  101. ngx.say("passed")
  102. }
  103. }
  104. --- request
  105. GET /t
  106. --- response_body
  107. passed
  108. --- error_log
  109. lua-resty-t1k: invalid t1k host: nil
  110. --- log_level: debug
  111. === TEST 5: do_access invalid port
  112. --- http_config eval: $::HttpConfig
  113. --- config
  114. location /t {
  115. access_by_lua_block {
  116. local t1k = require "resty.t1k"
  117. local t = {
  118. mode = "block",
  119. host = "127.0.0.1"
  120. }
  121. local ok, err, _ = t1k.do_access(t)
  122. if not ok then
  123. ngx.log(ngx.ERR, err)
  124. return
  125. end
  126. }
  127. content_by_lua_block {
  128. ngx.say("passed")
  129. }
  130. }
  131. --- request
  132. GET /t
  133. --- response_body
  134. passed
  135. --- error_log
  136. lua-resty-t1k: invalid t1k port: nil
  137. --- log_level: debug