PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/test/url_rewriter_test.rb

https://github.com/bcurren/ssl_requirement
Ruby | 142 lines | 114 code | 22 blank | 6 comment | 0 complexity | dcd9003625d827ba05083ce2fe87998c MD5 | raw file
  1. $:.unshift(File.dirname(__FILE__) + '/../lib')
  2. require 'rubygems'
  3. require 'test/unit'
  4. require 'action_controller'
  5. require 'action_controller/test_process'
  6. require "ssl_requirement"
  7. # Show backtraces for deprecated behavior for quicker cleanup.
  8. ActiveSupport::Deprecation.debug = true
  9. ActionController::Base.logger = nil
  10. ActionController::Routing::Routes.reload rescue nil
  11. class UrlRewriterTest < Test::Unit::TestCase
  12. def setup
  13. @request = ActionController::TestRequest.new
  14. @params = {}
  15. @rewriter = ActionController::UrlRewriter.new(@request, @params)
  16. @ssl_host_override = "www.example.com:80443"
  17. @non_ssl_host_override = "www.example.com:8080"
  18. SslRequirement.ssl_host = nil
  19. SslRequirement.non_ssl_host = nil
  20. puts @url_rewriter.to_s
  21. end
  22. def test_rewrite_secure_false
  23. SslRequirement.disable_ssl_check = false
  24. assert_equal('http://test.host/c/a',
  25. @rewriter.rewrite(:controller => 'c', :action => 'a', :secure => false)
  26. )
  27. assert_equal('/c/a',
  28. @rewriter.rewrite(:controller => 'c', :action => 'a', :secure => false,
  29. :only_path => true)
  30. )
  31. SslRequirement.disable_ssl_check = true
  32. assert_equal('http://test.host/c/a',
  33. @rewriter.rewrite(:controller => 'c', :action => 'a', :secure => false)
  34. )
  35. assert_equal('/c/a',
  36. @rewriter.rewrite(:controller => 'c', :action => 'a', :secure => false,
  37. :only_path => true)
  38. )
  39. end
  40. def test_rewrite_secure_true
  41. SslRequirement.disable_ssl_check = false
  42. assert_equal('https://test.host/c/a',
  43. @rewriter.rewrite(:controller => 'c', :action => 'a', :secure => true)
  44. )
  45. assert_equal('https://test.host/c/a',
  46. @rewriter.rewrite(:controller => 'c', :action => 'a', :secure => true, :only_path => true)
  47. )
  48. SslRequirement.disable_ssl_check = true
  49. assert_equal('http://test.host/c/a',
  50. @rewriter.rewrite(:controller => 'c', :action => 'a', :secure => true)
  51. )
  52. assert_equal('/c/a',
  53. @rewriter.rewrite(:controller => 'c', :action => 'a', :secure => true, :only_path => true)
  54. )
  55. end
  56. def test_rewrite_secure_not_specified
  57. SslRequirement.disable_ssl_check = false
  58. assert_equal('http://test.host/c/a',
  59. @rewriter.rewrite(:controller => 'c', :action => 'a')
  60. )
  61. assert_equal('/c/a',
  62. @rewriter.rewrite(:controller => 'c', :action => 'a', :only_path => true)
  63. )
  64. SslRequirement.disable_ssl_check = true
  65. assert_equal('http://test.host/c/a',
  66. @rewriter.rewrite(:controller => 'c', :action => 'a')
  67. )
  68. assert_equal('/c/a',
  69. @rewriter.rewrite(:controller => 'c', :action => 'a', :only_path => true)
  70. )
  71. end
  72. # tests for ssl_host overriding
  73. def test_rewrite_secure_with_ssl_host
  74. SslRequirement.disable_ssl_check = false
  75. SslRequirement.ssl_host = @ssl_host_override
  76. assert_equal("https://#{@ssl_host_override}/c/a",
  77. @rewriter.rewrite(:controller => 'c', :action => 'a',
  78. :secure => true))
  79. assert_equal("https://#{@ssl_host_override}/c/a",
  80. @rewriter.rewrite(:controller => 'c', :action => 'a',
  81. :secure => true, :only_path => true))
  82. SslRequirement.ssl_host = nil
  83. end
  84. def test_rewrite_non_secure_with_non_ssl_host
  85. SslRequirement.disable_ssl_check = false
  86. SslRequirement.non_ssl_host = @non_ssl_host_override
  87. # with secure option
  88. assert_equal("http://#{@non_ssl_host_override}/c/a",
  89. @rewriter.rewrite(:controller => 'c', :action => 'a',
  90. :secure => false))
  91. assert_equal("/c/a",
  92. @rewriter.rewrite(:controller => 'c', :action => 'a',
  93. :secure => false, :only_path => true))
  94. # without secure option
  95. assert_equal("http://#{@non_ssl_host_override}/c/a",
  96. @rewriter.rewrite(:controller => 'c', :action => 'a'))
  97. assert_equal("/c/a",
  98. @rewriter.rewrite(:controller => 'c', :action => 'a',
  99. :only_path => true))
  100. SslRequirement.non_ssl_host = nil
  101. end
  102. def test_rewrite_non_secure_with_non_ssl_host_disable_check
  103. SslRequirement.disable_ssl_check = true
  104. SslRequirement.non_ssl_host = @non_ssl_host_override
  105. # with secure option
  106. assert_equal("http://#{@non_ssl_host_override}/c/a",
  107. @rewriter.rewrite(:controller => 'c', :action => 'a',
  108. :secure => false))
  109. assert_equal("/c/a",
  110. @rewriter.rewrite(:controller => 'c', :action => 'a',
  111. :secure => false, :only_path => true))
  112. # without secure option
  113. assert_equal("http://#{@non_ssl_host_override}/c/a",
  114. @rewriter.rewrite(:controller => 'c', :action => 'a'))
  115. assert_equal("/c/a",
  116. @rewriter.rewrite(:controller => 'c', :action => 'a',
  117. :only_path => true))
  118. SslRequirement.non_ssl_host = nil
  119. end
  120. end