1 require 'action_controller/assertions'
2 require 'action_controller/test_case'
4 module ActionController
#:nodoc:
8 # Process a test request called with a TestRequest object.
9 def self.process_test(request
)
10 new
.process_test(request
)
13 def process_test(request
) #:nodoc:
14 process(request
, TestResponse
.new
)
17 def process_with_test(*args
)
18 returning
process_without_test(*args
) do
20 (instance_variable_names
- @
@protected_instance_variables).each
do |var
|
21 value
= instance_variable_get(var
)
22 @assigns[var
[1..-1]] = value
23 response
.template
.assigns
[var
[1..-1]] = value
if response
28 alias_method_chain
:process, :test
31 class TestRequest
< AbstractRequest
#:nodoc:
32 attr_accessor
:cookies, :session_options
33 attr_accessor
:query_parameters, :request_parameters, :path, :session
34 attr_accessor
:host, :user_agent
36 def initialize(query_parameters
= nil, request_parameters
= nil, session
= nil)
37 @query_parameters = query_parameters
|| {}
38 @request_parameters = request_parameters
|| {}
39 @session = session
|| TestSession
.new
42 initialize_default_values
48 @session = TestSession
.new
51 # Wraps raw_post in a StringIO.
52 def body_stream
#:nodoc:
53 StringIO
.new(raw_post
)
56 # Either the RAW_POST_DATA environment variable or the URL-encoded request
59 env['RAW_POST_DATA'] ||= returning(url_encoded_request_parameters
) { |b
| b
.force_encoding(Encoding
::BINARY) if b
.respond_to
?(:force_encoding) }
63 @env["SERVER_PORT"] = number
.to_i
67 def action
=(action_name
)
68 @query_parameters.update({ "action" => action_name
})
72 # Used to check AbstractRequest's request_uri functionality.
73 # Disables the use of @path and @request_uri so superclass can handle those.
74 def set_REQUEST_URI(value
)
75 @env["REQUEST_URI"] = value
84 @path = uri
.split("?").first
87 def accept
=(mime_types
)
88 @env["HTTP_ACCEPT"] = Array(mime_types
).collect
{ |mime_types
| mime_types
.to_s
}.join(",")
92 def if_modified_since
=(last_modified
)
93 @env["HTTP_IF_MODIFIED_SINCE"] = last_modified
96 def if_none_match
=(etag
)
97 @env["HTTP_IF_NONE_MATCH"] = etag
100 def remote_addr
=(addr
)
101 @env['REMOTE_ADDR'] = addr
104 def request_uri(*args
)
105 @request_uri || super
112 def assign_parameters(controller_path
, action
, parameters
)
113 parameters
= parameters
.symbolize_keys
.merge(:controller => controller_path
, :action => action
)
114 extra_keys
= ActionController
::Routing::Routes.extra_keys(parameters
)
115 non_path_parameters
= get
? ? query_parameters
: request_parameters
116 parameters
.each
do |key
, value
|
117 if value
.is_a
? Fixnum
119 elsif value
.is_a
? Array
120 value
= ActionController
::Routing::PathSegment::Result.new(value
)
123 if extra_keys
.include?(key
.to_sym
)
124 non_path_parameters
[key
] = value
126 path_parameters
[key
.to_s
] = value
129 @parameters = nil # reset TestRequest#parameters to use the new path_parameters
133 self.request_parameters
= {}
134 self.query_parameters
= {}
135 self.path_parameters
= {}
140 def initialize_containers
141 @env, @cookies = {}, {}
144 def initialize_default_values
147 @user_agent = "Rails Testing"
148 self.remote_addr
= "0.0.0.0"
149 @env["SERVER_PORT"] = 80
150 @env['REQUEST_METHOD'] = "GET"
153 def url_encoded_request_parameters
154 params
= self.request_parameters
.dup
156 %w(controller action only_path
).each
do |k
|
158 params
.delete(k
.to_sym
)
165 # A refactoring of TestResponse to allow the same behavior to be applied
166 # to the "real" CgiResponse class in integration tests.
167 module TestResponseBehavior
#:nodoc:
168 # The response code of the request
170 status
[0,3].to_i
rescue 0
173 # Returns a String to ensure compatibility with Net::HTTPResponse
175 status
.to_s
.split(' ')[0]
179 status
.to_s
.split(' ',2)[1]
182 # Was the response successful?
184 (200..299).include?(response_code
)
187 # Was the URL not found?
192 # Were we redirected?
194 (300..399).include?(response_code
)
197 # Was there a server-side error?
199 (500..599).include?(response_code
)
202 alias_method
:server_error?, :error?
204 # Returns the redirection location or nil
209 # Does the redirect location match this regexp pattern?
210 def redirect_url_match
?( pattern
)
211 return false if redirect_url
.nil?
212 p
= Regexp
.new(pattern
) if pattern
.class == String
213 p
= pattern
if pattern
.class == Regexp
214 return false if p
.nil?
215 p
.match(redirect_url
) != nil
218 # Returns the template of the file which was used to
219 # render this response (or nil)
220 def rendered_template
221 template
.instance_variable_get(:@_first_render)
224 # A shortcut to the flash. Returns an empty hash if no session flash exists.
226 session
['flash'] || {}
229 # Do we have a flash?
231 !session
['flash'].empty
?
234 # Do we have a flash that has contents?
235 def has_flash_with_contents
?
239 # Does the specified flash object exist?
240 def has_flash_object
?(name
=nil)
244 # Does the specified object exist in the session?
245 def has_session_object
?(name
=nil)
249 # A shortcut to the template.assigns
251 template
.assigns
|| {}
254 # Does the specified template object exist?
255 def has_template_object
?(name
=nil)
256 !template_objects
[name
].nil?
259 # Returns the response cookies, converted to a Hash of (name => CGI::Cookie) pairs
261 # assert_equal ['AuthorOfNewPage'], r.cookies['author'].value
263 headers
['cookie'].inject({}) { |hash
, cookie
| hash
[cookie
.name
] = cookie
; hash
}
266 # Returns binary content (downloadable file), converted to a String
268 raise "Response body is not a Proc: #{body.inspect}" unless body
.kind_of
?(Proc
)
279 # Integration test methods such as ActionController::Integration::Session#get
280 # and ActionController::Integration::Session#post return objects of class
281 # TestResponse, which represent the HTTP response results of the requested
282 # controller actions.
284 # See AbstractResponse for more information on controller response objects.
285 class TestResponse
< AbstractResponse
286 include TestResponseBehavior
289 headers
.delete('ETag')
290 headers
.delete('Last-Modified')
294 class TestSession
#:nodoc:
295 attr_accessor
:session_id
297 def initialize(attributes
= nil)
299 @attributes = attributes
.nil? ? nil : attributes
.stringify_keys
300 @saved_attributes = nil
304 @attributes ||= @saved_attributes || {}
312 data[key
.to_s
] = value
316 @saved_attributes = @attributes
329 # Essentially generates a modified Tempfile object similar to the object
330 # you'd get from the standard library CGI module in a multipart
331 # request. This means you can use an ActionController::TestUploadedFile
332 # object in the params of a test request in order to simulate
335 # Usage example, within a functional test:
336 # post :change_avatar, :avatar => ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + '/files/spongebob.png', 'image/png')
338 # Pass a true third parameter to ensure the uploaded file is opened in binary mode (only required for Windows):
339 # post :change_avatar, :avatar => ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + '/files/spongebob.png', 'image/png', :binary)
341 class TestUploadedFile
342 # The filename, *not* including the path, of the "uploaded" file
343 attr_reader
:original_filename
345 # The content type of the "uploaded" file
346 attr_accessor
:content_type
348 def initialize(path
, content_type
= Mime
::TEXT, binary
= false)
349 raise "#{path} file does not exist" unless File
.exist
?(path
)
350 @content_type = content_type
351 @original_filename = path
.sub(/^.*#{File::SEPARATOR}([^#{File::SEPARATOR}]+)$/) { $1 }
352 @tempfile = Tempfile
.new(@original_filename)
353 @tempfile.set_encoding(Encoding
::BINARY) if @tempfile.respond_to
?(:set_encoding)
354 @tempfile.binmode
if binary
355 FileUtils
.copy_file(path
, @tempfile.path
)
362 alias local_path path
364 def method_missing(method_name
, *args
, &block
) #:nodoc:
365 @tempfile.__send__(method_name
, *args
, &block
)
370 def self.included(base
)
371 # execute the request simulating a specific HTTP method and set/volley the response
372 # TODO: this should be un-DRY'ed for the sake of API documentation.
373 %w( get post put delete head
).each
do |method
|
374 base
.class_eval
<<-EOV, __FILE__, __LINE__
375 def #{method}(action, parameters = nil, session = nil, flash = nil)
376 @request.env['REQUEST_METHOD'] = "#{method.upcase}" if defined?(@request)
377 process(action, parameters, session, flash)
383 # execute the request and set/volley the response
384 def process(action
, parameters
= nil, session
= nil, flash
= nil)
385 # Sanity check for required instance variables so we can give an
386 # understandable error message.
387 %w(@controller @request @response).each
do |iv_name
|
388 if !(instance_variable_names
.include?(iv_name
) || instance_variable_names
.include?(iv_name
.to_sym
)) || instance_variable_get(iv_name
).nil?
389 raise "#{iv_name} is nil: make sure you set it in your test's setup method."
397 @request.env['REQUEST_METHOD'] ||= "GET"
399 @request.action
= action
.to_s
402 @request.assign_parameters(@controller.class.controller_path
, action
.to_s
, parameters
)
404 @request.session
= ActionController
::TestSession.new(session
) unless session
.nil?
405 @request.session
["flash"] = ActionController
::Flash::FlashHash.new
.update(flash
) if flash
406 build_request_uri(action
, parameters
)
407 @controller.process(@request, @response)
410 def xml_http_request(request_method
, action
, parameters
= nil, session
= nil, flash
= nil)
411 @request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
412 @request.env['HTTP_ACCEPT'] = 'text/javascript, text/html, application/xml, text/xml, */*'
413 returning
__send__(request_method
, action
, parameters
, session
, flash
) do
414 @request.env.delete
'HTTP_X_REQUESTED_WITH'
415 @request.env.delete
'HTTP_ACCEPT'
418 alias xhr
:xml_http_request
420 def assigns(key
= nil)
422 @response.template
.assigns
424 @response.template
.assigns
[key
.to_s
]
441 @response.redirect_url
444 def build_request_uri(action
, parameters
)
445 unless @request.env['REQUEST_URI']
446 options
= @controller.__send__(:rewrite_options, parameters
)
447 options
.update(:only_path => true, :action => action
)
449 url
= ActionController
::UrlRewriter.new(@request, parameters
)
450 @request.set_REQUEST_URI(url
.rewrite(options
))
455 xml
= @response.content_type
=~
/xml$/
456 @html_document ||= HTML
::Document.new(@response.body
, false, xml
)
459 def find_tag(conditions
)
460 html_document
.find(conditions
)
463 def find_all_tag(conditions
)
464 html_document
.find_all(conditions
)
467 def method_missing(selector
, *args
)
468 if ActionController
::Routing::Routes.named_routes
.helpers
.include?(selector
)
469 @controller.send(selector
, *args
)
475 # Shortcut for <tt>ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + path, type)</tt>:
477 # post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png')
479 # To upload binary files on Windows, pass <tt>:binary</tt> as the last parameter.
480 # This will not affect other platforms:
482 # post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png', :binary)
483 def fixture_file_upload(path
, mime_type
= nil, binary
= false)
484 ActionController
::TestUploadedFile.new(
485 Test
::Unit::TestCase.respond_to
?(:fixture_path) ? Test
::Unit::TestCase.fixture_path
+ path
: path
,
491 # A helper to make it easier to test different route configurations.
492 # This method temporarily replaces ActionController::Routing::Routes
493 # with a new RouteSet instance.
495 # The new instance is yielded to the passed block. Typically the block
496 # will create some routes using <tt>map.draw { map.connect ... }</tt>:
498 # with_routing do |set|
500 # map.connect ':controller/:action/:id'
502 # ['/content/10/show', {}],
503 # map.generate(:controller => 'content', :id => 10, :action => 'show')
509 real_routes
= ActionController
::Routing::Routes
510 ActionController
::Routing.module_eval
{ remove_const
:Routes }
512 temporary_routes
= ActionController
::Routing::RouteSet.new
513 ActionController
::Routing.module_eval
{ const_set
:Routes, temporary_routes
}
515 yield temporary_routes
517 if ActionController
::Routing.const_defined
? :Routes
518 ActionController
::Routing.module_eval
{ remove_const
:Routes }
520 ActionController
::Routing.const_set(:Routes, real_routes
) if real_routes
527 class TestCase
#:nodoc:
528 include ActionController
::TestProcess