diff options
author | Joshua Peek <josh@joshpeek.com> | 2009-04-30 17:26:03 -0500 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2009-04-30 17:26:03 -0500 |
commit | 00d1a57e9f99a1b2439281cb741fd82ef47a5c55 (patch) | |
tree | 3fafedd0ef5870df4c8d603aab7dadf3f833a977 /actionpack/lib/action_controller/testing/process.rb | |
parent | 64e66cf161ee8db0bdbccb1be18fb760f5a9d24e (diff) | |
download | rails-00d1a57e9f99a1b2439281cb741fd82ef47a5c55.tar.gz rails-00d1a57e9f99a1b2439281cb741fd82ef47a5c55.tar.bz2 rails-00d1a57e9f99a1b2439281cb741fd82ef47a5c55.zip |
Start moving TestRequest and TestResponse into ActionDispatch
Diffstat (limited to 'actionpack/lib/action_controller/testing/process.rb')
-rw-r--r-- | actionpack/lib/action_controller/testing/process.rb | 170 |
1 files changed, 14 insertions, 156 deletions
diff --git a/actionpack/lib/action_controller/testing/process.rb b/actionpack/lib/action_controller/testing/process.rb index 3a7445f890..e0f8f026fd 100644 --- a/actionpack/lib/action_controller/testing/process.rb +++ b/actionpack/lib/action_controller/testing/process.rb @@ -1,24 +1,19 @@ require 'rack/session/abstract/id' module ActionController #:nodoc: - class TestRequest < ActionDispatch::Request #:nodoc: + class TestRequest < ActionDispatch::TestRequest #:nodoc: attr_accessor :cookies - attr_accessor :query_parameters, :path - attr_accessor :host - - def self.new(env = {}) - super - end + attr_accessor :query_parameters def initialize(env = {}) - super(Rack::MockRequest.env_for("/").merge(env)) + super - @query_parameters = {} + @query_parameters = {} self.session = TestSession.new self.session_options = TestSession::DEFAULT_OPTIONS.merge(:id => ActiveSupport::SecureRandom.hex(16)) - initialize_default_values - initialize_containers + @request_uri = "/" + @cookies = {} end # Wraps raw_post in a StringIO. @@ -36,55 +31,8 @@ module ActionController #:nodoc: end end - def port=(number) - @env["SERVER_PORT"] = number.to_i - end - def action=(action_name) - @query_parameters.update({ "action" => action_name }) - @parameters = nil - end - - # Used to check AbstractRequest's request_uri functionality. - # Disables the use of @path and @request_uri so superclass can handle those. - def set_REQUEST_URI(value) - @env["REQUEST_URI"] = value - @request_uri = nil - @path = nil - end - - def request_uri=(uri) - @request_uri = uri - @path = uri.split("?").first - end - - def request_method=(method) - @request_method = method - end - - def accept=(mime_types) - @env["HTTP_ACCEPT"] = Array(mime_types).collect { |mime_types| mime_types.to_s }.join(",") - @accepts = nil - end - - def if_modified_since=(last_modified) - @env["HTTP_IF_MODIFIED_SINCE"] = last_modified - end - - def if_none_match=(etag) - @env["HTTP_IF_NONE_MATCH"] = etag - end - - def remote_addr=(addr) - @env['REMOTE_ADDR'] = addr - end - - def request_uri(*args) - @request_uri || super() - end - - def path(*args) - @path || super() + query_parameters.update({ "action" => action_name }) end def assign_parameters(controller_path, action, parameters) @@ -105,34 +53,15 @@ module ActionController #:nodoc: end end raw_post # populate env['RAW_POST_DATA'] - @parameters = nil # reset TestRequest#parameters to use the new path_parameters end def recycle! - @env["action_dispatch.request.request_parameters"] = {} - self.query_parameters = {} - self.path_parameters = {} - @headers, @request_method, @accepts, @content_type = nil, nil, nil, nil - end - - def user_agent=(user_agent) - @env['HTTP_USER_AGENT'] = user_agent + @env.delete_if { |k, v| k =~ /^action_dispatch\.request/ } + self.query_parameters = {} + @headers = nil end private - def initialize_containers - @cookies = {} - end - - def initialize_default_values - @host = "test.host" - @request_uri = "/" - @env['HTTP_USER_AGENT'] = "Rails Testing" - @env['REMOTE_ADDR'] = "0.0.0.0" - @env["SERVER_PORT"] = 80 - @env['REQUEST_METHOD'] = "GET" - end - def url_encoded_request_parameters params = self.request_parameters.dup @@ -145,84 +74,13 @@ module ActionController #:nodoc: end end - # A refactoring of TestResponse to allow the same behavior to be applied - # to the "real" CgiResponse class in integration tests. - module TestResponseBehavior #:nodoc: - def redirect_url_match?(pattern) - ::ActiveSupport::Deprecation.warn("response.redirect_url_match? is deprecated. Use assert_match(/foo/, response.redirect_url) instead", caller) - 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 - - # Returns the template of the file which was used to - # render this response (or nil) - def rendered - ActiveSupport::Deprecation.warn("response.rendered has been deprecated. Use tempate.rendered instead", caller) - @template.instance_variable_get(:@_rendered) - end - - # A shortcut to the flash. Returns an empty hash if no session flash exists. - def flash - request.session['flash'] || {} - end - - # Do we have a flash? - def has_flash? - !flash.empty? - end - - # Do we have a flash that has contents? - def has_flash_with_contents? - !flash.empty? - end - - # Does the specified flash object exist? - def has_flash_object?(name=nil) - !flash[name].nil? - end - - # Does the specified object exist in the session? - def has_session_object?(name=nil) - !session[name].nil? - end - - # A shortcut to the template.assigns - def template_objects - ActiveSupport::Deprecation.warn("response.template_objects has been deprecated. Use tempate.assigns instead", caller) - @template.assigns || {} - end - - # Does the specified template object exist? - def has_template_object?(name=nil) - ActiveSupport::Deprecation.warn("response.has_template_object? has been deprecated. Use tempate.assigns[name].nil? instead", caller) - !template_objects[name].nil? - end - - # Returns binary content (downloadable file), converted to a String - def binary_content - raise "Response body is not a Proc: #{body_parts.inspect}" unless body_parts.kind_of?(Proc) - require 'stringio' - - sio = StringIO.new - body_parts.call(self, sio) - - sio.rewind - sio.read - end - end - # Integration test methods such as ActionController::Integration::Session#get # and ActionController::Integration::Session#post return objects of class # TestResponse, which represent the HTTP response results of the requested # controller actions. # # See Response for more information on controller response objects. - class TestResponse < ActionDispatch::Response - include TestResponseBehavior - + class TestResponse < ActionDispatch::TestResponse def recycle! body_parts.clear headers.delete('ETag') @@ -293,7 +151,7 @@ module ActionController #:nodoc: @response.recycle! @html_document = nil - @request.env['REQUEST_METHOD'] = http_method + @request.request_method = http_method @request.action = action.to_s @@ -331,7 +189,7 @@ module ActionController #:nodoc: end def flash - @response.flash + @request.flash end def cookies @@ -348,7 +206,7 @@ module ActionController #:nodoc: options.update(:only_path => true, :action => action) url = ActionController::UrlRewriter.new(@request, parameters) - @request.set_REQUEST_URI(url.rewrite(options)) + @request.request_uri = url.rewrite(options) end end |