PageRenderTime 25ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 1ms

/actionpack/test/controller/cgi_test.rb

https://github.com/wayshawn/rails
Ruby | 220 lines | 189 code | 27 blank | 4 comment | 0 complexity | 0aba13001b48ffd0db44b2b19f3ea733 MD5 | raw file
  1. require 'abstract_unit'
  2. require 'action_controller/cgi_process'
  3. class BaseCgiTest < Test::Unit::TestCase
  4. def setup
  5. @request_hash = {
  6. "HTTP_MAX_FORWARDS" => "10",
  7. "SERVER_NAME" => "glu.ttono.us:8007",
  8. "FCGI_ROLE" => "RESPONDER",
  9. "AUTH_TYPE" => "Basic",
  10. "HTTP_X_FORWARDED_HOST" => "glu.ttono.us",
  11. "HTTP_ACCEPT_CHARSET" => "UTF-8",
  12. "HTTP_ACCEPT_ENCODING" => "gzip, deflate",
  13. "HTTP_CACHE_CONTROL" => "no-cache, max-age=0",
  14. "HTTP_PRAGMA" => "no-cache",
  15. "HTTP_USER_AGENT" => "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en)",
  16. "PATH_INFO" => "/homepage/",
  17. "HTTP_ACCEPT_LANGUAGE" => "en",
  18. "HTTP_NEGOTIATE" => "trans",
  19. "HTTP_HOST" => "glu.ttono.us:8007",
  20. "HTTP_REFERER" => "http://www.google.com/search?q=glu.ttono.us",
  21. "HTTP_FROM" => "googlebot",
  22. "SERVER_PROTOCOL" => "HTTP/1.1",
  23. "REDIRECT_URI" => "/dispatch.fcgi",
  24. "SCRIPT_NAME" => "/dispatch.fcgi",
  25. "SERVER_ADDR" => "207.7.108.53",
  26. "REMOTE_ADDR" => "207.7.108.53",
  27. "REMOTE_HOST" => "google.com",
  28. "REMOTE_IDENT" => "kevin",
  29. "REMOTE_USER" => "kevin",
  30. "SERVER_SOFTWARE" => "lighttpd/1.4.5",
  31. "HTTP_COOKIE" => "_session_id=c84ace84796670c052c6ceb2451fb0f2; is_admin=yes",
  32. "HTTP_X_FORWARDED_SERVER" => "glu.ttono.us",
  33. "REQUEST_URI" => "/admin",
  34. "DOCUMENT_ROOT" => "/home/kevinc/sites/typo/public",
  35. "PATH_TRANSLATED" => "/home/kevinc/sites/typo/public/homepage/",
  36. "SERVER_PORT" => "8007",
  37. "QUERY_STRING" => "",
  38. "REMOTE_PORT" => "63137",
  39. "GATEWAY_INTERFACE" => "CGI/1.1",
  40. "HTTP_X_FORWARDED_FOR" => "65.88.180.234",
  41. "HTTP_ACCEPT" => "*/*",
  42. "SCRIPT_FILENAME" => "/home/kevinc/sites/typo/public/dispatch.fcgi",
  43. "REDIRECT_STATUS" => "200",
  44. "REQUEST_METHOD" => "GET"
  45. }
  46. # some Nokia phone browsers omit the space after the semicolon separator.
  47. # some developers have grown accustomed to using comma in cookie values.
  48. @alt_cookie_fmt_request_hash = {"HTTP_COOKIE"=>"_session_id=c84ace847,96670c052c6ceb2451fb0f2;is_admin=yes"}
  49. @cgi = CGI.new
  50. @cgi.stubs(:env_table).returns(@request_hash)
  51. @request = ActionController::CgiRequest.new(@cgi)
  52. end
  53. def default_test; end
  54. end
  55. class CgiRequestTest < BaseCgiTest
  56. def test_proxy_request
  57. assert_equal 'glu.ttono.us', @request.host_with_port
  58. end
  59. def test_http_host
  60. @request_hash.delete "HTTP_X_FORWARDED_HOST"
  61. @request_hash['HTTP_HOST'] = "rubyonrails.org:8080"
  62. assert_equal "rubyonrails.org:8080", @request.host_with_port
  63. @request_hash['HTTP_X_FORWARDED_HOST'] = "www.firsthost.org, www.secondhost.org"
  64. assert_equal "www.secondhost.org", @request.host
  65. end
  66. def test_http_host_with_default_port_overrides_server_port
  67. @request_hash.delete "HTTP_X_FORWARDED_HOST"
  68. @request_hash['HTTP_HOST'] = "rubyonrails.org"
  69. assert_equal "rubyonrails.org", @request.host_with_port
  70. end
  71. def test_host_with_port_defaults_to_server_name_if_no_host_headers
  72. @request_hash.delete "HTTP_X_FORWARDED_HOST"
  73. @request_hash.delete "HTTP_HOST"
  74. assert_equal "glu.ttono.us:8007", @request.host_with_port
  75. end
  76. def test_host_with_port_falls_back_to_server_addr_if_necessary
  77. @request_hash.delete "HTTP_X_FORWARDED_HOST"
  78. @request_hash.delete "HTTP_HOST"
  79. @request_hash.delete "SERVER_NAME"
  80. assert_equal "207.7.108.53:8007", @request.host_with_port
  81. end
  82. def test_host_with_port_if_http_standard_port_is_specified
  83. @request_hash['HTTP_X_FORWARDED_HOST'] = "glu.ttono.us:80"
  84. assert_equal "glu.ttono.us", @request.host_with_port
  85. end
  86. def test_host_with_port_if_https_standard_port_is_specified
  87. @request_hash['HTTP_X_FORWARDED_PROTO'] = "https"
  88. @request_hash['HTTP_X_FORWARDED_HOST'] = "glu.ttono.us:443"
  89. assert_equal "glu.ttono.us", @request.host_with_port
  90. end
  91. def test_host_if_ipv6_reference
  92. @request_hash.delete "HTTP_X_FORWARDED_HOST"
  93. @request_hash['HTTP_HOST'] = "[2001:1234:5678:9abc:def0::dead:beef]"
  94. assert_equal "[2001:1234:5678:9abc:def0::dead:beef]", @request.host
  95. end
  96. def test_host_if_ipv6_reference_with_port
  97. @request_hash.delete "HTTP_X_FORWARDED_HOST"
  98. @request_hash['HTTP_HOST'] = "[2001:1234:5678:9abc:def0::dead:beef]:8008"
  99. assert_equal "[2001:1234:5678:9abc:def0::dead:beef]", @request.host
  100. end
  101. def test_cgi_environment_variables
  102. assert_equal "Basic", @request.auth_type
  103. assert_equal 0, @request.content_length
  104. assert_equal nil, @request.content_type
  105. assert_equal "CGI/1.1", @request.gateway_interface
  106. assert_equal "*/*", @request.accept
  107. assert_equal "UTF-8", @request.accept_charset
  108. assert_equal "gzip, deflate", @request.accept_encoding
  109. assert_equal "en", @request.accept_language
  110. assert_equal "no-cache, max-age=0", @request.cache_control
  111. assert_equal "googlebot", @request.from
  112. assert_equal "glu.ttono.us", @request.host
  113. assert_equal "trans", @request.negotiate
  114. assert_equal "no-cache", @request.pragma
  115. assert_equal "http://www.google.com/search?q=glu.ttono.us", @request.referer
  116. assert_equal "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en)", @request.user_agent
  117. assert_equal "/homepage/", @request.path_info
  118. assert_equal "/home/kevinc/sites/typo/public/homepage/", @request.path_translated
  119. assert_equal "", @request.query_string
  120. assert_equal "207.7.108.53", @request.remote_addr
  121. assert_equal "google.com", @request.remote_host
  122. assert_equal "kevin", @request.remote_ident
  123. assert_equal "kevin", @request.remote_user
  124. assert_equal :get, @request.request_method
  125. assert_equal "/dispatch.fcgi", @request.script_name
  126. assert_equal "glu.ttono.us:8007", @request.server_name
  127. assert_equal 8007, @request.server_port
  128. assert_equal "HTTP/1.1", @request.server_protocol
  129. assert_equal "lighttpd", @request.server_software
  130. end
  131. def test_cookie_syntax_resilience
  132. cookies = CGI::Cookie::parse(@request_hash["HTTP_COOKIE"]);
  133. assert_equal ["c84ace84796670c052c6ceb2451fb0f2"], cookies["_session_id"], cookies.inspect
  134. assert_equal ["yes"], cookies["is_admin"], cookies.inspect
  135. alt_cookies = CGI::Cookie::parse(@alt_cookie_fmt_request_hash["HTTP_COOKIE"]);
  136. assert_equal ["c84ace847,96670c052c6ceb2451fb0f2"], alt_cookies["_session_id"], alt_cookies.inspect
  137. assert_equal ["yes"], alt_cookies["is_admin"], alt_cookies.inspect
  138. end
  139. end
  140. class CgiRequestParamsParsingTest < BaseCgiTest
  141. def test_doesnt_break_when_content_type_has_charset
  142. data = 'flamenco=love'
  143. @request.env['CONTENT_LENGTH'] = data.length
  144. @request.env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded; charset=utf-8'
  145. @request.env['RAW_POST_DATA'] = data
  146. assert_equal({"flamenco"=> "love"}, @request.request_parameters)
  147. end
  148. def test_doesnt_interpret_request_uri_as_query_string_when_missing
  149. @request.env['REQUEST_URI'] = 'foo'
  150. assert_equal({}, @request.query_parameters)
  151. end
  152. end
  153. class CgiRequestNeedsRewoundTest < BaseCgiTest
  154. def test_body_should_be_rewound
  155. data = 'foo'
  156. fake_cgi = Struct.new(:env_table, :query_string, :stdinput).new(@request_hash, '', StringIO.new(data))
  157. fake_cgi.env_table['CONTENT_LENGTH'] = data.length
  158. fake_cgi.env_table['CONTENT_TYPE'] = 'application/x-www-form-urlencoded; charset=utf-8'
  159. # Read the request body by parsing params.
  160. request = ActionController::CgiRequest.new(fake_cgi)
  161. request.request_parameters
  162. # Should have rewound the body.
  163. assert_equal 0, request.body.pos
  164. end
  165. end
  166. uses_mocha 'CGI Response' do
  167. class CgiResponseTest < BaseCgiTest
  168. def setup
  169. super
  170. @cgi.expects(:header).returns("HTTP/1.0 200 OK\nContent-Type: text/html\n")
  171. @response = ActionController::CgiResponse.new(@cgi)
  172. @output = StringIO.new('')
  173. end
  174. def test_simple_output
  175. @response.body = "Hello, World!"
  176. @response.out(@output)
  177. assert_equal "HTTP/1.0 200 OK\nContent-Type: text/html\nHello, World!", @output.string
  178. end
  179. def test_head_request
  180. @cgi.env_table['REQUEST_METHOD'] = 'HEAD'
  181. @response.body = "Hello, World!"
  182. @response.out(@output)
  183. assert_equal "HTTP/1.0 200 OK\nContent-Type: text/html\n", @output.string
  184. end
  185. def test_streaming_block
  186. @response.body = Proc.new do |response, output|
  187. 5.times { |n| output.write(n) }
  188. end
  189. @response.out(@output)
  190. assert_equal "HTTP/1.0 200 OK\nContent-Type: text/html\n01234", @output.string
  191. end
  192. end
  193. end