Class: ActionController::TestResponse

Inherits:
AbstractResponse show all
Defined in:
lib/action_controller/test_process.rb

Overview

:nodoc:

Constant Summary

Constants inherited from AbstractResponse

AbstractResponse::DEFAULT_HEADERS

Instance Attribute Summary

Attributes inherited from AbstractResponse

#assigns, #body, #headers, #redirected_to, #redirected_to_method_params, #session, #template

Instance Method Summary collapse

Methods inherited from AbstractResponse

#initialize, #redirect

Constructor Details

This class inherits a constructor from ActionController::AbstractResponse

Instance Method Details

#binary_contentObject

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


235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/action_controller/test_process.rb', line 235

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


130
131
132
# File 'lib/action_controller/test_process.rb', line 130

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


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

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)

154
155
156
# File 'lib/action_controller/test_process.rb', line 154

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

#flashObject

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


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

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

#has_flash?Boolean

do we have a flash?

Returns:

  • (Boolean)

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

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

#has_flash_object?(name = nil) ⇒ Boolean

does the specified flash object exist?

Returns:

  • (Boolean)

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

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)

202
203
204
# File 'lib/action_controller/test_process.rb', line 202

def has_flash_with_contents?
  !flash.empty?
end

#has_session_object?(name = nil) ⇒ Boolean

does the specified object exist in the session?

Returns:

  • (Boolean)

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

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

#has_template_object?(name = nil) ⇒ Boolean

does the specified template object exist?

Returns:

  • (Boolean)

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

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

#messageObject


134
135
136
# File 'lib/action_controller/test_process.rb', line 134

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

#missing?Boolean

was the URL not found?

Returns:

  • (Boolean)

144
145
146
# File 'lib/action_controller/test_process.rb', line 144

def missing?
  response_code == 404
end

#redirect?Boolean

were we redirected?

Returns:

  • (Boolean)

149
150
151
# File 'lib/action_controller/test_process.rb', line 149

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

#redirect_urlObject

returns the redirection location or nil


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

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

#redirect_url_match?(pattern) ⇒ Boolean

does the redirect location match this regexp pattern?

Returns:

  • (Boolean)

166
167
168
169
170
171
172
# File 'lib/action_controller/test_process.rb', line 166

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)


176
177
178
179
180
181
182
183
184
# File 'lib/action_controller/test_process.rb', line 176

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)

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

def rendered_with_file?
  !rendered_file.nil?
end

#response_codeObject

the response code of the request


125
126
127
# File 'lib/action_controller/test_process.rb', line 125

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

#success?Boolean

was the response successful?

Returns:

  • (Boolean)

139
140
141
# File 'lib/action_controller/test_process.rb', line 139

def success?
  response_code == 200
end

#template_objectsObject

a shortcut to the template.assigns


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

def template_objects
  template.assigns || {}
end