actionpack 2.3.2 → 2.3.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of actionpack might be problematic. Click here for more details.

Files changed (117) hide show
  1. data/CHANGELOG +5 -0
  2. data/Rakefile +10 -9
  3. data/lib/action_controller.rb +2 -7
  4. data/lib/action_controller/assertions/response_assertions.rb +11 -3
  5. data/lib/action_controller/base.rb +7 -2
  6. data/lib/action_controller/caching.rb +1 -1
  7. data/lib/action_controller/caching/actions.rb +9 -1
  8. data/lib/action_controller/caching/sweeper.rb +45 -0
  9. data/lib/action_controller/caching/sweeping.rb +0 -42
  10. data/lib/action_controller/failsafe.rb +40 -6
  11. data/lib/action_controller/flash.rb +11 -3
  12. data/lib/action_controller/http_authentication.rb +5 -2
  13. data/lib/action_controller/integration.rb +5 -12
  14. data/lib/action_controller/middlewares.rb +0 -1
  15. data/lib/action_controller/reloader.rb +34 -3
  16. data/lib/action_controller/request.rb +6 -2
  17. data/lib/action_controller/response.rb +2 -2
  18. data/lib/action_controller/routing/route_set.rb +2 -1
  19. data/lib/action_controller/streaming.rb +1 -1
  20. data/lib/action_controller/test_process.rb +9 -1
  21. data/lib/action_pack/version.rb +1 -1
  22. data/lib/action_view/helpers.rb +1 -1
  23. data/lib/action_view/helpers/asset_tag_helper.rb +21 -9
  24. data/lib/action_view/helpers/date_helper.rb +2 -2
  25. data/lib/action_view/helpers/form_helper.rb +25 -12
  26. data/lib/action_view/helpers/form_options_helper.rb +2 -0
  27. data/lib/action_view/helpers/form_tag_helper.rb +10 -4
  28. data/lib/action_view/helpers/number_helper.rb +1 -1
  29. data/lib/action_view/helpers/prototype_helper.rb +7 -7
  30. data/lib/action_view/helpers/scriptaculous_helper.rb +5 -5
  31. data/lib/action_view/helpers/text_helper.rb +3 -3
  32. data/lib/action_view/helpers/url_helper.rb +1 -1
  33. data/lib/action_view/paths.rb +1 -1
  34. data/lib/action_view/template.rb +22 -33
  35. data/test/abstract_unit.rb +1 -1
  36. data/test/activerecord/active_record_store_test.rb +3 -3
  37. data/test/controller/action_pack_assertions_test.rb +27 -0
  38. data/test/controller/caching_test.rb +39 -0
  39. data/test/controller/cookie_test.rb +10 -0
  40. data/test/controller/dispatcher_test.rb +9 -7
  41. data/test/controller/failsafe_test.rb +60 -0
  42. data/test/controller/filter_params_test.rb +2 -1
  43. data/test/controller/flash_test.rb +6 -1
  44. data/test/controller/http_digest_authentication_test.rb +34 -3
  45. data/test/controller/integration_test.rb +31 -3
  46. data/test/controller/redirect_test.rb +4 -1
  47. data/test/controller/reloader_test.rb +97 -0
  48. data/test/controller/render_test.rb +19 -9
  49. data/test/controller/request/multipart_params_parsing_test.rb +1 -62
  50. data/test/controller/request/test_request_test.rb +35 -0
  51. data/test/controller/request/url_encoded_params_parsing_test.rb +0 -38
  52. data/test/controller/request_test.rb +218 -230
  53. data/test/controller/resources_test.rb +8 -0
  54. data/test/controller/routing_test.rb +21 -0
  55. data/test/controller/send_file_test.rb +1 -1
  56. data/test/controller/session/cookie_store_test.rb +9 -32
  57. data/test/controller/test_test.rb +8 -0
  58. data/test/fixtures/failsafe/500.html +1 -0
  59. data/test/template/active_record_helper_test.rb +1 -1
  60. data/test/template/asset_tag_helper_test.rb +46 -0
  61. data/test/template/form_helper_test.rb +117 -6
  62. data/test/template/form_options_helper_test.rb +22 -0
  63. data/test/template/form_tag_helper_test.rb +23 -6
  64. data/test/template/prototype_helper_test.rb +11 -11
  65. data/test/template/template_test.rb +32 -0
  66. metadata +20 -63
  67. data/lib/action_controller/rewindable_input.rb +0 -28
  68. data/lib/action_controller/vendor/rack-1.0/rack.rb +0 -89
  69. data/lib/action_controller/vendor/rack-1.0/rack/adapter/camping.rb +0 -22
  70. data/lib/action_controller/vendor/rack-1.0/rack/auth/abstract/handler.rb +0 -37
  71. data/lib/action_controller/vendor/rack-1.0/rack/auth/abstract/request.rb +0 -37
  72. data/lib/action_controller/vendor/rack-1.0/rack/auth/basic.rb +0 -58
  73. data/lib/action_controller/vendor/rack-1.0/rack/auth/digest/md5.rb +0 -124
  74. data/lib/action_controller/vendor/rack-1.0/rack/auth/digest/nonce.rb +0 -51
  75. data/lib/action_controller/vendor/rack-1.0/rack/auth/digest/params.rb +0 -55
  76. data/lib/action_controller/vendor/rack-1.0/rack/auth/digest/request.rb +0 -40
  77. data/lib/action_controller/vendor/rack-1.0/rack/auth/openid.rb +0 -480
  78. data/lib/action_controller/vendor/rack-1.0/rack/builder.rb +0 -63
  79. data/lib/action_controller/vendor/rack-1.0/rack/cascade.rb +0 -36
  80. data/lib/action_controller/vendor/rack-1.0/rack/chunked.rb +0 -49
  81. data/lib/action_controller/vendor/rack-1.0/rack/commonlogger.rb +0 -61
  82. data/lib/action_controller/vendor/rack-1.0/rack/conditionalget.rb +0 -45
  83. data/lib/action_controller/vendor/rack-1.0/rack/content_length.rb +0 -29
  84. data/lib/action_controller/vendor/rack-1.0/rack/content_type.rb +0 -23
  85. data/lib/action_controller/vendor/rack-1.0/rack/deflater.rb +0 -85
  86. data/lib/action_controller/vendor/rack-1.0/rack/directory.rb +0 -153
  87. data/lib/action_controller/vendor/rack-1.0/rack/file.rb +0 -88
  88. data/lib/action_controller/vendor/rack-1.0/rack/handler.rb +0 -48
  89. data/lib/action_controller/vendor/rack-1.0/rack/handler/cgi.rb +0 -61
  90. data/lib/action_controller/vendor/rack-1.0/rack/handler/evented_mongrel.rb +0 -8
  91. data/lib/action_controller/vendor/rack-1.0/rack/handler/fastcgi.rb +0 -89
  92. data/lib/action_controller/vendor/rack-1.0/rack/handler/lsws.rb +0 -55
  93. data/lib/action_controller/vendor/rack-1.0/rack/handler/mongrel.rb +0 -84
  94. data/lib/action_controller/vendor/rack-1.0/rack/handler/scgi.rb +0 -59
  95. data/lib/action_controller/vendor/rack-1.0/rack/handler/swiftiplied_mongrel.rb +0 -8
  96. data/lib/action_controller/vendor/rack-1.0/rack/handler/thin.rb +0 -18
  97. data/lib/action_controller/vendor/rack-1.0/rack/handler/webrick.rb +0 -67
  98. data/lib/action_controller/vendor/rack-1.0/rack/head.rb +0 -19
  99. data/lib/action_controller/vendor/rack-1.0/rack/lint.rb +0 -462
  100. data/lib/action_controller/vendor/rack-1.0/rack/lobster.rb +0 -65
  101. data/lib/action_controller/vendor/rack-1.0/rack/lock.rb +0 -16
  102. data/lib/action_controller/vendor/rack-1.0/rack/methodoverride.rb +0 -27
  103. data/lib/action_controller/vendor/rack-1.0/rack/mime.rb +0 -204
  104. data/lib/action_controller/vendor/rack-1.0/rack/mock.rb +0 -160
  105. data/lib/action_controller/vendor/rack-1.0/rack/recursive.rb +0 -57
  106. data/lib/action_controller/vendor/rack-1.0/rack/reloader.rb +0 -64
  107. data/lib/action_controller/vendor/rack-1.0/rack/request.rb +0 -241
  108. data/lib/action_controller/vendor/rack-1.0/rack/response.rb +0 -179
  109. data/lib/action_controller/vendor/rack-1.0/rack/session/abstract/id.rb +0 -142
  110. data/lib/action_controller/vendor/rack-1.0/rack/session/cookie.rb +0 -91
  111. data/lib/action_controller/vendor/rack-1.0/rack/session/memcache.rb +0 -109
  112. data/lib/action_controller/vendor/rack-1.0/rack/session/pool.rb +0 -100
  113. data/lib/action_controller/vendor/rack-1.0/rack/showexceptions.rb +0 -349
  114. data/lib/action_controller/vendor/rack-1.0/rack/showstatus.rb +0 -106
  115. data/lib/action_controller/vendor/rack-1.0/rack/static.rb +0 -38
  116. data/lib/action_controller/vendor/rack-1.0/rack/urlmap.rb +0 -55
  117. data/lib/action_controller/vendor/rack-1.0/rack/utils.rb +0 -392
@@ -3,7 +3,6 @@ require 'abstract_unit'
3
3
  class RequestTest < ActiveSupport::TestCase
4
4
  def setup
5
5
  ActionController::Base.relative_url_root = nil
6
- @request = ActionController::TestRequest.new
7
6
  end
8
7
 
9
8
  def teardown
@@ -11,60 +10,52 @@ class RequestTest < ActiveSupport::TestCase
11
10
  end
12
11
 
13
12
  def test_remote_ip
14
- assert_equal '0.0.0.0', @request.remote_ip
13
+ request = stub_request 'REMOTE_ADDR' => '1.2.3.4'
14
+ assert_equal '1.2.3.4', request.remote_ip
15
15
 
16
- @request.remote_addr = '1.2.3.4'
17
- assert_equal '1.2.3.4', @request.remote_ip
16
+ request = stub_request 'REMOTE_ADDR' => '1.2.3.4,3.4.5.6'
17
+ assert_equal '1.2.3.4', request.remote_ip
18
18
 
19
- @request.remote_addr = '1.2.3.4,3.4.5.6'
20
- assert_equal '1.2.3.4', @request.remote_ip
19
+ request = stub_request 'REMOTE_ADDR' => '1.2.3.4',
20
+ 'HTTP_X_FORWARDED_FOR' => '3.4.5.6'
21
+ assert_equal '1.2.3.4', request.remote_ip
21
22
 
22
- @request.env['HTTP_CLIENT_IP'] = '2.3.4.5'
23
- assert_equal '1.2.3.4', @request.remote_ip
23
+ request = stub_request 'REMOTE_ADDR' => '127.0.0.1',
24
+ 'HTTP_X_FORWARDED_FOR' => '3.4.5.6'
25
+ assert_equal '3.4.5.6', request.remote_ip
24
26
 
25
- @request.remote_addr = '192.168.0.1'
26
- assert_equal '2.3.4.5', @request.remote_ip
27
- @request.env.delete 'HTTP_CLIENT_IP'
27
+ request = stub_request 'HTTP_X_FORWARDED_FOR' => 'unknown,3.4.5.6'
28
+ assert_equal '3.4.5.6', request.remote_ip
28
29
 
29
- @request.remote_addr = '1.2.3.4'
30
- @request.env['HTTP_X_FORWARDED_FOR'] = '3.4.5.6'
31
- assert_equal '1.2.3.4', @request.remote_ip
30
+ request = stub_request 'HTTP_X_FORWARDED_FOR' => '172.16.0.1,3.4.5.6'
31
+ assert_equal '3.4.5.6', request.remote_ip
32
32
 
33
- @request.remote_addr = '127.0.0.1'
34
- @request.env['HTTP_X_FORWARDED_FOR'] = '3.4.5.6'
35
- assert_equal '3.4.5.6', @request.remote_ip
33
+ request = stub_request 'HTTP_X_FORWARDED_FOR' => '192.168.0.1,3.4.5.6'
34
+ assert_equal '3.4.5.6', request.remote_ip
36
35
 
37
- @request.env['HTTP_X_FORWARDED_FOR'] = 'unknown,3.4.5.6'
38
- assert_equal '3.4.5.6', @request.remote_ip
36
+ request = stub_request 'HTTP_X_FORWARDED_FOR' => '10.0.0.1,3.4.5.6'
37
+ assert_equal '3.4.5.6', request.remote_ip
39
38
 
40
- @request.env['HTTP_X_FORWARDED_FOR'] = '172.16.0.1,3.4.5.6'
41
- assert_equal '3.4.5.6', @request.remote_ip
39
+ request = stub_request 'HTTP_X_FORWARDED_FOR' => '10.0.0.1, 10.0.0.1, 3.4.5.6'
40
+ assert_equal '3.4.5.6', request.remote_ip
42
41
 
43
- @request.env['HTTP_X_FORWARDED_FOR'] = '192.168.0.1,3.4.5.6'
44
- assert_equal '3.4.5.6', @request.remote_ip
42
+ request = stub_request 'HTTP_X_FORWARDED_FOR' => '127.0.0.1,3.4.5.6'
43
+ assert_equal '3.4.5.6', request.remote_ip
45
44
 
46
- @request.env['HTTP_X_FORWARDED_FOR'] = '10.0.0.1,3.4.5.6'
47
- assert_equal '3.4.5.6', @request.remote_ip
45
+ request = stub_request 'HTTP_X_FORWARDED_FOR' => 'unknown,192.168.0.1'
46
+ assert_equal 'unknown', request.remote_ip
48
47
 
49
- @request.env['HTTP_X_FORWARDED_FOR'] = '10.0.0.1, 10.0.0.1, 3.4.5.6'
50
- assert_equal '3.4.5.6', @request.remote_ip
48
+ request = stub_request 'HTTP_X_FORWARDED_FOR' => '9.9.9.9, 3.4.5.6, 10.0.0.1, 172.31.4.4'
49
+ assert_equal '3.4.5.6', request.remote_ip
51
50
 
52
- @request.env['HTTP_X_FORWARDED_FOR'] = '127.0.0.1,3.4.5.6'
53
- assert_equal '3.4.5.6', @request.remote_ip
54
-
55
- @request.env['HTTP_X_FORWARDED_FOR'] = 'unknown,192.168.0.1'
56
- assert_equal 'unknown', @request.remote_ip
57
-
58
- @request.env['HTTP_X_FORWARDED_FOR'] = '9.9.9.9, 3.4.5.6, 10.0.0.1, 172.31.4.4'
59
- assert_equal '3.4.5.6', @request.remote_ip
60
-
61
- @request.env['HTTP_CLIENT_IP'] = '8.8.8.8'
51
+ request = stub_request 'HTTP_X_FORWARDED_FOR' => '1.1.1.1',
52
+ 'HTTP_CLIENT_IP' => '2.2.2.2'
62
53
  e = assert_raise(ActionController::ActionControllerError) {
63
- @request.remote_ip
54
+ request.remote_ip
64
55
  }
65
56
  assert_match /IP spoofing attack/, e.message
66
- assert_match /HTTP_X_FORWARDED_FOR="9.9.9.9, 3.4.5.6, 10.0.0.1, 172.31.4.4"/, e.message
67
- assert_match /HTTP_CLIENT_IP="8.8.8.8"/, e.message
57
+ assert_match /HTTP_X_FORWARDED_FOR="1.1.1.1"/, e.message
58
+ assert_match /HTTP_CLIENT_IP="2.2.2.2"/, e.message
68
59
 
69
60
  # turn IP Spoofing detection off.
70
61
  # This is useful for sites that are aimed at non-IP clients. The typical
@@ -72,336 +63,333 @@ class RequestTest < ActiveSupport::TestCase
72
63
  # leap of faith to assume that their proxies are ever going to set the
73
64
  # HTTP_CLIENT_IP/HTTP_X_FORWARDED_FOR headers properly.
74
65
  ActionController::Base.ip_spoofing_check = false
75
- assert_equal('8.8.8.8', @request.remote_ip)
66
+ request = stub_request 'HTTP_X_FORWARDED_FOR' => '1.1.1.1',
67
+ 'HTTP_CLIENT_IP' => '2.2.2.2'
68
+ assert_equal '2.2.2.2', request.remote_ip
76
69
  ActionController::Base.ip_spoofing_check = true
77
70
 
78
- @request.env['HTTP_X_FORWARDED_FOR'] = '8.8.8.8, 9.9.9.9'
79
- assert_equal '8.8.8.8', @request.remote_ip
80
-
81
- @request.env.delete 'HTTP_CLIENT_IP'
82
- @request.env.delete 'HTTP_X_FORWARDED_FOR'
71
+ request = stub_request 'HTTP_X_FORWARDED_FOR' => '8.8.8.8, 9.9.9.9'
72
+ assert_equal '9.9.9.9', request.remote_ip
83
73
  end
84
74
 
85
75
  def test_domains
86
- @request.host = "www.rubyonrails.org"
87
- assert_equal "rubyonrails.org", @request.domain
88
-
89
- @request.host = "www.rubyonrails.co.uk"
90
- assert_equal "rubyonrails.co.uk", @request.domain(2)
76
+ request = stub_request 'HTTP_HOST' => 'www.rubyonrails.org'
77
+ assert_equal "rubyonrails.org", request.domain
91
78
 
92
- @request.host = "192.168.1.200"
93
- assert_nil @request.domain
79
+ request = stub_request 'HTTP_HOST' => "www.rubyonrails.co.uk"
80
+ assert_equal "rubyonrails.co.uk", request.domain(2)
94
81
 
95
- @request.host = "foo.192.168.1.200"
96
- assert_nil @request.domain
82
+ request = stub_request 'HTTP_HOST' => "192.168.1.200"
83
+ assert_nil request.domain
97
84
 
98
- @request.host = "192.168.1.200.com"
99
- assert_equal "200.com", @request.domain
85
+ request = stub_request 'HTTP_HOST' => "foo.192.168.1.200"
86
+ assert_nil request.domain
100
87
 
101
- @request.host = nil
102
- assert_nil @request.domain
88
+ request = stub_request 'HTTP_HOST' => "192.168.1.200.com"
89
+ assert_equal "200.com", request.domain
103
90
  end
104
91
 
105
92
  def test_subdomains
106
- @request.host = "www.rubyonrails.org"
107
- assert_equal %w( www ), @request.subdomains
93
+ request = stub_request 'HTTP_HOST' => "www.rubyonrails.org"
94
+ assert_equal %w( www ), request.subdomains
108
95
 
109
- @request.host = "www.rubyonrails.co.uk"
110
- assert_equal %w( www ), @request.subdomains(2)
96
+ request = stub_request 'HTTP_HOST' => "www.rubyonrails.co.uk"
97
+ assert_equal %w( www ), request.subdomains(2)
111
98
 
112
- @request.host = "dev.www.rubyonrails.co.uk"
113
- assert_equal %w( dev www ), @request.subdomains(2)
99
+ request = stub_request 'HTTP_HOST' => "dev.www.rubyonrails.co.uk"
100
+ assert_equal %w( dev www ), request.subdomains(2)
114
101
 
115
- @request.host = "foobar.foobar.com"
116
- assert_equal %w( foobar ), @request.subdomains
102
+ request = stub_request 'HTTP_HOST' => "foobar.foobar.com"
103
+ assert_equal %w( foobar ), request.subdomains
117
104
 
118
- @request.host = "192.168.1.200"
119
- assert_equal [], @request.subdomains
105
+ request = stub_request 'HTTP_HOST' => "192.168.1.200"
106
+ assert_equal [], request.subdomains
120
107
 
121
- @request.host = "foo.192.168.1.200"
122
- assert_equal [], @request.subdomains
108
+ request = stub_request 'HTTP_HOST' => "foo.192.168.1.200"
109
+ assert_equal [], request.subdomains
123
110
 
124
- @request.host = "192.168.1.200.com"
125
- assert_equal %w( 192 168 1 ), @request.subdomains
111
+ request = stub_request 'HTTP_HOST' => "192.168.1.200.com"
112
+ assert_equal %w( 192 168 1 ), request.subdomains
126
113
 
127
- @request.host = nil
128
- assert_equal [], @request.subdomains
114
+ request = stub_request 'HTTP_HOST' => nil
115
+ assert_equal [], request.subdomains
129
116
  end
130
117
 
131
118
  def test_port_string
132
- @request.port = 80
133
- assert_equal "", @request.port_string
119
+ request = stub_request 'HTTP_HOST' => 'www.example.org:80'
120
+ assert_equal "", request.port_string
134
121
 
135
- @request.port = 8080
136
- assert_equal ":8080", @request.port_string
122
+ request = stub_request 'HTTP_HOST' => 'www.example.org:8080'
123
+ assert_equal ":8080", request.port_string
137
124
  end
138
125
 
139
126
  def test_request_uri
140
- @request.env['SERVER_SOFTWARE'] = 'Apache 42.342.3432'
127
+ request = stub_request 'REQUEST_URI' => "http://www.rubyonrails.org/path/of/some/uri?mapped=1"
128
+ assert_equal "/path/of/some/uri?mapped=1", request.request_uri
129
+ assert_equal "/path/of/some/uri", request.path
141
130
 
142
- @request.set_REQUEST_URI "http://www.rubyonrails.org/path/of/some/uri?mapped=1"
143
- assert_equal "/path/of/some/uri?mapped=1", @request.request_uri
144
- assert_equal "/path/of/some/uri", @request.path
131
+ request = stub_request 'REQUEST_URI' => "http://www.rubyonrails.org/path/of/some/uri"
132
+ assert_equal "/path/of/some/uri", request.request_uri
133
+ assert_equal "/path/of/some/uri", request.path
145
134
 
146
- @request.set_REQUEST_URI "http://www.rubyonrails.org/path/of/some/uri"
147
- assert_equal "/path/of/some/uri", @request.request_uri
148
- assert_equal "/path/of/some/uri", @request.path
135
+ request = stub_request 'REQUEST_URI' => "/path/of/some/uri"
136
+ assert_equal "/path/of/some/uri", request.request_uri
137
+ assert_equal "/path/of/some/uri", request.path
149
138
 
150
- @request.set_REQUEST_URI "/path/of/some/uri"
151
- assert_equal "/path/of/some/uri", @request.request_uri
152
- assert_equal "/path/of/some/uri", @request.path
139
+ request = stub_request 'REQUEST_URI' => "/"
140
+ assert_equal "/", request.request_uri
141
+ assert_equal "/", request.path
153
142
 
154
- @request.set_REQUEST_URI "/"
155
- assert_equal "/", @request.request_uri
156
- assert_equal "/", @request.path
143
+ request = stub_request 'REQUEST_URI' => "/?m=b"
144
+ assert_equal "/?m=b", request.request_uri
145
+ assert_equal "/", request.path
157
146
 
158
- @request.set_REQUEST_URI "/?m=b"
159
- assert_equal "/?m=b", @request.request_uri
160
- assert_equal "/", @request.path
161
-
162
- @request.set_REQUEST_URI "/"
163
- @request.env['SCRIPT_NAME'] = "/dispatch.cgi"
164
- assert_equal "/", @request.request_uri
165
- assert_equal "/", @request.path
147
+ request = stub_request 'REQUEST_URI' => "/", 'SCRIPT_NAME' => '/dispatch.cgi'
148
+ assert_equal "/", request.request_uri
149
+ assert_equal "/", request.path
166
150
 
167
151
  ActionController::Base.relative_url_root = "/hieraki"
168
- @request.set_REQUEST_URI "/hieraki/"
169
- @request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi"
170
- assert_equal "/hieraki/", @request.request_uri
171
- assert_equal "/", @request.path
152
+ request = stub_request 'REQUEST_URI' => "/hieraki/", 'SCRIPT_NAME' => "/hieraki/dispatch.cgi"
153
+ assert_equal "/hieraki/", request.request_uri
154
+ assert_equal "/", request.path
172
155
  ActionController::Base.relative_url_root = nil
173
156
 
174
157
  ActionController::Base.relative_url_root = "/collaboration/hieraki"
175
- @request.set_REQUEST_URI "/collaboration/hieraki/books/edit/2"
176
- @request.env['SCRIPT_NAME'] = "/collaboration/hieraki/dispatch.cgi"
177
- assert_equal "/collaboration/hieraki/books/edit/2", @request.request_uri
178
- assert_equal "/books/edit/2", @request.path
158
+ request = stub_request 'REQUEST_URI' => "/collaboration/hieraki/books/edit/2",
159
+ 'SCRIPT_NAME' => "/collaboration/hieraki/dispatch.cgi"
160
+ assert_equal "/collaboration/hieraki/books/edit/2", request.request_uri
161
+ assert_equal "/books/edit/2", request.path
179
162
  ActionController::Base.relative_url_root = nil
180
163
 
181
164
  # The following tests are for when REQUEST_URI is not supplied (as in IIS)
182
- @request.env['PATH_INFO'] = "/path/of/some/uri?mapped=1"
183
- @request.env['SCRIPT_NAME'] = nil #"/path/dispatch.rb"
184
- @request.set_REQUEST_URI nil
185
- assert_equal "/path/of/some/uri?mapped=1", @request.request_uri
186
- assert_equal "/path/of/some/uri", @request.path
165
+ request = stub_request 'PATH_INFO' => "/path/of/some/uri?mapped=1",
166
+ 'SCRIPT_NAME' => nil,
167
+ 'REQUEST_URI' => nil
168
+ assert_equal "/path/of/some/uri?mapped=1", request.request_uri
169
+ assert_equal "/path/of/some/uri", request.path
187
170
 
188
171
  ActionController::Base.relative_url_root = '/path'
189
- @request.env['PATH_INFO'] = "/path/of/some/uri?mapped=1"
190
- @request.env['SCRIPT_NAME'] = "/path/dispatch.rb"
191
- @request.set_REQUEST_URI nil
192
- assert_equal "/path/of/some/uri?mapped=1", @request.request_uri
193
- assert_equal "/of/some/uri", @request.path
172
+ request = stub_request 'PATH_INFO' => "/path/of/some/uri?mapped=1",
173
+ 'SCRIPT_NAME' => "/path/dispatch.rb",
174
+ 'REQUEST_URI' => nil
175
+ assert_equal "/path/of/some/uri?mapped=1", request.request_uri
176
+ assert_equal "/of/some/uri", request.path
194
177
  ActionController::Base.relative_url_root = nil
195
178
 
196
- @request.env['PATH_INFO'] = "/path/of/some/uri"
197
- @request.env['SCRIPT_NAME'] = nil
198
- @request.set_REQUEST_URI nil
199
- assert_equal "/path/of/some/uri", @request.request_uri
200
- assert_equal "/path/of/some/uri", @request.path
179
+ request = stub_request 'PATH_INFO' => "/path/of/some/uri",
180
+ 'SCRIPT_NAME' => nil,
181
+ 'REQUEST_URI' => nil
182
+ assert_equal "/path/of/some/uri", request.request_uri
183
+ assert_equal "/path/of/some/uri", request.path
201
184
 
202
- @request.env['PATH_INFO'] = "/"
203
- @request.set_REQUEST_URI nil
204
- assert_equal "/", @request.request_uri
205
- assert_equal "/", @request.path
185
+ request = stub_request 'PATH_INFO' => '/', 'REQUEST_URI' => nil
186
+ assert_equal "/", request.request_uri
187
+ assert_equal "/", request.path
206
188
 
207
- @request.env['PATH_INFO'] = "/?m=b"
208
- @request.set_REQUEST_URI nil
209
- assert_equal "/?m=b", @request.request_uri
210
- assert_equal "/", @request.path
189
+ request = stub_request 'PATH_INFO' => '/?m=b', 'REQUEST_URI' => nil
190
+ assert_equal "/?m=b", request.request_uri
191
+ assert_equal "/", request.path
211
192
 
212
- @request.env['PATH_INFO'] = "/"
213
- @request.env['SCRIPT_NAME'] = "/dispatch.cgi"
214
- @request.set_REQUEST_URI nil
215
- assert_equal "/", @request.request_uri
216
- assert_equal "/", @request.path
193
+ request = stub_request 'PATH_INFO' => "/",
194
+ 'SCRIPT_NAME' => "/dispatch.cgi",
195
+ 'REQUEST_URI' => nil
196
+ assert_equal "/", request.request_uri
197
+ assert_equal "/", request.path
217
198
 
218
199
  ActionController::Base.relative_url_root = '/hieraki'
219
- @request.env['PATH_INFO'] = "/hieraki/"
220
- @request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi"
221
- @request.set_REQUEST_URI nil
222
- assert_equal "/hieraki/", @request.request_uri
223
- assert_equal "/", @request.path
200
+ request = stub_request 'PATH_INFO' => "/hieraki/",
201
+ 'SCRIPT_NAME' => "/hieraki/dispatch.cgi",
202
+ 'REQUEST_URI' => nil
203
+ assert_equal "/hieraki/", request.request_uri
204
+ assert_equal "/", request.path
224
205
  ActionController::Base.relative_url_root = nil
225
206
 
226
- @request.set_REQUEST_URI '/hieraki/dispatch.cgi'
207
+ request = stub_request 'REQUEST_URI' => '/hieraki/dispatch.cgi'
227
208
  ActionController::Base.relative_url_root = '/hieraki'
228
- assert_equal "/dispatch.cgi", @request.path
209
+ assert_equal "/dispatch.cgi", request.path
229
210
  ActionController::Base.relative_url_root = nil
230
211
 
231
- @request.set_REQUEST_URI '/hieraki/dispatch.cgi'
212
+ request = stub_request 'REQUEST_URI' => '/hieraki/dispatch.cgi'
232
213
  ActionController::Base.relative_url_root = '/foo'
233
- assert_equal "/hieraki/dispatch.cgi", @request.path
214
+ assert_equal "/hieraki/dispatch.cgi", request.path
234
215
  ActionController::Base.relative_url_root = nil
235
216
 
236
217
  # This test ensures that Rails uses REQUEST_URI over PATH_INFO
237
218
  ActionController::Base.relative_url_root = nil
238
- @request.env['REQUEST_URI'] = "/some/path"
239
- @request.env['PATH_INFO'] = "/another/path"
240
- @request.env['SCRIPT_NAME'] = "/dispatch.cgi"
241
- assert_equal "/some/path", @request.request_uri
242
- assert_equal "/some/path", @request.path
219
+ request = stub_request 'REQUEST_URI' => "/some/path",
220
+ 'PATH_INFO' => "/another/path",
221
+ 'SCRIPT_NAME' => "/dispatch.cgi"
222
+ assert_equal "/some/path", request.request_uri
223
+ assert_equal "/some/path", request.path
243
224
  end
244
225
 
245
226
  def test_host_with_default_port
246
- @request.host = "rubyonrails.org"
247
- @request.port = 80
248
- assert_equal "rubyonrails.org", @request.host_with_port
227
+ request = stub_request 'HTTP_HOST' => 'rubyonrails.org:80'
228
+ assert_equal "rubyonrails.org", request.host_with_port
249
229
  end
250
230
 
251
231
  def test_host_with_non_default_port
252
- @request.host = "rubyonrails.org"
253
- @request.port = 81
254
- assert_equal "rubyonrails.org:81", @request.host_with_port
232
+ request = stub_request 'HTTP_HOST' => 'rubyonrails.org:81'
233
+ assert_equal "rubyonrails.org:81", request.host_with_port
255
234
  end
256
235
 
257
236
  def test_server_software
258
- assert_equal nil, @request.server_software
237
+ request = stub_request
238
+ assert_equal nil, request.server_software
259
239
 
260
- @request.env['SERVER_SOFTWARE'] = 'Apache3.422'
261
- assert_equal 'apache', @request.server_software
240
+ request = stub_request 'SERVER_SOFTWARE' => 'Apache3.422'
241
+ assert_equal 'apache', request.server_software
262
242
 
263
- @request.env['SERVER_SOFTWARE'] = 'lighttpd(1.1.4)'
264
- assert_equal 'lighttpd', @request.server_software
243
+ request = stub_request 'SERVER_SOFTWARE' => 'lighttpd(1.1.4)'
244
+ assert_equal 'lighttpd', request.server_software
265
245
  end
266
246
 
267
247
  def test_xml_http_request
268
- assert !@request.xml_http_request?
269
- assert !@request.xhr?
248
+ request = stub_request
249
+
250
+ assert !request.xml_http_request?
251
+ assert !request.xhr?
270
252
 
271
- @request.env['HTTP_X_REQUESTED_WITH'] = "DefinitelyNotAjax1.0"
272
- assert !@request.xml_http_request?
273
- assert !@request.xhr?
253
+ request = stub_request 'HTTP_X_REQUESTED_WITH' => 'DefinitelyNotAjax1.0'
254
+ assert !request.xml_http_request?
255
+ assert !request.xhr?
274
256
 
275
- @request.env['HTTP_X_REQUESTED_WITH'] = "XMLHttpRequest"
276
- assert @request.xml_http_request?
277
- assert @request.xhr?
257
+ request = stub_request 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'
258
+ assert request.xml_http_request?
259
+ assert request.xhr?
278
260
  end
279
261
 
280
262
  def test_reports_ssl
281
- assert !@request.ssl?
282
- @request.env['HTTPS'] = 'on'
283
- assert @request.ssl?
263
+ request = stub_request
264
+ assert !request.ssl?
265
+
266
+ request = stub_request 'HTTPS' => 'on'
267
+ assert request.ssl?
284
268
  end
285
269
 
286
270
  def test_reports_ssl_when_proxied_via_lighttpd
287
- assert !@request.ssl?
288
- @request.env['HTTP_X_FORWARDED_PROTO'] = 'https'
289
- assert @request.ssl?
271
+ request = stub_request
272
+ assert !request.ssl?
273
+
274
+ request = stub_request 'HTTP_X_FORWARDED_PROTO' => 'https'
275
+ assert request.ssl?
290
276
  end
291
277
 
292
278
  def test_symbolized_request_methods
293
279
  [:get, :post, :put, :delete].each do |method|
294
- self.request_method = method
295
- assert_equal method, @request.method
280
+ request = stub_request 'REQUEST_METHOD' => method.to_s.upcase
281
+ assert_equal method, request.method
296
282
  end
297
283
  end
298
284
 
299
285
  def test_invalid_http_method_raises_exception
300
286
  assert_raise(ActionController::UnknownHttpMethod) do
301
- self.request_method = :random_method
302
- @request.request_method
287
+ request = stub_request 'REQUEST_METHOD' => 'RANDOM_METHOD'
288
+ request.request_method
303
289
  end
304
290
  end
305
291
 
306
292
  def test_allow_method_hacking_on_post
307
293
  [:get, :head, :options, :put, :post, :delete].each do |method|
308
- self.request_method = method
309
- assert_equal(method == :head ? :get : method, @request.method)
310
- end
311
- end
312
-
313
- def test_invalid_method_hacking_on_post_raises_exception
314
- assert_raise(ActionController::UnknownHttpMethod) do
315
- self.request_method = :_random_method
316
- @request.request_method
294
+ request = stub_request 'REQUEST_METHOD' => method.to_s.upcase
295
+ assert_equal(method == :head ? :get : method, request.method)
317
296
  end
318
297
  end
319
298
 
320
299
  def test_restrict_method_hacking
321
- @request.instance_eval { @parameters = { :_method => 'put' } }
322
300
  [:get, :put, :delete].each do |method|
323
- self.request_method = method
324
- assert_equal method, @request.method
301
+ request = stub_request 'REQUEST_METHOD' => method.to_s.upcase,
302
+ 'action_controller.request.request_parameters' => { :_method => 'put' }
303
+ assert_equal method, request.method
325
304
  end
326
305
  end
327
306
 
328
307
  def test_head_masquerading_as_get
329
- self.request_method = :head
330
- assert_equal :get, @request.method
331
- assert @request.get?
332
- assert @request.head?
308
+ request = stub_request 'REQUEST_METHOD' => 'HEAD'
309
+ assert_equal :get, request.method
310
+ assert request.get?
311
+ assert request.head?
333
312
  end
334
313
 
335
314
  def test_xml_format
336
- @request.instance_eval { @parameters = { :format => 'xml' } }
337
- assert_equal Mime::XML, @request.format
315
+ request = stub_request
316
+ request.expects(:parameters).at_least_once.returns({ :format => 'xml' })
317
+ assert_equal Mime::XML, request.format
338
318
  end
339
319
 
340
320
  def test_xhtml_format
341
- @request.instance_eval { @parameters = { :format => 'xhtml' } }
342
- assert_equal Mime::HTML, @request.format
321
+ request = stub_request
322
+ request.expects(:parameters).at_least_once.returns({ :format => 'xhtml' })
323
+ assert_equal Mime::HTML, request.format
343
324
  end
344
325
 
345
326
  def test_txt_format
346
- @request.instance_eval { @parameters = { :format => 'txt' } }
347
- assert_equal Mime::TEXT, @request.format
327
+ request = stub_request
328
+ request.expects(:parameters).at_least_once.returns({ :format => 'txt' })
329
+ assert_equal Mime::TEXT, request.format
348
330
  end
349
331
 
350
- def test_nil_format
332
+ def test_xml_http_request
351
333
  ActionController::Base.use_accept_header, old =
352
334
  false, ActionController::Base.use_accept_header
353
335
 
354
- @request.instance_eval { @parameters = {} }
355
- @request.env["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest"
356
- assert @request.xhr?
357
- assert_equal Mime::JS, @request.format
358
-
336
+ request = stub_request 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'
337
+ request.expects(:parameters).at_least_once.returns({})
338
+ assert request.xhr?
339
+ assert_equal Mime::JS, request.format
359
340
  ensure
360
341
  ActionController::Base.use_accept_header = old
361
342
  end
362
343
 
363
344
  def test_content_type
364
- @request.env["CONTENT_TYPE"] = "text/html"
365
- assert_equal Mime::HTML, @request.content_type
345
+ request = stub_request 'CONTENT_TYPE' => 'text/html'
346
+ assert_equal Mime::HTML, request.content_type
366
347
  end
367
348
 
368
- def test_format_assignment_should_set_format
369
- @request.instance_eval { self.format = :txt }
370
- assert !@request.format.xml?
371
- @request.instance_eval { self.format = :xml }
372
- assert @request.format.xml?
349
+ def test_can_override_format_with_parameter
350
+ request = stub_request
351
+ request.expects(:parameters).at_least_once.returns({ :format => :txt })
352
+ assert !request.format.xml?
353
+
354
+ request = stub_request
355
+ request.expects(:parameters).at_least_once.returns({ :format => :xml })
356
+ assert request.format.xml?
373
357
  end
374
358
 
375
359
  def test_content_no_type
376
- assert_equal nil, @request.content_type
360
+ request = stub_request
361
+ assert_equal nil, request.content_type
377
362
  end
378
363
 
379
364
  def test_content_type_xml
380
- @request.env["CONTENT_TYPE"] = "application/xml"
381
- assert_equal Mime::XML, @request.content_type
365
+ request = stub_request 'CONTENT_TYPE' => 'application/xml'
366
+ assert_equal Mime::XML, request.content_type
382
367
  end
383
368
 
384
369
  def test_content_type_with_charset
385
- @request.env["CONTENT_TYPE"] = "application/xml; charset=UTF-8"
386
- assert_equal Mime::XML, @request.content_type
370
+ request = stub_request 'CONTENT_TYPE' => 'application/xml; charset=UTF-8'
371
+ assert_equal Mime::XML, request.content_type
387
372
  end
388
373
 
389
374
  def test_user_agent
390
- assert_not_nil @request.user_agent
375
+ request = stub_request 'HTTP_USER_AGENT' => 'TestAgent'
376
+ assert_equal 'TestAgent', request.user_agent
391
377
  end
392
378
 
393
379
  def test_parameters
394
- @request.stubs(:request_parameters).returns({ "foo" => 1 })
395
- @request.stubs(:query_parameters).returns({ "bar" => 2 })
380
+ request = stub_request
381
+ request.stubs(:request_parameters).returns({ "foo" => 1 })
382
+ request.stubs(:query_parameters).returns({ "bar" => 2 })
396
383
 
397
- assert_equal({"foo" => 1, "bar" => 2}, @request.parameters)
398
- assert_equal({"foo" => 1}, @request.request_parameters)
399
- assert_equal({"bar" => 2}, @request.query_parameters)
384
+ assert_equal({"foo" => 1, "bar" => 2}, request.parameters)
385
+ assert_equal({"foo" => 1}, request.request_parameters)
386
+ assert_equal({"bar" => 2}, request.query_parameters)
387
+ end
388
+
389
+ protected
390
+
391
+ def stub_request(env={})
392
+ ActionController::Request.new(env)
400
393
  end
401
394
 
402
- protected
403
- def request_method=(method)
404
- @request.env['REQUEST_METHOD'] = method.to_s.upcase
405
- @request.request_method = nil # Reset the ivar cache
406
- end
407
395
  end