PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/actionpack/test/controller/rack_test.rb

https://github.com/drd/rails
Ruby | 150 lines | 116 code | 30 blank | 4 comment | 0 complexity | 586b4f9503fad9074ab17fbf9135e0e4 MD5 | raw file
  1. require 'abstract_unit'
  2. require 'action_controller/rack_process'
  3. class BaseRackTest < Test::Unit::TestCase
  4. def setup
  5. @env = {"HTTP_MAX_FORWARDS"=>"10", "SERVER_NAME"=>"glu.ttono.us:8007", "FCGI_ROLE"=>"RESPONDER", "HTTP_X_FORWARDED_HOST"=>"glu.ttono.us", "HTTP_ACCEPT_ENCODING"=>"gzip, deflate", "HTTP_USER_AGENT"=>"Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/312.5.1 (KHTML, like Gecko) Safari/312.3.1", "PATH_INFO"=>"", "HTTP_ACCEPT_LANGUAGE"=>"en", "HTTP_HOST"=>"glu.ttono.us:8007", "SERVER_PROTOCOL"=>"HTTP/1.1", "REDIRECT_URI"=>"/dispatch.fcgi", "SCRIPT_NAME"=>"/dispatch.fcgi", "SERVER_ADDR"=>"207.7.108.53", "REMOTE_ADDR"=>"207.7.108.53", "SERVER_SOFTWARE"=>"lighttpd/1.4.5", "HTTP_COOKIE"=>"_session_id=c84ace84796670c052c6ceb2451fb0f2; is_admin=yes", "HTTP_X_FORWARDED_SERVER"=>"glu.ttono.us", "REQUEST_URI"=>"/admin", "DOCUMENT_ROOT"=>"/home/kevinc/sites/typo/public", "SERVER_PORT"=>"8007", "QUERY_STRING"=>"", "REMOTE_PORT"=>"63137", "GATEWAY_INTERFACE"=>"CGI/1.1", "HTTP_X_FORWARDED_FOR"=>"65.88.180.234", "HTTP_ACCEPT"=>"*/*", "SCRIPT_FILENAME"=>"/home/kevinc/sites/typo/public/dispatch.fcgi", "REDIRECT_STATUS"=>"200", "REQUEST_METHOD"=>"GET"}
  6. # some Nokia phone browsers omit the space after the semicolon separator.
  7. # some developers have grown accustomed to using comma in cookie values.
  8. @alt_cookie_fmt_request_hash = {"HTTP_COOKIE"=>"_session_id=c84ace847,96670c052c6ceb2451fb0f2;is_admin=yes"}
  9. @request = ActionController::RackRequest.new(@env)
  10. end
  11. def default_test; end
  12. end
  13. class RackRequestTest < BaseRackTest
  14. def test_proxy_request
  15. assert_equal 'glu.ttono.us', @request.host_with_port
  16. end
  17. def test_http_host
  18. @env.delete "HTTP_X_FORWARDED_HOST"
  19. @env['HTTP_HOST'] = "rubyonrails.org:8080"
  20. assert_equal "rubyonrails.org:8080", @request.host_with_port
  21. @env['HTTP_X_FORWARDED_HOST'] = "www.firsthost.org, www.secondhost.org"
  22. assert_equal "www.secondhost.org", @request.host
  23. end
  24. def test_http_host_with_default_port_overrides_server_port
  25. @env.delete "HTTP_X_FORWARDED_HOST"
  26. @env['HTTP_HOST'] = "rubyonrails.org"
  27. assert_equal "rubyonrails.org", @request.host_with_port
  28. end
  29. def test_host_with_port_defaults_to_server_name_if_no_host_headers
  30. @env.delete "HTTP_X_FORWARDED_HOST"
  31. @env.delete "HTTP_HOST"
  32. assert_equal "glu.ttono.us:8007", @request.host_with_port
  33. end
  34. def test_host_with_port_falls_back_to_server_addr_if_necessary
  35. @env.delete "HTTP_X_FORWARDED_HOST"
  36. @env.delete "HTTP_HOST"
  37. @env.delete "SERVER_NAME"
  38. assert_equal "207.7.108.53:8007", @request.host_with_port
  39. end
  40. def test_host_with_port_if_http_standard_port_is_specified
  41. @env['HTTP_X_FORWARDED_HOST'] = "glu.ttono.us:80"
  42. assert_equal "glu.ttono.us", @request.host_with_port
  43. end
  44. def test_host_with_port_if_https_standard_port_is_specified
  45. @env['HTTP_X_FORWARDED_PROTO'] = "https"
  46. @env['HTTP_X_FORWARDED_HOST'] = "glu.ttono.us:443"
  47. assert_equal "glu.ttono.us", @request.host_with_port
  48. end
  49. def test_host_if_ipv6_reference
  50. @env.delete "HTTP_X_FORWARDED_HOST"
  51. @env['HTTP_HOST'] = "[2001:1234:5678:9abc:def0::dead:beef]"
  52. assert_equal "[2001:1234:5678:9abc:def0::dead:beef]", @request.host
  53. end
  54. def test_host_if_ipv6_reference_with_port
  55. @env.delete "HTTP_X_FORWARDED_HOST"
  56. @env['HTTP_HOST'] = "[2001:1234:5678:9abc:def0::dead:beef]:8008"
  57. assert_equal "[2001:1234:5678:9abc:def0::dead:beef]", @request.host
  58. end
  59. def test_cookie_syntax_resilience
  60. cookies = CGI::Cookie::parse(@env["HTTP_COOKIE"]);
  61. assert_equal ["c84ace84796670c052c6ceb2451fb0f2"], cookies["_session_id"], cookies.inspect
  62. assert_equal ["yes"], cookies["is_admin"], cookies.inspect
  63. alt_cookies = CGI::Cookie::parse(@alt_cookie_fmt_request_hash["HTTP_COOKIE"]);
  64. assert_equal ["c84ace847,96670c052c6ceb2451fb0f2"], alt_cookies["_session_id"], alt_cookies.inspect
  65. assert_equal ["yes"], alt_cookies["is_admin"], alt_cookies.inspect
  66. end
  67. end
  68. class RackRequestParamsParsingTest < BaseRackTest
  69. def test_doesnt_break_when_content_type_has_charset
  70. data = 'flamenco=love'
  71. @request.env['CONTENT_LENGTH'] = data.length
  72. @request.env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded; charset=utf-8'
  73. @request.env['RAW_POST_DATA'] = data
  74. assert_equal({"flamenco"=> "love"}, @request.request_parameters)
  75. end
  76. def test_doesnt_interpret_request_uri_as_query_string_when_missing
  77. @request.env['REQUEST_URI'] = 'foo'
  78. assert_equal({}, @request.query_parameters)
  79. end
  80. end
  81. class RackRequestNeedsRewoundTest < BaseRackTest
  82. def test_body_should_be_rewound
  83. data = 'foo'
  84. @env['rack.input'] = StringIO.new(data)
  85. @env['CONTENT_LENGTH'] = data.length
  86. @env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded; charset=utf-8'
  87. # Read the request body by parsing params.
  88. request = ActionController::RackRequest.new(@env)
  89. request.request_parameters
  90. # Should have rewound the body.
  91. assert_equal 0, request.body.pos
  92. end
  93. end
  94. class RackResponseTest < BaseRackTest
  95. def setup
  96. super
  97. @response = ActionController::RackResponse.new
  98. @output = StringIO.new('')
  99. end
  100. def test_simple_output
  101. @response.body = "Hello, World!"
  102. status, headers, body = @response.out(@output)
  103. assert_equal 200, status
  104. assert_equal({"Content-Type" => "text/html", "Cache-Control" => "no-cache", "Set-Cookie" => ""}, headers)
  105. parts = []
  106. body.each { |part| parts << part }
  107. assert_equal ["Hello, World!"], parts
  108. end
  109. def test_streaming_block
  110. @response.body = Proc.new do |response, output|
  111. 5.times { |n| output.write(n) }
  112. end
  113. status, headers, body = @response.out(@output)
  114. assert_equal 200, status
  115. assert_equal({"Content-Type" => "text/html", "Cache-Control" => "no-cache", "Set-Cookie" => ""}, headers)
  116. parts = []
  117. body.each { |part| parts << part }
  118. assert_equal ["0", "1", "2", "3", "4"], parts
  119. end
  120. end