file.t 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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: read full file
  10. --- http_config eval: $::HttpConfig
  11. --- config
  12. location /t {
  13. content_by_lua_block {
  14. local file = require "resty.t1k.file"
  15. local path = ngx.var.document_root .. "/foo.bar"
  16. local ok, err, content = file.read(path, 2 ^ 15)
  17. if not ok then
  18. ngx.say(err)
  19. end
  20. ngx.print(table.concat(content))
  21. }
  22. }
  23. --- user_files eval
  24. [
  25. ["foo.bar" => "a" x (2 ** 14) ],
  26. ]
  27. --- request
  28. GET /t
  29. --- response_body eval
  30. "a" x (2 ** 14)
  31. --- no_error_log
  32. [error]
  33. === TEST 2: read partial file
  34. --- http_config eval: $::HttpConfig
  35. --- config
  36. location /t {
  37. content_by_lua_block {
  38. local file = require "resty.t1k.file"
  39. local path = ngx.var.document_root .. "/foo.bar"
  40. local ok, err, content = file.read(path, 2 ^ 13)
  41. if not ok then
  42. ngx.say(err)
  43. end
  44. ngx.print(table.concat(content))
  45. }
  46. }
  47. --- user_files eval
  48. [
  49. ["foo.bar" => "a" x (2 ** 14) ],
  50. ]
  51. --- request
  52. GET /t
  53. --- response_body eval
  54. "a" x (2 ** 13)
  55. --- no_error_log
  56. [error]
  57. === TEST 3: read negative bytes
  58. --- http_config eval: $::HttpConfig
  59. --- config
  60. location /t {
  61. content_by_lua_block {
  62. local file = require "resty.t1k.file"
  63. local path = ngx.var.document_root .. "/foo.bar"
  64. local ok, err, content = file.read(path, -1)
  65. if not ok then
  66. ngx.say(err)
  67. end
  68. ngx.print(table.concat(content))
  69. }
  70. }
  71. --- user_files
  72. >>> foo.bar
  73. --- request
  74. GET /t
  75. --- response_body eval
  76. ""
  77. --- no_error_log
  78. [error]
  79. === TEST 4: read empty file
  80. --- http_config eval: $::HttpConfig
  81. --- config
  82. location /t {
  83. content_by_lua_block {
  84. local file = require "resty.t1k.file"
  85. local path = ngx.var.document_root .. "/foo.bar"
  86. local ok, err, content = file.read(path, 1)
  87. if not ok then
  88. ngx.say(err)
  89. end
  90. ngx.print(table.concat(content))
  91. }
  92. }
  93. --- user_files
  94. >>> foo.bar
  95. --- request
  96. GET /t
  97. --- response_body eval
  98. ""
  99. --- no_error_log
  100. [error]
  101. === TEST 5: read non-existent file
  102. --- http_config eval: $::HttpConfig
  103. --- config
  104. location /t {
  105. content_by_lua_block {
  106. local file = require "resty.t1k.file"
  107. local ok, err, buffer = file.read("/opt/non_existent_file", 0)
  108. if not ok then
  109. ngx.say(err)
  110. end
  111. }
  112. }
  113. --- request
  114. GET /t
  115. --- response_body
  116. /opt/non_existent_file: No such file or directory
  117. --- no_error_log
  118. [error]