~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/actionpack/test/controller/action_pack_assertions_test.rb

  • Committer: Richard Lee (Canonical)
  • Date: 2010-10-15 15:17:58 UTC
  • mfrom: (190.1.3 use-case-mapper)
  • Revision ID: richard.lee@canonical.com-20101015151758-wcvmfxrexsongf9d
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require 'abstract_unit'
2
 
 
3
 
# a controller class to facilitate the tests
4
 
class ActionPackAssertionsController < ActionController::Base
5
 
 
6
 
  # this does absolutely nothing
7
 
  def nothing() head :ok end
8
 
 
9
 
  # a standard template
10
 
  def hello_world() render :template => "test/hello_world"; end
11
 
 
12
 
  # a standard template
13
 
  def hello_xml_world() render :template => "test/hello_xml_world"; end
14
 
  
15
 
  # a standard partial
16
 
  def partial() render :partial => 'test/partial'; end
17
 
 
18
 
  # a redirect to an internal location
19
 
  def redirect_internal() redirect_to "/nothing"; end
20
 
 
21
 
  def redirect_to_action() redirect_to :action => "flash_me", :id => 1, :params => { "panda" => "fun" }; end
22
 
 
23
 
  def redirect_to_controller() redirect_to :controller => "elsewhere", :action => "flash_me"; end
24
 
 
25
 
  def redirect_to_controller_with_symbol() redirect_to :controller => :elsewhere, :action => :flash_me; end
26
 
 
27
 
  def redirect_to_path() redirect_to '/some/path' end
28
 
 
29
 
  def redirect_to_named_route() redirect_to route_one_url end
30
 
 
31
 
  # a redirect to an external location
32
 
  def redirect_external() redirect_to "http://www.rubyonrails.org"; end
33
 
 
34
 
  # a 404
35
 
  def response404() head '404 AWOL' end
36
 
 
37
 
  # a 500
38
 
  def response500() head '500 Sorry' end
39
 
 
40
 
  # a fictional 599
41
 
  def response599() head '599 Whoah!' end
42
 
 
43
 
  # putting stuff in the flash
44
 
  def flash_me
45
 
    flash['hello'] = 'my name is inigo montoya...'
46
 
    render :text => "Inconceivable!"
47
 
  end
48
 
 
49
 
  # we have a flash, but nothing is in it
50
 
  def flash_me_naked
51
 
    flash.clear
52
 
    render :text => "wow!"
53
 
  end
54
 
 
55
 
  # assign some template instance variables
56
 
  def assign_this
57
 
    @howdy = "ho"
58
 
    render :inline => "Mr. Henke"
59
 
  end
60
 
 
61
 
  def render_based_on_parameters
62
 
    render :text => "Mr. #{params[:name]}"
63
 
  end
64
 
 
65
 
  def render_url
66
 
    render :text => "<div>#{url_for(:action => 'flash_me', :only_path => true)}</div>"
67
 
  end
68
 
 
69
 
  def render_text_with_custom_content_type
70
 
    render :text => "Hello!", :content_type => Mime::RSS
71
 
  end
72
 
 
73
 
  # puts something in the session
74
 
  def session_stuffing
75
 
    session['xmas'] = 'turkey'
76
 
    render :text => "ho ho ho"
77
 
  end
78
 
 
79
 
  # raises exception on get requests
80
 
  def raise_on_get
81
 
    raise "get" if request.get?
82
 
    render :text => "request method: #{request.env['REQUEST_METHOD']}"
83
 
  end
84
 
 
85
 
  # raises exception on post requests
86
 
  def raise_on_post
87
 
    raise "post" if request.post?
88
 
    render :text => "request method: #{request.env['REQUEST_METHOD']}"
89
 
  end
90
 
 
91
 
  def get_valid_record
92
 
    @record = Class.new do
93
 
      def valid?
94
 
        true
95
 
      end
96
 
 
97
 
      def errors
98
 
        Class.new do
99
 
           def full_messages; []; end
100
 
        end.new
101
 
      end
102
 
 
103
 
    end.new
104
 
 
105
 
    render :nothing => true
106
 
  end
107
 
 
108
 
 
109
 
  def get_invalid_record
110
 
    @record = Class.new do
111
 
 
112
 
      def valid?
113
 
        false
114
 
      end
115
 
 
116
 
      def errors
117
 
        Class.new do
118
 
           def full_messages; ['...stuff...']; end
119
 
        end.new
120
 
      end
121
 
    end.new
122
 
 
123
 
    render :nothing => true
124
 
  end
125
 
 
126
 
  # 911
127
 
  def rescue_action(e) raise; end
128
 
end
129
 
 
130
 
# Used to test that assert_response includes the exception message
131
 
# in the failure message when an action raises and assert_response
132
 
# is expecting something other than an error.
133
 
class AssertResponseWithUnexpectedErrorController < ActionController::Base
134
 
  def index
135
 
    raise 'FAIL'
136
 
  end
137
 
 
138
 
  def show
139
 
    render :text => "Boom", :status => 500
140
 
  end
141
 
end
142
 
 
143
 
class UserController < ActionController::Base
144
 
end
145
 
 
146
 
module Admin
147
 
  class InnerModuleController < ActionController::Base
148
 
    def index
149
 
      render :nothing => true
150
 
    end
151
 
 
152
 
    def redirect_to_index
153
 
      redirect_to admin_inner_module_path
154
 
    end
155
 
 
156
 
    def redirect_to_absolute_controller
157
 
      redirect_to :controller => '/content'
158
 
    end
159
 
 
160
 
    def redirect_to_fellow_controller
161
 
      redirect_to :controller => 'user'
162
 
    end
163
 
 
164
 
    def redirect_to_top_level_named_route
165
 
      redirect_to top_level_url(:id => "foo")
166
 
    end
167
 
  end
168
 
end
169
 
 
170
 
# a test case to exercise the new capabilities TestRequest & TestResponse
171
 
class ActionPackAssertionsControllerTest < ActionController::TestCase
172
 
  # let's get this party started
173
 
  def setup
174
 
    ActionController::Routing::Routes.reload
175
 
    ActionController::Routing.use_controllers!(%w(action_pack_assertions admin/inner_module user content admin/user))
176
 
  end
177
 
 
178
 
  def teardown
179
 
    ActionController::Routing::Routes.reload
180
 
  end
181
 
 
182
 
  # -- assertion-based testing ------------------------------------------------
183
 
 
184
 
  def test_assert_tag_and_url_for
185
 
    get :render_url
186
 
    assert_tag :content => "/action_pack_assertions/flash_me"
187
 
  end
188
 
 
189
 
  # test the get method, make sure the request really was a get
190
 
  def test_get
191
 
    assert_raise(RuntimeError) { get :raise_on_get }
192
 
    get :raise_on_post
193
 
    assert_equal @response.body, 'request method: GET'
194
 
  end
195
 
 
196
 
  # test the get method, make sure the request really was a get
197
 
  def test_post
198
 
    assert_raise(RuntimeError) { post :raise_on_post }
199
 
    post :raise_on_get
200
 
    assert_equal @response.body, 'request method: POST'
201
 
  end
202
 
 
203
 
#   the following test fails because the request_method is now cached on the request instance
204
 
#   test the get/post switch within one test action
205
 
#   def test_get_post_switch
206
 
#     post :raise_on_get
207
 
#     assert_equal @response.body, 'request method: POST'
208
 
#     get :raise_on_post
209
 
#     assert_equal @response.body, 'request method: GET'
210
 
#     post :raise_on_get
211
 
#     assert_equal @response.body, 'request method: POST'
212
 
#     get :raise_on_post
213
 
#     assert_equal @response.body, 'request method: GET'
214
 
#   end
215
 
 
216
 
  # test the redirection to a named route
217
 
  def test_assert_redirect_to_named_route
218
 
    with_routing do |set|
219
 
      set.draw do |map|
220
 
        map.route_one 'route_one', :controller => 'action_pack_assertions', :action => 'nothing'
221
 
        map.connect   ':controller/:action/:id'
222
 
      end
223
 
      set.install_helpers
224
 
 
225
 
      process :redirect_to_named_route
226
 
      assert_redirected_to 'http://test.host/route_one'
227
 
      assert_redirected_to route_one_url
228
 
    end
229
 
  end
230
 
 
231
 
  def test_assert_redirect_to_named_route_failure
232
 
    with_routing do |set|
233
 
      set.draw do |map|
234
 
        map.route_one 'route_one', :controller => 'action_pack_assertions', :action => 'nothing', :id => 'one'
235
 
        map.route_two 'route_two', :controller => 'action_pack_assertions', :action => 'nothing', :id => 'two'
236
 
        map.connect   ':controller/:action/:id'
237
 
      end
238
 
      process :redirect_to_named_route
239
 
      assert_raise(ActiveSupport::TestCase::Assertion) do
240
 
        assert_redirected_to 'http://test.host/route_two'
241
 
      end
242
 
      assert_raise(ActiveSupport::TestCase::Assertion) do
243
 
        assert_redirected_to :controller => 'action_pack_assertions', :action => 'nothing', :id => 'two'
244
 
      end
245
 
      assert_raise(ActiveSupport::TestCase::Assertion) do
246
 
        assert_redirected_to route_two_url
247
 
      end
248
 
    end
249
 
  end
250
 
 
251
 
  def test_assert_redirect_to_nested_named_route
252
 
    with_routing do |set|
253
 
      set.draw do |map|
254
 
        map.admin_inner_module 'admin/inner_module', :controller => 'admin/inner_module', :action => 'index'
255
 
        map.connect            ':controller/:action/:id'
256
 
      end
257
 
      @controller = Admin::InnerModuleController.new
258
 
      process :redirect_to_index
259
 
      # redirection is <{"action"=>"index", "controller"=>"admin/admin/inner_module"}>
260
 
      assert_redirected_to admin_inner_module_path
261
 
    end
262
 
  end
263
 
 
264
 
  def test_assert_redirected_to_top_level_named_route_from_nested_controller
265
 
    with_routing do |set|
266
 
      set.draw do |map|
267
 
        map.top_level '/action_pack_assertions/:id', :controller => 'action_pack_assertions', :action => 'index'
268
 
        map.connect   ':controller/:action/:id'
269
 
      end
270
 
      @controller = Admin::InnerModuleController.new
271
 
      process :redirect_to_top_level_named_route
272
 
      # assert_redirected_to "http://test.host/action_pack_assertions/foo" would pass because of exact match early return
273
 
      assert_redirected_to "/action_pack_assertions/foo"
274
 
    end
275
 
  end
276
 
 
277
 
  def test_assert_redirected_to_top_level_named_route_with_same_controller_name_in_both_namespaces
278
 
    with_routing do |set|
279
 
      set.draw do |map|
280
 
        # this controller exists in the admin namespace as well which is the only difference from previous test
281
 
        map.top_level '/user/:id', :controller => 'user', :action => 'index'
282
 
        map.connect   ':controller/:action/:id'
283
 
      end
284
 
      @controller = Admin::InnerModuleController.new
285
 
      process :redirect_to_top_level_named_route
286
 
      # assert_redirected_to top_level_url('foo') would pass because of exact match early return
287
 
      assert_redirected_to top_level_path('foo')
288
 
    end
289
 
  end
290
 
 
291
 
  # -- standard request/response object testing --------------------------------
292
 
 
293
 
  # make sure that the template objects exist
294
 
  def test_template_objects_alive
295
 
    process :assign_this
296
 
    assert !@response.has_template_object?('hi')
297
 
    assert @response.has_template_object?('howdy')
298
 
  end
299
 
 
300
 
  # make sure we don't have template objects when we shouldn't
301
 
  def test_template_object_missing
302
 
    process :nothing
303
 
    assert_nil @response.template_objects['howdy']
304
 
  end
305
 
 
306
 
  # check the empty flashing
307
 
  def test_flash_me_naked
308
 
    process :flash_me_naked
309
 
    assert !@response.has_flash?
310
 
    assert !@response.has_flash_with_contents?
311
 
  end
312
 
 
313
 
  # check if we have flash objects
314
 
  def test_flash_haves
315
 
    process :flash_me
316
 
    assert @response.has_flash?
317
 
    assert @response.has_flash_with_contents?
318
 
    assert @response.has_flash_object?('hello')
319
 
  end
320
 
 
321
 
  # ensure we don't have flash objects
322
 
  def test_flash_have_nots
323
 
    process :nothing
324
 
    assert !@response.has_flash?
325
 
    assert !@response.has_flash_with_contents?
326
 
    assert_nil @response.flash['hello']
327
 
  end
328
 
 
329
 
  # check if we were rendered by a file-based template?
330
 
  def test_rendered_action
331
 
    process :nothing
332
 
    assert_nil @response.rendered[:template]
333
 
 
334
 
    process :hello_world
335
 
    assert @response.rendered[:template]
336
 
    assert 'hello_world', @response.rendered[:template].to_s
337
 
  end
338
 
  
339
 
  def test_assert_template_with_partial
340
 
    get :partial
341
 
    assert_template :partial => '_partial'
342
 
  end
343
 
  
344
 
  def test_assert_template_with_nil
345
 
    get :nothing
346
 
    assert_template nil
347
 
  end
348
 
  
349
 
  def test_assert_template_with_string
350
 
    get :hello_world
351
 
    assert_template 'hello_world'    
352
 
  end
353
 
  
354
 
  def test_assert_template_with_symbol
355
 
    get :hello_world
356
 
    assert_template :hello_world
357
 
  end
358
 
  
359
 
  def test_assert_template_with_bad_argument
360
 
    assert_raise(ArgumentError) { assert_template 1 }    
361
 
  end
362
 
 
363
 
  # check the redirection location
364
 
  def test_redirection_location
365
 
    process :redirect_internal
366
 
    assert_equal 'http://test.host/nothing', @response.redirect_url
367
 
 
368
 
    process :redirect_external
369
 
    assert_equal 'http://www.rubyonrails.org', @response.redirect_url
370
 
  end
371
 
 
372
 
  def test_no_redirect_url
373
 
    process :nothing
374
 
    assert_nil @response.redirect_url
375
 
  end
376
 
 
377
 
 
378
 
  # check server errors
379
 
  def test_server_error_response_code
380
 
    process :response500
381
 
    assert @response.server_error?
382
 
 
383
 
    process :response599
384
 
    assert @response.server_error?
385
 
 
386
 
    process :response404
387
 
    assert !@response.server_error?
388
 
  end
389
 
 
390
 
  # check a 404 response code
391
 
  def test_missing_response_code
392
 
    process :response404
393
 
    assert @response.missing?
394
 
  end
395
 
 
396
 
  # check client errors
397
 
  def test_client_error_response_code
398
 
    process :response404
399
 
    assert @response.client_error?
400
 
  end
401
 
 
402
 
  # check to see if our redirection matches a pattern
403
 
  def test_redirect_url_match
404
 
    process :redirect_external
405
 
    assert @response.redirect?
406
 
    assert @response.redirect_url_match?("rubyonrails")
407
 
    assert @response.redirect_url_match?(/rubyonrails/)
408
 
    assert !@response.redirect_url_match?("phpoffrails")
409
 
    assert !@response.redirect_url_match?(/perloffrails/)
410
 
  end
411
 
 
412
 
  # check for a redirection
413
 
  def test_redirection
414
 
    process :redirect_internal
415
 
    assert @response.redirect?
416
 
 
417
 
    process :redirect_external
418
 
    assert @response.redirect?
419
 
 
420
 
    process :nothing
421
 
    assert !@response.redirect?
422
 
  end
423
 
 
424
 
  # check a successful response code
425
 
  def test_successful_response_code
426
 
    process :nothing
427
 
    assert @response.success?
428
 
  end
429
 
 
430
 
  # a basic check to make sure we have a TestResponse object
431
 
  def test_has_response
432
 
    process :nothing
433
 
    assert_kind_of ActionController::TestResponse, @response
434
 
  end
435
 
 
436
 
  def test_render_based_on_parameters
437
 
    process :render_based_on_parameters, "name" => "David"
438
 
    assert_equal "Mr. David", @response.body
439
 
  end
440
 
 
441
 
 
442
 
  def test_assert_redirection_fails_with_incorrect_controller
443
 
    process :redirect_to_controller
444
 
    assert_raise(ActiveSupport::TestCase::Assertion) do
445
 
      assert_redirected_to :controller => "action_pack_assertions", :action => "flash_me"
446
 
    end
447
 
  end
448
 
 
449
 
  def test_assert_redirection_with_extra_controller_option
450
 
    get :redirect_to_action
451
 
    assert_redirected_to :controller => 'action_pack_assertions', :action => "flash_me", :id => 1, :params => { :panda => 'fun' }
452
 
  end
453
 
 
454
 
  def test_redirected_to_url_leading_slash
455
 
    process :redirect_to_path
456
 
    assert_redirected_to '/some/path'
457
 
  end
458
 
 
459
 
  def test_redirected_to_url_no_leadling_slash
460
 
    process :redirect_to_path
461
 
    assert_redirected_to 'some/path'
462
 
  end
463
 
 
464
 
  def test_redirected_to_url_full_url
465
 
    process :redirect_to_path
466
 
    assert_redirected_to 'http://test.host/some/path'
467
 
  end
468
 
 
469
 
  def test_assert_redirection_with_symbol
470
 
    process :redirect_to_controller_with_symbol
471
 
    assert_nothing_raised {
472
 
      assert_redirected_to :controller => "elsewhere", :action => "flash_me"
473
 
    }
474
 
    process :redirect_to_controller_with_symbol
475
 
    assert_nothing_raised {
476
 
      assert_redirected_to :controller => :elsewhere, :action => :flash_me
477
 
    }
478
 
  end
479
 
 
480
 
  def test_redirected_to_with_nested_controller
481
 
    @controller = Admin::InnerModuleController.new
482
 
    get :redirect_to_absolute_controller
483
 
    assert_redirected_to :controller => '/content'
484
 
 
485
 
    get :redirect_to_fellow_controller
486
 
    assert_redirected_to :controller => 'admin/user'
487
 
  end
488
 
 
489
 
  def test_assert_valid
490
 
    get :get_valid_record
491
 
    assert_deprecated { assert_valid assigns('record') }
492
 
  end
493
 
 
494
 
  def test_assert_valid_failing
495
 
    get :get_invalid_record
496
 
 
497
 
    begin
498
 
      assert_deprecated { assert_valid assigns('record') }
499
 
      assert false
500
 
    rescue ActiveSupport::TestCase::Assertion => e
501
 
    end
502
 
  end
503
 
 
504
 
  def test_assert_response_uses_exception_message
505
 
    @controller = AssertResponseWithUnexpectedErrorController.new
506
 
    get :index
507
 
    assert_response :success
508
 
    flunk 'Expected non-success response'
509
 
  rescue ActiveSupport::TestCase::Assertion => e
510
 
    assert e.message.include?('FAIL')
511
 
  end
512
 
 
513
 
  def test_assert_response_failure_response_with_no_exception
514
 
    @controller = AssertResponseWithUnexpectedErrorController.new
515
 
    get :show
516
 
    assert_response :success
517
 
    flunk 'Expected non-success response'
518
 
  rescue ActiveSupport::TestCase::Assertion
519
 
    # success
520
 
  rescue
521
 
    flunk "assert_response failed to handle failure response with missing, but optional, exception."
522
 
  end
523
 
end
524
 
 
525
 
class ActionPackHeaderTest < ActionController::TestCase
526
 
  tests ActionPackAssertionsController
527
 
 
528
 
  def test_rendering_xml_sets_content_type
529
 
    process :hello_xml_world
530
 
    assert_equal('application/xml; charset=utf-8', @response.headers['Content-Type'])
531
 
  end
532
 
 
533
 
  def test_rendering_xml_respects_content_type
534
 
    @response.headers['type'] = 'application/pdf'
535
 
    process :hello_xml_world
536
 
    assert_equal('application/pdf; charset=utf-8', @response.headers['Content-Type'])
537
 
  end
538
 
 
539
 
  def test_render_text_with_custom_content_type
540
 
    get :render_text_with_custom_content_type
541
 
    assert_equal 'application/rss+xml; charset=utf-8', @response.headers['Content-Type']
542
 
  end
543
 
end