test-index-cgi.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. ##
  2. ## $Release: 2.7.0 $
  3. ## copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
  4. ##
  5. require "#{File.dirname(__FILE__)}/test.rb"
  6. require 'stringio'
  7. load "#{File.dirname(__FILE__)}/../public_html/index.cgi"
  8. def spec(desc)
  9. yield
  10. end
  11. def dummy_env(request_method='GET', request_uri='/', opts={})
  12. if request_method.is_a?(Hash)
  13. opts = request_method
  14. request_method = 'GET'
  15. request_uri = '/'
  16. elsif request_uri.is_a?(Hash)
  17. opts = request_uri
  18. request_uri = '/'
  19. end
  20. env = {
  21. 'REQUEST_METHOD' => request_method.to_s,
  22. 'REQUEST_URI' => request_uri.to_s,
  23. 'DOCUMENT_ROOT' => Dir.pwd,
  24. }
  25. opts.each {|k, v| env[k.to_s.upcase] = v }
  26. env.update(opts)
  27. return env
  28. end
  29. def dummy_template(filename, content)
  30. begin
  31. File.open(filename, 'wb') {|f| f.write(content) }
  32. return yield
  33. ensure
  34. [filename, filename + '.cache'].each do |fname|
  35. File.unlink(fname) if File.exist?(fname)
  36. end
  37. end
  38. end
  39. class ErubisHandlerTest < Test::Unit::TestCase
  40. def test_initialize
  41. spec "sets @encoding and @layout" do
  42. encoding_bkup = $ENCODING
  43. layout_bkup = $LAYOUT
  44. begin
  45. $ENCODING = 'cp932'
  46. $LAYOUT = 'site.rhtml'
  47. obj = ErubisHandler.new
  48. assert_equal 'cp932', obj.encoding
  49. assert_equal 'site.rhtml', obj.layout
  50. ensure
  51. $ENCODING = encoding_bkup
  52. $LAYOUT = layout_bkup
  53. end
  54. end
  55. end
  56. def test_handle
  57. spec "renders requested template file." do
  58. base = "_test_handle"
  59. env = dummy_env('GET', "/#{base}.html")
  60. handler = ErubisHandler.new
  61. input = <<'END'
  62. <h1><%= '<b>SOS</b>' %></h1>
  63. <ul>
  64. <% for item in %w[Haruhi Mikuru Yuki] %>
  65. <li><%= item %></li>
  66. <% end %>
  67. </ul>
  68. END
  69. expected = <<'END'
  70. <h1><b>SOS</b></h1>
  71. <ul>
  72. <li>Haruhi</li>
  73. <li>Mikuru</li>
  74. <li>Yuki</li>
  75. </ul>
  76. END
  77. out = dummy_template("#{base}.rhtml", input) do
  78. handler.handle(env)
  79. end
  80. assert_equal expected, out
  81. end
  82. spec "raises 404 error when requested file not found." do
  83. req_url = "/_test_handle.html"
  84. env = dummy_env('GET', req_url)
  85. handler = ErubisHandler.new
  86. ex = assert_raise HttpError do
  87. handler.handle(env)
  88. end
  89. assert_equal 404, ex.status
  90. assert_equal "#{req_url}: not found.", ex.message
  91. end
  92. end
  93. end
  94. class ErubisApplicationTest < Test::Unit::TestCase
  95. def test_handle_request
  96. spec "handles request by handler object and returns response data." do
  97. app = ErubisApplication.new()
  98. def app.get_handler
  99. return Class.new {
  100. def handle(env); "<p>Hello SOS</p>"; end
  101. def encoding; "euc_jp"; end
  102. }.new
  103. end
  104. expected = [
  105. 200,
  106. [["Content-Type", "text/html;charset=euc_jp"]],
  107. ["<p>Hello SOS</p>"],
  108. ]
  109. env = dummy_env('GET', '/')
  110. ret = app.call(env)
  111. assert_equal expected, ret
  112. end
  113. end
  114. def test_handle_http_error
  115. spec "renders error page." do
  116. req_path = '/HaruhiSuzumiya.html'
  117. app = ErubisApplication.new()
  118. env = dummy_env('GET', req_path)
  119. expected = [
  120. 404,
  121. [["Content-Type", "text/html"]],
  122. ["<h2>404 Not Found</h2>\n<p>#{req_path}: not found.</p>\n"],
  123. ]
  124. ret = app.call(env)
  125. assert_equal expected, ret
  126. end
  127. end
  128. def test_run
  129. spec "prints to $stdout" do
  130. input = "<p>Hello SOS</p>"
  131. app = ErubisApplication.new
  132. base = "SOS"
  133. env = dummy_env("GET", "/#{base}.html")
  134. sio = StringIO.new
  135. output = dummy_template("#{base}.rhtml", input) do
  136. app.run(env, sio)
  137. sio.string
  138. end
  139. expected = ""
  140. expected << "Content-Type: text/html\r\n"
  141. expected << "\r\n"
  142. expected << "<p>Hello SOS</p>"
  143. assert_equal expected, output
  144. end
  145. spec "prints 'Status:' header if status code is not 200." do
  146. req_path = "/SOS.html"
  147. env = dummy_env("GET", req_path)
  148. app = ErubisApplication.new
  149. sio = StringIO.new
  150. app.run(env, sio)
  151. expected = "Status: 404 Not Found\r\n"
  152. expected << "Content-Type: text/html\r\n"
  153. expected << "\r\n"
  154. expected << "<h2>404 Not Found</h2>\n"
  155. expected << "<p>#{req_path}: not found.</p>\n"
  156. assert_equal expected, sio.string
  157. end
  158. end
  159. end