handler.t 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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);
  7. run_tests();
  8. __DATA__
  9. === TEST 1: handle passed action
  10. --- http_config eval: $::HttpConfig
  11. --- config
  12. location /t {
  13. access_by_lua_block {
  14. local handler = require "resty.t1k.handler"
  15. local t = {
  16. action = ".",
  17. }
  18. local ok, err = handler.handle(t)
  19. if not ok then
  20. ngx.log(ngx.ERR, err)
  21. end
  22. }
  23. content_by_lua_block {
  24. ngx.say("passed")
  25. }
  26. }
  27. --- request
  28. GET /t
  29. --- response_body
  30. passed
  31. --- no_error_log
  32. [error]
  33. === TEST 2: handle blocked action
  34. --- http_config eval: $::HttpConfig
  35. --- config
  36. location /t {
  37. access_by_lua_block {
  38. local handler = require "resty.t1k.handler"
  39. local t = {
  40. action = "?",
  41. status = 405,
  42. event_id = "c0c039a7c348486eaffd9e2f9846b66b",
  43. }
  44. local ok, err = handler.handle(t)
  45. if not ok then
  46. ngx.log(ngx.ERR, err)
  47. end
  48. }
  49. header_filter_by_lua_block {
  50. local filter = require "resty.t1k.filter"
  51. filter.do_header_filter()
  52. }
  53. content_by_lua_block {
  54. ngx.say("passed")
  55. }
  56. }
  57. --- request
  58. GET /t
  59. --- response_body
  60. {"code": 405, "success":false, "message": "blocked by Chaitin SafeLine Web Application Firewall", "event_id": "c0c039a7c348486eaffd9e2f9846b66b"}
  61. --- error_code eval
  62. "405"
  63. --- no_error_log
  64. [error]
  65. --- no_error_log
  66. [error]
  67. === TEST 3: handle unknown action
  68. --- http_config eval: $::HttpConfig
  69. --- config
  70. location /t {
  71. access_by_lua_block {
  72. local handler = require "resty.t1k.handler"
  73. local t = {
  74. action = "~"
  75. }
  76. local ok, err = handler.handle(t)
  77. if not ok then
  78. ngx.log(ngx.ERR, err)
  79. end
  80. }
  81. content_by_lua_block {
  82. ngx.say("passed")
  83. }
  84. }
  85. --- request
  86. GET /t
  87. --- response_body
  88. passed
  89. --- error_log
  90. lua-resty-t1k: unknown action from t1k server: ~
  91. === TEST 4: handle nil result
  92. --- http_config eval: $::HttpConfig
  93. --- config
  94. location /t {
  95. access_by_lua_block {
  96. local handler = require "resty.t1k.handler"
  97. local ok, err = handler.handle(nil)
  98. if not ok then
  99. ngx.log(ngx.ERR, err)
  100. end
  101. }
  102. content_by_lua_block {
  103. ngx.say("passed")
  104. }
  105. }
  106. --- request
  107. GET /t
  108. --- response_body
  109. passed
  110. --- error_log
  111. lua-resty-t1k: invalid result type: nil