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



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/action_controller/test_process.rb', line 252

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

#codeObject

returns a String to ensure compatibility with Net::HTTPResponse



147
148
149
# File 'lib/action_controller/test_process.rb', line 147

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

#cookiesObject

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

assert_equal [‘AuthorOfNewPage’], r.cookies.value



247
248
249
# File 'lib/action_controller/test_process.rb', line 247

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)


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

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

#flashObject

a shortcut to the flash (or an empty hash if no flash.. hey! that rhymes!)



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

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

#has_flash?Boolean

do we have a flash?

Returns:

  • (Boolean)


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

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

#has_flash_object?(name = nil) ⇒ Boolean

does the specified flash object exist?

Returns:

  • (Boolean)


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

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)


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

def has_flash_with_contents?
  !flash.empty?
end

#has_session_object?(name = nil) ⇒ Boolean

does the specified object exist in the session?

Returns:

  • (Boolean)


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

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

#has_template_object?(name = nil) ⇒ Boolean

does the specified template object exist?

Returns:

  • (Boolean)


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

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

#messageObject



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

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

#missing?Boolean

was the URL not found?

Returns:

  • (Boolean)


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

def missing?
  response_code == 404
end

#redirect?Boolean

were we redirected?

Returns:

  • (Boolean)


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

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

#redirect_urlObject

returns the redirection location or nil



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

def redirect_url
  redirect? ? headers['location'] : nil
end

#redirect_url_match?(pattern) ⇒ Boolean

does the redirect location match this regexp pattern?

Returns:

  • (Boolean)


183
184
185
186
187
188
189
# File 'lib/action_controller/test_process.rb', line 183

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)



193
194
195
196
197
198
199
200
201
# File 'lib/action_controller/test_process.rb', line 193

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?

Returns:

  • (Boolean)


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

def rendered_with_file?
  !rendered_file.nil?
end

#response_codeObject

the response code of the request



142
143
144
# File 'lib/action_controller/test_process.rb', line 142

def response_code
  headers['Status'][0,3].to_i rescue 0
end

#success?Boolean

was the response successful?

Returns:

  • (Boolean)


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

def success?
  response_code == 200
end

#template_objectsObject

a shortcut to the template.assigns



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

def template_objects
  template.assigns || {}
end