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



260
261
262
263
264
265
266
267
268
269
# File 'lib/action_controller/test_process.rb', line 260

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?

Returns:

  • (Boolean)


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

def client_error?
  (400..499).include?(response_code)
end

#codeObject

Returns a String to ensure compatibility with Net::HTTPResponse



157
158
159
# File 'lib/action_controller/test_process.rb', line 157

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

#cookiesObject

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

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


250
251
252
253
254
255
256
257
# File 'lib/action_controller/test_process.rb', line 250

def cookies
  cookies = {}
  Array(headers['Set-Cookie']).each do |cookie|
    key, value = cookie.split(";").first.split("=")
    cookies[key] = value
  end
  cookies
end

#error?Boolean Also known as: server_error?

Was there a server-side error?

Returns:

  • (Boolean)


181
182
183
# File 'lib/action_controller/test_process.rb', line 181

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

#flashObject

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



213
214
215
# File 'lib/action_controller/test_process.rb', line 213

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

#has_flash?Boolean

Do we have a flash?

Returns:

  • (Boolean)


218
219
220
# File 'lib/action_controller/test_process.rb', line 218

def has_flash?
  !flash.empty?
end

#has_flash_object?(name = nil) ⇒ Boolean

Does the specified flash object exist?

Returns:

  • (Boolean)


228
229
230
# File 'lib/action_controller/test_process.rb', line 228

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)


223
224
225
# File 'lib/action_controller/test_process.rb', line 223

def has_flash_with_contents?
  !flash.empty?
end

#has_session_object?(name = nil) ⇒ Boolean

Does the specified object exist in the session?

Returns:

  • (Boolean)


233
234
235
# File 'lib/action_controller/test_process.rb', line 233

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

#has_template_object?(name = nil) ⇒ Boolean

Does the specified template object exist?

Returns:

  • (Boolean)


243
244
245
# File 'lib/action_controller/test_process.rb', line 243

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

#messageObject



161
162
163
# File 'lib/action_controller/test_process.rb', line 161

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

#missing?Boolean

Was the URL not found?

Returns:

  • (Boolean)


171
172
173
# File 'lib/action_controller/test_process.rb', line 171

def missing?
  response_code == 404
end

#redirect?Boolean

Were we redirected?

Returns:

  • (Boolean)


176
177
178
# File 'lib/action_controller/test_process.rb', line 176

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

#redirect_urlObject

Returns the redirection location or nil



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

def redirect_url
  headers['Location']
end

#redirect_url_match?(pattern) ⇒ Boolean

Does the redirect location match this regexp pattern?

Returns:

  • (Boolean)


198
199
200
201
202
203
204
# File 'lib/action_controller/test_process.rb', line 198

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

#renderedObject

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



208
209
210
# File 'lib/action_controller/test_process.rb', line 208

def rendered
  template.instance_variable_get(:@_rendered)
end

#response_codeObject

The response code of the request



152
153
154
# File 'lib/action_controller/test_process.rb', line 152

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

#success?Boolean

Was the response successful?

Returns:

  • (Boolean)


166
167
168
# File 'lib/action_controller/test_process.rb', line 166

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

#template_objectsObject

A shortcut to the template.assigns



238
239
240
# File 'lib/action_controller/test_process.rb', line 238

def template_objects
  template.assigns || {}
end