1 module ActionController
#:nodoc:
2 class TestRequest
< Request
#:nodoc:
3 attr_accessor
:cookies, :session_options
4 attr_accessor
:query_parameters, :path, :session
11 def initialize(env = {})
12 super(Rack
::MockRequest.env_for("/").merge(env))
14 @query_parameters = {}
15 @session = TestSession
.new
17 initialize_default_values
25 # Wraps raw_post in a StringIO.
26 def body_stream
#:nodoc:
27 StringIO
.new(raw_post
)
30 # Either the RAW_POST_DATA environment variable or the URL-encoded request
33 @env['RAW_POST_DATA'] ||= begin
34 data = url_encoded_request_parameters
35 data.force_encoding(Encoding
::BINARY) if data.respond_to
?(:force_encoding)
41 @env["SERVER_PORT"] = number
.to_i
44 def action
=(action_name
)
45 @query_parameters.update({ "action" => action_name
})
49 # Used to check AbstractRequest's request_uri functionality.
50 # Disables the use of @path and @request_uri so superclass can handle those.
51 def set_REQUEST_URI(value
)
52 @env["REQUEST_URI"] = value
59 @path = uri
.split("?").first
62 def request_method
=(method
)
63 @request_method = method
66 def accept
=(mime_types
)
67 @env["HTTP_ACCEPT"] = Array(mime_types
).collect
{ |mime_types
| mime_types
.to_s
}.join(",")
71 def if_modified_since
=(last_modified
)
72 @env["HTTP_IF_MODIFIED_SINCE"] = last_modified
75 def if_none_match
=(etag
)
76 @env["HTTP_IF_NONE_MATCH"] = etag
79 def remote_addr
=(addr
)
80 @env['REMOTE_ADDR'] = addr
83 def request_uri(*args
)
84 @request_uri || super()
91 def assign_parameters(controller_path
, action
, parameters
)
92 parameters
= parameters
.symbolize_keys
.merge(:controller => controller_path
, :action => action
)
93 extra_keys
= ActionController
::Routing::Routes.extra_keys(parameters
)
94 non_path_parameters
= get
? ? query_parameters
: request_parameters
95 parameters
.each
do |key
, value
|
98 elsif value
.is_a
? Array
99 value
= ActionController
::Routing::PathSegment::Result.new(value
)
102 if extra_keys
.include?(key
.to_sym
)
103 non_path_parameters
[key
] = value
105 path_parameters
[key
.to_s
] = value
108 raw_post
# populate env['RAW_POST_DATA']
109 @parameters = nil # reset TestRequest#parameters to use the new path_parameters
113 self.query_parameters
= {}
114 self.path_parameters
= {}
115 @headers, @request_method, @accepts, @content_type = nil, nil, nil, nil
118 def user_agent
=(user_agent
)
119 @env['HTTP_USER_AGENT'] = user_agent
123 def initialize_containers
127 def initialize_default_values
130 @env['HTTP_USER_AGENT'] = "Rails Testing"
131 @env['REMOTE_ADDR'] = "0.0.0.0"
132 @env["SERVER_PORT"] = 80
133 @env['REQUEST_METHOD'] = "GET"
136 def url_encoded_request_parameters
137 params
= self.request_parameters
.dup
139 %w(controller action only_path
).each
do |k
|
141 params
.delete(k
.to_sym
)
148 # A refactoring of TestResponse to allow the same behavior to be applied
149 # to the "real" CgiResponse class in integration tests.
150 module TestResponseBehavior
#:nodoc:
151 # The response code of the request
153 status
.to_s
[0,3].to_i
rescue 0
156 # Returns a String to ensure compatibility with Net::HTTPResponse
158 status
.to_s
.split(' ')[0]
162 status
.to_s
.split(' ',2)[1]
165 # Was the response successful?
167 (200..299).include?(response_code
)
170 # Was the URL not found?
175 # Were we redirected?
177 (300..399).include?(response_code
)
180 # Was there a server-side error?
182 (500..599).include?(response_code
)
185 alias_method
:server_error?, :error?
187 # Was there a client client?
189 (400..499).include?(response_code
)
192 # Returns the redirection location or nil
197 # Does the redirect location match this regexp pattern?
198 def redirect_url_match
?( pattern
)
199 return false if redirect_url
.nil?
200 p
= Regexp
.new(pattern
) if pattern
.class == String
201 p
= pattern
if pattern
.class == Regexp
202 return false if p
.nil?
203 p
.match(redirect_url
) != nil
206 # Returns the template of the file which was used to
207 # render this response (or nil)
209 template
.instance_variable_get(:@_rendered)
212 # A shortcut to the flash. Returns an empty hash if no session flash exists.
214 session
['flash'] || {}
217 # Do we have a flash?
222 # Do we have a flash that has contents?
223 def has_flash_with_contents
?
227 # Does the specified flash object exist?
228 def has_flash_object
?(name
=nil)
232 # Does the specified object exist in the session?
233 def has_session_object
?(name
=nil)
237 # A shortcut to the template.assigns
239 template
.assigns
|| {}
242 # Does the specified template object exist?
243 def has_template_object
?(name
=nil)
244 !template_objects
[name
].nil?
247 # Returns the response cookies, converted to a Hash of (name => value) pairs
249 # assert_equal 'AuthorOfNewPage', r.cookies['author']
252 Array(headers
['Set-Cookie']).each
do |cookie
|
253 key
, value
= cookie
.split(";").first
.split("=")
259 # Returns binary content (downloadable file), converted to a String
261 raise "Response body is not a Proc: #{body.inspect}" unless body
.kind_of
?(Proc
)
272 # Integration test methods such as ActionController::Integration::Session#get
273 # and ActionController::Integration::Session#post return objects of class
274 # TestResponse, which represent the HTTP response results of the requested
275 # controller actions.
277 # See Response for more information on controller response objects.
278 class TestResponse
< Response
279 include TestResponseBehavior
282 headers
.delete('ETag')
283 headers
.delete('Last-Modified')
287 class TestSession
< Hash
#:nodoc:
288 attr_accessor
:session_id
290 def initialize(attributes
= nil)
292 replace_attributes(attributes
)
297 replace_attributes({ })
309 super(key
.to_s
, value
)
312 def update(hash
= nil)
314 ActiveSupport
::Deprecation.warn('use replace instead', caller
)
321 def delete(key
= nil)
323 ActiveSupport
::Deprecation.warn('use clear instead', caller
)
331 ActiveSupport
::Deprecation.warn('sessions should no longer be closed', caller
)
340 def replace_attributes(attributes
= nil)
342 replace(attributes
.stringify_keys
)
346 # Essentially generates a modified Tempfile object similar to the object
347 # you'd get from the standard library CGI module in a multipart
348 # request. This means you can use an ActionController::TestUploadedFile
349 # object in the params of a test request in order to simulate
352 # Usage example, within a functional test:
353 # post :change_avatar, :avatar => ActionController::TestUploadedFile.new(ActionController::TestCase.fixture_path + '/files/spongebob.png', 'image/png')
355 # Pass a true third parameter to ensure the uploaded file is opened in binary mode (only required for Windows):
356 # post :change_avatar, :avatar => ActionController::TestUploadedFile.new(ActionController::TestCase.fixture_path + '/files/spongebob.png', 'image/png', :binary)
358 class TestUploadedFile
359 # The filename, *not* including the path, of the "uploaded" file
360 attr_reader
:original_filename
362 # The content type of the "uploaded" file
363 attr_accessor
:content_type
365 def initialize(path
, content_type
= Mime
::TEXT, binary
= false)
366 raise "#{path} file does not exist" unless File
.exist
?(path
)
367 @content_type = content_type
368 @original_filename = path
.sub(/^.*#{File::SEPARATOR}([^#{File::SEPARATOR}]+)$/) { $1 }
369 @tempfile = Tempfile
.new(@original_filename)
370 @tempfile.set_encoding(Encoding
::BINARY) if @tempfile.respond_to
?(:set_encoding)
371 @tempfile.binmode
if binary
372 FileUtils
.copy_file(path
, @tempfile.path
)
379 alias local_path path
381 def method_missing(method_name
, *args
, &block
) #:nodoc:
382 @tempfile.__send__(method_name
, *args
, &block
)
387 def self.included(base
)
388 # Executes a request simulating GET HTTP method and set/volley the response
389 def get(action
, parameters
= nil, session
= nil, flash
= nil)
390 process(action
, parameters
, session
, flash
, "GET")
393 # Executes a request simulating POST HTTP method and set/volley the response
394 def post(action
, parameters
= nil, session
= nil, flash
= nil)
395 process(action
, parameters
, session
, flash
, "POST")
398 # Executes a request simulating PUT HTTP method and set/volley the response
399 def put(action
, parameters
= nil, session
= nil, flash
= nil)
400 process(action
, parameters
, session
, flash
, "PUT")
403 # Executes a request simulating DELETE HTTP method and set/volley the response
404 def delete(action
, parameters
= nil, session
= nil, flash
= nil)
405 process(action
, parameters
, session
, flash
, "DELETE")
408 # Executes a request simulating HEAD HTTP method and set/volley the response
409 def head(action
, parameters
= nil, session
= nil, flash
= nil)
410 process(action
, parameters
, session
, flash
, "HEAD")
414 def process(action
, parameters
= nil, session
= nil, flash
= nil, http_method
= 'GET')
415 # Sanity check for required instance variables so we can give an
416 # understandable error message.
417 %w(@controller @request @response).each
do |iv_name
|
418 if !(instance_variable_names
.include?(iv_name
) || instance_variable_names
.include?(iv_name
.to_sym
)) || instance_variable_get(iv_name
).nil?
419 raise "#{iv_name} is nil: make sure you set it in your test's setup method."
427 @request.env['REQUEST_METHOD'] = http_method
429 @request.action
= action
.to_s
432 @request.assign_parameters(@controller.class.controller_path
, action
.to_s
, parameters
)
434 @request.session
= ActionController
::TestSession.new(session
) unless session
.nil?
435 @request.session
["flash"] = ActionController
::Flash::FlashHash.new
.update(flash
) if flash
436 build_request_uri(action
, parameters
)
438 Base
.class_eval
{ include ProcessWithTest
} unless Base
< ProcessWithTest
439 @controller.process_with_test(@request, @response)
442 def xml_http_request(request_method
, action
, parameters
= nil, session
= nil, flash
= nil)
443 @request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
444 @request.env['HTTP_ACCEPT'] = [Mime
::JS, Mime
::HTML, Mime
::XML, 'text/xml', Mime
::ALL].join(', ')
445 returning
__send__(request_method
, action
, parameters
, session
, flash
) do
446 @request.env.delete
'HTTP_X_REQUESTED_WITH'
447 @request.env.delete
'HTTP_ACCEPT'
450 alias xhr
:xml_http_request
452 def assigns(key
= nil)
454 @response.template
.assigns
456 @response.template
.assigns
[key
.to_s
]
473 @response.redirect_url
476 def build_request_uri(action
, parameters
)
477 unless @request.env['REQUEST_URI']
478 options
= @controller.__send__(:rewrite_options, parameters
)
479 options
.update(:only_path => true, :action => action
)
481 url
= ActionController
::UrlRewriter.new(@request, parameters
)
482 @request.set_REQUEST_URI(url
.rewrite(options
))
487 xml
= @response.content_type
=~
/xml$/
488 @html_document ||= HTML
::Document.new(@response.body
, false, xml
)
491 def find_tag(conditions
)
492 html_document
.find(conditions
)
495 def find_all_tag(conditions
)
496 html_document
.find_all(conditions
)
499 def method_missing(selector
, *args
, &block
)
500 if @controller && ActionController
::Routing::Routes.named_routes
.helpers
.include?(selector
)
501 @controller.send(selector
, *args
, &block
)
507 # Shortcut for <tt>ActionController::TestUploadedFile.new(ActionController::TestCase.fixture_path + path, type)</tt>:
509 # post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png')
511 # To upload binary files on Windows, pass <tt>:binary</tt> as the last parameter.
512 # This will not affect other platforms:
514 # post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png', :binary)
515 def fixture_file_upload(path
, mime_type
= nil, binary
= false)
516 fixture_path
= ActionController
::TestCase.send(:fixture_path) if ActionController
::TestCase.respond_to
?(:fixture_path)
517 ActionController
::TestUploadedFile.new("#{fixture_path}#{path}", mime_type
, binary
)
520 # A helper to make it easier to test different route configurations.
521 # This method temporarily replaces ActionController::Routing::Routes
522 # with a new RouteSet instance.
524 # The new instance is yielded to the passed block. Typically the block
525 # will create some routes using <tt>map.draw { map.connect ... }</tt>:
527 # with_routing do |set|
529 # map.connect ':controller/:action/:id'
531 # ['/content/10/show', {}],
532 # map.generate(:controller => 'content', :id => 10, :action => 'show')
538 real_routes
= ActionController
::Routing::Routes
539 ActionController
::Routing.module_eval
{ remove_const
:Routes }
541 temporary_routes
= ActionController
::Routing::RouteSet.new
542 ActionController
::Routing.module_eval
{ const_set
:Routes, temporary_routes
}
544 yield temporary_routes
546 if ActionController
::Routing.const_defined
? :Routes
547 ActionController
::Routing.module_eval
{ remove_const
:Routes }
549 ActionController
::Routing.const_set(:Routes, real_routes
) if real_routes
553 module ProcessWithTest
#:nodoc:
554 def self.included(base
)
555 base
.class_eval
{ attr_reader
:assigns }
558 def process_with_test(*args
)
559 process(*args
).tap
{ set_test_assigns
}
565 (instance_variable_names
- self.class.protected_instance_variables
).each
do |var
|
566 name
, value
= var
[1..-1], instance_variable_get(var
)
567 @assigns[name
] = value
568 response
.template
.assigns
[name
] = value
if response