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

Instance Method Details

#binary_contentObject

Returns binary content (downloadable file), converted to a String



267
268
269
270
271
272
273
274
275
276
# File 'lib/action_controller/test_process.rb', line 267

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

#codeObject

Returns a String to ensure compatibility with Net::HTTPResponse



174
175
176
# File 'lib/action_controller/test_process.rb', line 174

def code
  status.to_s.split(' ')[0]
end

#cookiesObject

Returns the response cookies, converted to a Hash of (name => CGI::Cookie) pairs

assert_equal ['AuthorOfNewPage'], r.cookies['author'].value


262
263
264
# File 'lib/action_controller/test_process.rb', line 262

def cookies
  headers['cookie'].inject({}) { |hash, cookie| hash[cookie.name] = cookie; hash }
end

#error?Boolean Also known as: server_error?

Was there a server-side error?

Returns:

  • (Boolean)


198
199
200
# File 'lib/action_controller/test_process.rb', line 198

def error?
  (500..599).include?(response_code)
end

#flashObject

A shortcut to the flash. Returns an empty hash if no session flash exists.



225
226
227
# File 'lib/action_controller/test_process.rb', line 225

def flash
  session['flash'] || {}
end

#has_flash?Boolean

Do we have a flash?

Returns:

  • (Boolean)


230
231
232
# File 'lib/action_controller/test_process.rb', line 230

def has_flash?
  !session['flash'].empty?
end

#has_flash_object?(name = nil) ⇒ Boolean

Does the specified flash object exist?

Returns:

  • (Boolean)


240
241
242
# File 'lib/action_controller/test_process.rb', line 240

def has_flash_object?(name=nil)
  !flash[name].nil?
end

#has_flash_with_contents?Boolean

Do we have a flash that has contents?

Returns:

  • (Boolean)


235
236
237
# File 'lib/action_controller/test_process.rb', line 235

def has_flash_with_contents?
  !flash.empty?
end

#has_session_object?(name = nil) ⇒ Boolean

Does the specified object exist in the session?

Returns:

  • (Boolean)


245
246
247
# File 'lib/action_controller/test_process.rb', line 245

def has_session_object?(name=nil)
  !session[name].nil?
end

#has_template_object?(name = nil) ⇒ Boolean

Does the specified template object exist?

Returns:

  • (Boolean)


255
256
257
# File 'lib/action_controller/test_process.rb', line 255

def has_template_object?(name=nil)
  !template_objects[name].nil?
end

#messageObject



178
179
180
# File 'lib/action_controller/test_process.rb', line 178

def message
  status.to_s.split(' ',2)[1]
end

#missing?Boolean

Was the URL not found?

Returns:

  • (Boolean)


188
189
190
# File 'lib/action_controller/test_process.rb', line 188

def missing?
  response_code == 404
end

#redirect?Boolean

Were we redirected?

Returns:

  • (Boolean)


193
194
195
# File 'lib/action_controller/test_process.rb', line 193

def redirect?
  (300..399).include?(response_code)
end

#redirect_urlObject

Returns the redirection location or nil



205
206
207
# File 'lib/action_controller/test_process.rb', line 205

def redirect_url
  headers['Location']
end

#redirect_url_match?(pattern) ⇒ Boolean

Does the redirect location match this regexp pattern?

Returns:

  • (Boolean)


210
211
212
213
214
215
216
# File 'lib/action_controller/test_process.rb', line 210

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_templateObject

Returns the template of the file which was used to render this response (or nil)



220
221
222
# File 'lib/action_controller/test_process.rb', line 220

def rendered_template
  template.instance_variable_get(:@_first_render)
end

#response_codeObject

The response code of the request



169
170
171
# File 'lib/action_controller/test_process.rb', line 169

def response_code
  status[0,3].to_i rescue 0
end

#success?Boolean

Was the response successful?

Returns:

  • (Boolean)


183
184
185
# File 'lib/action_controller/test_process.rb', line 183

def success?
  (200..299).include?(response_code)
end

#template_objectsObject

A shortcut to the template.assigns



250
251
252
# File 'lib/action_controller/test_process.rb', line 250

def template_objects
  template.assigns || {}
end