redirect_test.rb 7.1 KB
Newer Older
1
require 'abstract_unit'
D
Initial  
David Heinemeier Hansson 已提交
2

3 4 5 6
class WorkshopsController < ActionController::Base
end

class Workshop
J
Joshua Peek 已提交
7
  extend ActiveModel::Naming
8 9 10 11 12
  attr_accessor :id, :new_record

  def initialize(id, new_record)
    @id, @new_record = id, new_record
  end
13

14 15 16
  def new_record?
    @new_record
  end
17

18 19 20 21 22
  def to_s
    id.to_s
  end
end

23 24 25 26
class RedirectController < ActionController::Base
  def simple_redirect
    redirect_to :action => "hello_world"
  end
27

28
  def redirect_with_status
29
    redirect_to({:action => "hello_world", :status => 301})
30
  end
31 32 33

  def redirect_with_status_hash
    redirect_to({:action => "hello_world"}, {:status => 301})
34
  end
35

36
  def url_redirect_with_status
37
    redirect_to("http://www.example.com", :status => :moved_permanently)
38 39 40
  end

  def url_redirect_with_status_hash
41
    redirect_to("http://www.example.com", {:status => 301})
42
  end
43

44
  def relative_url_redirect_with_status
45
    redirect_to("/things/stuff", :status => :found)
46 47
  end

48 49
  def relative_url_redirect_with_status_hash
    redirect_to("/things/stuff", {:status => 301})
50 51 52 53
  end

  def redirect_to_back_with_status
    redirect_to :back, :status => 307
54 55
  end

56 57 58 59
  def host_redirect
    redirect_to :action => "other_host", :only_path => false, :host => 'other.test.host'
  end

60 61 62 63
  def module_redirect
    redirect_to :controller => 'module_test/module_redirect', :action => "hello_world"
  end

64 65 66 67 68
  def redirect_with_assigns
    @hello = "world"
    redirect_to :action => "hello_world"
  end

69 70 71 72 73 74 75 76
  def redirect_to_url
    redirect_to "http://www.rubyonrails.org/"
  end

  def redirect_to_url_with_unescaped_query_string
    redirect_to "http://dev.rubyonrails.org/query?status=new"
  end

77 78 79 80
  def redirect_to_url_with_complex_scheme
    redirect_to "x-test+scheme.complex:redirect"
  end

81 82 83 84
  def redirect_to_back
    redirect_to :back
  end

85 86 87 88 89 90 91 92
  def redirect_to_existing_record
    redirect_to Workshop.new(5, false)
  end

  def redirect_to_new_record
    redirect_to Workshop.new(5, true)
  end

93 94 95 96
  def redirect_to_nil
    redirect_to nil
  end

97
  def rescue_errors(e) raise e end
98

99
  def rescue_action(e) raise end
100

101 102 103 104 105
  protected
    def dashbord_url(id, message)
      url_for :action => "dashboard", :params => { "id" => id, "message" => message }
    end
end
D
Initial  
David Heinemeier Hansson 已提交
106

107 108
class RedirectTest < ActionController::TestCase
  tests RedirectController
D
Initial  
David Heinemeier Hansson 已提交
109 110

  def test_simple_redirect
111
    get :simple_redirect
J
Jeremy Kemper 已提交
112 113
    assert_response :redirect
    assert_equal "http://test.host/redirect/hello_world", redirect_to_url
D
Initial  
David Heinemeier Hansson 已提交
114 115
  end

116 117 118 119 120 121
  def test_redirect_with_no_status
    get :simple_redirect
    assert_response 302
    assert_equal "http://test.host/redirect/hello_world", redirect_to_url
  end

122 123 124 125 126
  def test_redirect_with_status
    get :redirect_with_status
    assert_response 301
    assert_equal "http://test.host/redirect/hello_world", redirect_to_url
  end
127

128
  def test_redirect_with_status_hash
129
    get :redirect_with_status_hash
130 131 132 133 134 135 136 137
    assert_response 301
    assert_equal "http://test.host/redirect/hello_world", redirect_to_url
  end

  def test_url_redirect_with_status
    get :url_redirect_with_status
    assert_response 301
    assert_equal "http://www.example.com", redirect_to_url
138 139 140 141
  end

  def test_url_redirect_with_status_hash
    get :url_redirect_with_status_hash
142 143 144
    assert_response 301
    assert_equal "http://www.example.com", redirect_to_url
  end
145

146 147 148

  def test_relative_url_redirect_with_status
    get :relative_url_redirect_with_status
149
    assert_response 302
150 151 152
    assert_equal "http://test.host/things/stuff", redirect_to_url
  end

153 154
  def test_relative_url_redirect_with_status_hash
    get :relative_url_redirect_with_status_hash
155 156 157 158 159 160 161 162 163
    assert_response 301
    assert_equal "http://test.host/things/stuff", redirect_to_url
  end

  def test_redirect_to_back_with_status
    @request.env["HTTP_REFERER"] = "http://www.example.com/coming/from"
    get :redirect_to_back_with_status
    assert_response 307
    assert_equal "http://www.example.com/coming/from", redirect_to_url
164 165
  end

166 167
  def test_simple_redirect_using_options
    get :host_redirect
J
Jeremy Kemper 已提交
168
    assert_response :redirect
169 170
    assert_redirected_to :action => "other_host", :only_path => false, :host => 'other.test.host'
  end
171 172 173

  def test_module_redirect
    get :module_redirect
J
Jeremy Kemper 已提交
174 175
    assert_response :redirect
    assert_redirected_to "http://test.host/module_test/module_redirect/hello_world"
176 177 178 179
  end

  def test_module_redirect_using_options
    get :module_redirect
J
Jeremy Kemper 已提交
180
    assert_response :redirect
181 182
    assert_redirected_to :controller => 'module_test/module_redirect', :action => 'hello_world'
  end
183 184 185

  def test_redirect_with_assigns
    get :redirect_with_assigns
J
Jeremy Kemper 已提交
186
    assert_response :redirect
187 188
    assert_equal "world", assigns["hello"]
  end
189

190 191 192 193 194 195 196 197 198 199 200 201
  def test_redirect_to_url
    get :redirect_to_url
    assert_response :redirect
    assert_redirected_to "http://www.rubyonrails.org/"
  end

  def test_redirect_to_url_with_unescaped_query_string
    get :redirect_to_url_with_unescaped_query_string
    assert_response :redirect
    assert_redirected_to "http://dev.rubyonrails.org/query?status=new"
  end

202 203 204 205 206 207
  def test_redirect_to_url_with_complex_scheme
    get :redirect_to_url_with_complex_scheme
    assert_response :redirect
    assert_equal "x-test+scheme.complex:redirect", redirect_to_url
  end

208 209 210
  def test_redirect_to_back
    @request.env["HTTP_REFERER"] = "http://www.example.com/coming/from"
    get :redirect_to_back
J
Jeremy Kemper 已提交
211 212
    assert_response :redirect
    assert_equal "http://www.example.com/coming/from", redirect_to_url
213
  end
214

215
  def test_redirect_to_back_with_no_referer
216
    assert_raise(ActionController::RedirectBackError) {
217 218 219 220
      @request.env["HTTP_REFERER"] = nil
      get :redirect_to_back
    }
  end
221

222 223 224 225 226
  def test_redirect_to_record
    ActionController::Routing::Routes.draw do |map|
      map.resources :workshops
      map.connect ':controller/:action/:id'
    end
227

228 229
    get :redirect_to_existing_record
    assert_equal "http://test.host/workshops/5", redirect_to_url
230
    assert_redirected_to Workshop.new(5, false)
231 232 233

    get :redirect_to_new_record
    assert_equal "http://test.host/workshops", redirect_to_url
234
    assert_redirected_to Workshop.new(5, true)
235
  end
236 237

  def test_redirect_to_nil
238
    assert_raise(ActionController::ActionControllerError) do
239 240 241
      get :redirect_to_nil
    end
  end
242
end
243 244 245 246 247 248 249 250

module ModuleTest
  class ModuleRedirectController < ::RedirectController
    def module_redirect
      redirect_to :controller => '/redirect', :action => "hello_world"
    end
  end

251 252
  class ModuleRedirectTest < ActionController::TestCase
    tests ModuleRedirectController
253

254 255
    def test_simple_redirect
      get :simple_redirect
J
Jeremy Kemper 已提交
256 257
      assert_response :redirect
      assert_equal "http://test.host/module_test/module_redirect/hello_world", redirect_to_url
258
    end
259

260 261
    def test_simple_redirect_using_options
      get :host_redirect
J
Jeremy Kemper 已提交
262
      assert_response :redirect
263 264 265 266 267
      assert_redirected_to :action => "other_host", :only_path => false, :host => 'other.test.host'
    end

    def test_module_redirect
      get :module_redirect
J
Jeremy Kemper 已提交
268 269
      assert_response :redirect
      assert_equal "http://test.host/redirect/hello_world", redirect_to_url
270 271 272 273
    end

    def test_module_redirect_using_options
      get :module_redirect
J
Jeremy Kemper 已提交
274
      assert_response :redirect
275
      assert_redirected_to :controller => '/redirect', :action => "hello_world"
276 277
    end
  end
278
end