request_test.rb 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. require 'test_helper'
  2. require 'dav4rack/request'
  3. class RequestTest < Minitest::Test
  4. def request(env = {}, options = {})
  5. env = {
  6. 'HTTP_HOST' => 'localhost',
  7. 'REMOTE_USER' => 'user',
  8. 'rack.url_scheme' => 'https',
  9. 'SERVER_PORT' => 443,
  10. }.merge(env)
  11. DAV4Rack::Request.new(env, options)
  12. end
  13. def test_should_have_unescaped_path
  14. assert_equal '/fo o/a', request('PATH_INFO' => '/fo%20o/a').unescaped_path
  15. assert_equal '/fo o/a', request('PATH_INFO' => '/fo%20o/a').unescaped_path_info
  16. assert_equal '/fo o/a/', request('PATH_INFO' => '/fo%20o/a/').unescaped_path_info
  17. end
  18. def test_should_expand_pathinfo
  19. assert_equal '/a', request('PATH_INFO' => '/foo/../a').unescaped_path
  20. assert_equal '/a', request('PATH_INFO' => '/foo/../a').unescaped_path_info
  21. r = request('PATH_INFO' => '/foo/../../../a', 'SCRIPT_NAME' => '/redmine')
  22. assert_equal '/redmine/a', r.unescaped_path
  23. assert_equal '/a', r.unescaped_path_info
  24. end
  25. def test_should_expand_path_with_unescaped_special_chars
  26. assert_equal '/a [foo].pdf', request('PATH_INFO' => '/foo/../a%20[foo].pdf').unescaped_path
  27. assert_equal '/a f#o.pdf', request('PATH_INFO' => '/foo/../a%20f#o.pdf').unescaped_path
  28. end
  29. def test_should_normalize_path_with_double_slashes
  30. assert_equal '/foo', request('PATH_INFO' => '/').expand_path('//foo')
  31. assert_equal '/foo/', request('PATH_INFO' => '/').expand_path('//foo/')
  32. assert_equal '/foo/a', request('PATH_INFO' => '/').expand_path('//foo/a')
  33. end
  34. def test_should_handle_script_name
  35. r = request('PATH_INFO' => '/fo%20o/a', 'SCRIPT_NAME' => '/redmine')
  36. assert_equal '/redmine/fo o/a', r.unescaped_path
  37. assert_equal '/fo o/a', r.unescaped_path_info
  38. assert_equal "/foo bar", r.path_info_for("/redmine/foo%20bar")
  39. assert_nil r.path_info_for("/redmine/foo%20bar", script_name: '/other')
  40. assert_equal "/foo bar", r.path_info_for("/other/foo%20bar", script_name: '/other')
  41. end
  42. def test_should_parse_depth_header
  43. {
  44. infinity: [nil, 'foo', '', 'infinity', '2'],
  45. 1 => ['1'],
  46. 0 => ['0']
  47. }.each do |expected, values|
  48. values.each do |value|
  49. assert_equal expected, request('HTTP_DEPTH' => value).depth, "#{value} should result in #{expected}"
  50. end
  51. end
  52. end
  53. def test_should_generate_path
  54. r = request('PATH_INFO' => '/', 'SCRIPT_NAME' => '/redmine')
  55. assert_equal '/redmine/fo%20o%5B%23.pdf',
  56. r.path_for('/fo o[#.pdf')
  57. end
  58. def test_should_generate_url
  59. r = request('PATH_INFO' => '/', 'SCRIPT_NAME' => '/redmine')
  60. assert_equal 'https://localhost:443/redmine/fo%20o%5B%23.pdf',
  61. r.url_for('/fo o[#.pdf')
  62. end
  63. end