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.
-
#code ⇒ Object
returns a String to ensure compatibility with Net::HTTPResponse.
-
#cookies ⇒ Object
Returns the response cookies, converted to a Hash of (name => CGI::Cookie) pairs Example:.
-
#error? ⇒ Boolean
(also: #server_error?)
was there a server-side error?.
-
#flash ⇒ Object
a shortcut to the flash (or an empty hash if no flash.. hey! that rhymes!).
-
#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_file(with_controller = false) ⇒ Object
returns the template path of the file which was used to render this response (or nil).
-
#rendered_with_file? ⇒ Boolean
was this template rendered by a file?.
-
#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
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/action_controller/test_process.rb', line 255 def binary_content raise "Response body is not a Proc: #{body.inspect}" unless body.kind_of?(Proc) require 'stringio' sio = StringIO.new begin $stdout = sio body.call ensure $stdout = STDOUT end sio.rewind sio.read end |
#code ⇒ Object
returns a String to ensure compatibility with Net::HTTPResponse
150 151 152 |
# File 'lib/action_controller/test_process.rb', line 150 def code headers['Status'].to_s.split(' ')[0] end |
#cookies ⇒ Object
Returns the response cookies, converted to a Hash of (name => CGI::Cookie) pairs Example:
assert_equal [‘AuthorOfNewPage’], r.cookies.value
250 251 252 |
# File 'lib/action_controller/test_process.rb', line 250 def headers['cookie'].inject({}) { |hash, | hash[.name] = ; hash } end |
#error? ⇒ Boolean Also known as: server_error?
was there a server-side error?
174 175 176 |
# File 'lib/action_controller/test_process.rb', line 174 def error? (500..599).include?(response_code) end |
#flash ⇒ Object
a shortcut to the flash (or an empty hash if no flash.. hey! that rhymes!)
212 213 214 |
# File 'lib/action_controller/test_process.rb', line 212 def flash session['flash'] || {} end |
#has_flash? ⇒ Boolean
do we have a flash?
217 218 219 |
# File 'lib/action_controller/test_process.rb', line 217 def has_flash? !session['flash'].empty? end |
#has_flash_object?(name = nil) ⇒ Boolean
does the specified flash object exist?
227 228 229 |
# File 'lib/action_controller/test_process.rb', line 227 def has_flash_object?(name=nil) !flash[name].nil? end |
#has_flash_with_contents? ⇒ Boolean
do we have a flash that has contents?
222 223 224 |
# File 'lib/action_controller/test_process.rb', line 222 def has_flash_with_contents? !flash.empty? end |
#has_session_object?(name = nil) ⇒ Boolean
does the specified object exist in the session?
232 233 234 |
# File 'lib/action_controller/test_process.rb', line 232 def has_session_object?(name=nil) !session[name].nil? end |
#has_template_object?(name = nil) ⇒ Boolean
does the specified template object exist?
242 243 244 |
# File 'lib/action_controller/test_process.rb', line 242 def has_template_object?(name=nil) !template_objects[name].nil? end |
#message ⇒ Object
154 155 156 |
# File 'lib/action_controller/test_process.rb', line 154 def headers['Status'].to_s.split(' ',2)[1] end |
#missing? ⇒ Boolean
was the URL not found?
164 165 166 |
# File 'lib/action_controller/test_process.rb', line 164 def missing? response_code == 404 end |
#redirect? ⇒ Boolean
were we redirected?
169 170 171 |
# File 'lib/action_controller/test_process.rb', line 169 def redirect? (300..399).include?(response_code) end |
#redirect_url ⇒ Object
returns the redirection location or nil
181 182 183 |
# File 'lib/action_controller/test_process.rb', line 181 def redirect_url headers['Location'] end |
#redirect_url_match?(pattern) ⇒ Boolean
does the redirect location match this regexp pattern?
186 187 188 189 190 191 192 |
# File 'lib/action_controller/test_process.rb', line 186 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_file(with_controller = false) ⇒ Object
returns the template path of the file which was used to render this response (or nil)
196 197 198 199 200 201 202 203 204 |
# File 'lib/action_controller/test_process.rb', line 196 def rendered_file(with_controller=false) unless template.first_render.nil? unless with_controller template.first_render else template.first_render.split('/').last || template.first_render end end end |
#rendered_with_file? ⇒ Boolean
was this template rendered by a file?
207 208 209 |
# File 'lib/action_controller/test_process.rb', line 207 def rendered_with_file? !rendered_file.nil? end |
#response_code ⇒ Object
the response code of the request
145 146 147 |
# File 'lib/action_controller/test_process.rb', line 145 def response_code headers['Status'][0,3].to_i rescue 0 end |
#success? ⇒ Boolean
was the response successful?
159 160 161 |
# File 'lib/action_controller/test_process.rb', line 159 def success? response_code == 200 end |
#template_objects ⇒ Object
a shortcut to the template.assigns
237 238 239 |
# File 'lib/action_controller/test_process.rb', line 237 def template_objects template.assigns || {} end |