ssl_requirement /test/url_for_test.rb

Language Ruby Lines 170
MD5 Hash cff4357778058c780f8cf992b3c0e4b1
Repository https://github.com/amishyn/ssl_requirement.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
$:.unshift(File.dirname(__FILE__) + '/../lib')

require 'rubygems'
require 'test/unit'
require 'action_controller'
require 'action_controller/test_process'

require "ssl_requirement"

# Show backtraces for deprecated behavior for quicker cleanup.
ActiveSupport::Deprecation.debug = true
ActionController::Base.logger = nil
ActionController::Routing::Routes.reload rescue nil

class UrlRewriterTest < Test::Unit::TestCase
  def setup
    @request = ActionController::TestRequest.new
    @params = {}
    @rewriter = ActionController::UrlRewriter.new(@request, @params)

    @ssl_host_override = "www.example.com:80443"
    @non_ssl_host_override = "www.example.com:8080"

    SslRequirement.ssl_host = nil
    SslRequirement.non_ssl_host = nil
    
    # puts @url_rewriter.to_s
  end

  def test_rewrite_secure_false
    SslRequirement.disable_ssl_check = false
    assert_equal('http://test.host/c/a',
      @rewriter.rewrite(:controller => 'c', :action => 'a', :secure => false)
    )
    assert_equal('/c/a',
      @rewriter.rewrite(:controller => 'c', :action => 'a', :secure => false, 
                        :only_path => true)
    )
    
    SslRequirement.disable_ssl_check = true
    assert_equal('http://test.host/c/a',
      @rewriter.rewrite(:controller => 'c', :action => 'a', :secure => false)
    )
    assert_equal('/c/a',
      @rewriter.rewrite(:controller => 'c', :action => 'a', :secure => false,
                        :only_path => true)
    )
  end
  
  def test_rewrite_secure_true
    SslRequirement.disable_ssl_check = false
    assert_equal('https://test.host/c/a',
      @rewriter.rewrite(:controller => 'c', :action => 'a', :secure => true)
    )
    assert_equal('https://test.host/c/a',
      @rewriter.rewrite(:controller => 'c', :action => 'a', :secure => true, :only_path => true)
    )
    
    SslRequirement.disable_ssl_check = true
    assert_equal('http://test.host/c/a',
      @rewriter.rewrite(:controller => 'c', :action => 'a', :secure => true)
    )
    assert_equal('/c/a',
      @rewriter.rewrite(:controller => 'c', :action => 'a', :secure => true, :only_path => true)
    )
  end
  
  def test_rewrite_secure_not_specified
    SslRequirement.disable_ssl_check = false
    assert_equal('http://test.host/c/a',
      @rewriter.rewrite(:controller => 'c', :action => 'a')
    )
    assert_equal('/c/a',
      @rewriter.rewrite(:controller => 'c', :action => 'a', :only_path => true)
    )
    
    SslRequirement.disable_ssl_check = true
    assert_equal('http://test.host/c/a',
      @rewriter.rewrite(:controller => 'c', :action => 'a')
    )
    assert_equal('/c/a',
      @rewriter.rewrite(:controller => 'c', :action => 'a', :only_path => true)
    )
  end

  # tests for ssl_host overriding

  def test_rewrite_secure_with_ssl_host
    SslRequirement.disable_ssl_check = false
    SslRequirement.ssl_host = @ssl_host_override
    assert_equal("https://#{@ssl_host_override}/c/a",
                 @rewriter.rewrite(:controller => 'c', :action => 'a', 
                                   :secure => true))
    assert_equal("https://#{@ssl_host_override}/c/a",
                 @rewriter.rewrite(:controller => 'c', :action => 'a',
                                   :secure => true, :only_path => true))
    SslRequirement.ssl_host = nil
  end

  def test_rewrite_non_secure_with_non_ssl_host
    SslRequirement.disable_ssl_check = false
    SslRequirement.non_ssl_host = @non_ssl_host_override

    # with secure option
    assert_equal("http://#{@non_ssl_host_override}/c/a",
                 @rewriter.rewrite(:controller => 'c', :action => 'a',
                                   :secure => false))
    assert_equal("/c/a",
                 @rewriter.rewrite(:controller => 'c', :action => 'a', 
                                   :secure => false, :only_path => true))

    # without secure option
    assert_equal("http://#{@non_ssl_host_override}/c/a",
                 @rewriter.rewrite(:controller => 'c', :action => 'a'))
    assert_equal("/c/a",
                 @rewriter.rewrite(:controller => 'c', :action => 'a', 
                                   :only_path => true))
    SslRequirement.non_ssl_host = nil
  end

  def test_rewrite_non_secure_with_non_ssl_host_disable_check
    SslRequirement.disable_ssl_check = true
    SslRequirement.non_ssl_host = @non_ssl_host_override

    # with secure option
    assert_equal("http://#{@non_ssl_host_override}/c/a",
                 @rewriter.rewrite(:controller => 'c', :action => 'a',
                                   :secure => false))
    assert_equal("/c/a",
                 @rewriter.rewrite(:controller => 'c', :action => 'a', 
                                   :secure => false, :only_path => true))

    # without secure option
    assert_equal("http://#{@non_ssl_host_override}/c/a",
                 @rewriter.rewrite(:controller => 'c', :action => 'a'))
    assert_equal("/c/a",
                 @rewriter.rewrite(:controller => 'c', :action => 'a', 
                                   :only_path => true))
    SslRequirement.non_ssl_host = nil
  end
  
  # tests for ssl_host overriding with Procs
  
  def test_rewrite_secure_with_ssl_host_proc
    SslRequirement.disable_ssl_check = false
    SslRequirement.ssl_host = Proc.new do
      @ssl_host_override
    end
    assert_equal("https://#{@ssl_host_override}/c/a",
                 @rewriter.rewrite(:controller => 'c', :action => 'a', 
                                   :secure => true))
    SslRequirement.ssl_host = nil
  end

  def test_rewrite_non_secure_with_non_ssl_host_proc
    SslRequirement.disable_ssl_check = false
    SslRequirement.non_ssl_host = Proc.new do
      @non_ssl_host_override
    end
    # with secure option
    assert_equal("http://#{@non_ssl_host_override}/c/a",
                 @rewriter.rewrite(:controller => 'c', :action => 'a',
                                   :secure => false))
    # without secure option
    assert_equal("http://#{@non_ssl_host_override}/c/a",
                 @rewriter.rewrite(:controller => 'c', :action => 'a'))
    SslRequirement.non_ssl_host = nil
  end
end
Back to Top