Module: ActionController::TestResponseBehavior
- Included in:
- TestResponse
- Defined in:
- lib/action_controller/test_process.rb
Overview
A refactoring of TestResponse to allow the same behavior to be applied to the “real” CgiResponse class in integration tests.
Instance Method Summary collapse
-
#binary_content ⇒ Object
Returns binary content (downloadable file), converted to a String.
-
#client_error? ⇒ Boolean
Was there a client client?.
-
#code ⇒ Object
Returns a String to ensure compatibility with Net::HTTPResponse.
-
#cookies ⇒ Object
Returns the response cookies, converted to a Hash of (name => value) pairs.
-
#error? ⇒ Boolean
(also: #server_error?)
Was there a server-side error?.
-
#flash ⇒ Object
A shortcut to the flash.
-
#has_flash? ⇒ Boolean
Do we have a flash?.
-
#has_flash_object?(name = nil) ⇒ Boolean
Does the specified flash object exist?.
-
#has_flash_with_contents? ⇒ Boolean
Do we have a flash that has contents?.
-
#has_session_object?(name = nil) ⇒ Boolean
Does the specified object exist in the session?.
-
#has_template_object?(name = nil) ⇒ Boolean
Does the specified template object exist?.
- #message ⇒ Object
-
#missing? ⇒ Boolean
Was the URL not found?.
-
#redirect? ⇒ Boolean
Were we redirected?.
-
#redirect_url ⇒ Object
Returns the redirection location or nil.
-
#redirect_url_match?(pattern) ⇒ Boolean
Does the redirect location match this regexp pattern?.
-
#rendered ⇒ Object
Returns the template of the file which was used to render this response (or nil).
-
#response_code ⇒ Object
The response code of the request.
-
#success? ⇒ Boolean
Was the response successful?.
-
#template_objects ⇒ Object
A shortcut to the template.assigns.
Instance Method Details
#binary_content ⇒ Object
Returns binary content (downloadable file), converted to a String
268 269 270 271 272 273 274 275 276 277 |
# File 'lib/action_controller/test_process.rb', line 268 def binary_content raise "Response body is not a Proc: #{body.inspect}" unless body.kind_of?(Proc) require 'stringio' sio = StringIO.new body.call(self, sio) sio.rewind sio.read end |
#client_error? ⇒ Boolean
Was there a client client?
196 197 198 |
# File 'lib/action_controller/test_process.rb', line 196 def client_error? (400..499).include?(response_code) end |
#code ⇒ Object
Returns a String to ensure compatibility with Net::HTTPResponse
165 166 167 |
# File 'lib/action_controller/test_process.rb', line 165 def code status.to_s.split(' ')[0] end |
#cookies ⇒ Object
Returns the response cookies, converted to a Hash of (name => value) pairs
assert_equal 'AuthorOfNewPage', r.['author']
258 259 260 261 262 263 264 265 |
# File 'lib/action_controller/test_process.rb', line 258 def = {} Array(headers['Set-Cookie']).each do || key, value = .split(";").first.split("=").map {|val| Rack::Utils.unescape(val)} [key] = value end end |
#error? ⇒ Boolean Also known as: server_error?
Was there a server-side error?
189 190 191 |
# File 'lib/action_controller/test_process.rb', line 189 def error? (500..599).include?(response_code) end |
#flash ⇒ Object
A shortcut to the flash. Returns an empty hash if no session flash exists.
221 222 223 |
# File 'lib/action_controller/test_process.rb', line 221 def flash session['flash'] || {} end |
#has_flash? ⇒ Boolean
Do we have a flash?
226 227 228 |
# File 'lib/action_controller/test_process.rb', line 226 def has_flash? !flash.empty? end |
#has_flash_object?(name = nil) ⇒ Boolean
Does the specified flash object exist?
236 237 238 |
# File 'lib/action_controller/test_process.rb', line 236 def has_flash_object?(name=nil) !flash[name].nil? end |
#has_flash_with_contents? ⇒ Boolean
Do we have a flash that has contents?
231 232 233 |
# File 'lib/action_controller/test_process.rb', line 231 def has_flash_with_contents? !flash.empty? end |
#has_session_object?(name = nil) ⇒ Boolean
Does the specified object exist in the session?
241 242 243 |
# File 'lib/action_controller/test_process.rb', line 241 def has_session_object?(name=nil) !session[name].nil? end |
#has_template_object?(name = nil) ⇒ Boolean
Does the specified template object exist?
251 252 253 |
# File 'lib/action_controller/test_process.rb', line 251 def has_template_object?(name=nil) !template_objects[name].nil? end |
#message ⇒ Object
169 170 171 |
# File 'lib/action_controller/test_process.rb', line 169 def status.to_s.split(' ',2)[1] end |
#missing? ⇒ Boolean
Was the URL not found?
179 180 181 |
# File 'lib/action_controller/test_process.rb', line 179 def missing? response_code == 404 end |
#redirect? ⇒ Boolean
Were we redirected?
184 185 186 |
# File 'lib/action_controller/test_process.rb', line 184 def redirect? (300..399).include?(response_code) end |
#redirect_url ⇒ Object
Returns the redirection location or nil
201 202 203 |
# File 'lib/action_controller/test_process.rb', line 201 def redirect_url headers['Location'] end |
#redirect_url_match?(pattern) ⇒ Boolean
Does the redirect location match this regexp pattern?
206 207 208 209 210 211 212 |
# File 'lib/action_controller/test_process.rb', line 206 def redirect_url_match?( pattern ) return false if redirect_url.nil? p = Regexp.new(pattern) if pattern.class == String p = pattern if pattern.class == Regexp return false if p.nil? p.match(redirect_url) != nil end |
#rendered ⇒ Object
Returns the template of the file which was used to render this response (or nil)
216 217 218 |
# File 'lib/action_controller/test_process.rb', line 216 def rendered template.instance_variable_get(:@_rendered) end |
#response_code ⇒ Object
The response code of the request
160 161 162 |
# File 'lib/action_controller/test_process.rb', line 160 def response_code status.to_s[0,3].to_i rescue 0 end |
#success? ⇒ Boolean
Was the response successful?
174 175 176 |
# File 'lib/action_controller/test_process.rb', line 174 def success? (200..299).include?(response_code) end |
#template_objects ⇒ Object
A shortcut to the template.assigns
246 247 248 |
# File 'lib/action_controller/test_process.rb', line 246 def template_objects template.assigns || {} end |