Skip to content

Commit

Permalink
Start moving TestRequest and TestResponse into ActionDispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Apr 30, 2009
1 parent 64e66cf commit 00d1a57
Show file tree
Hide file tree
Showing 14 changed files with 279 additions and 253 deletions.
3 changes: 2 additions & 1 deletion actionpack/lib/action_controller/base/base.rb
Expand Up @@ -817,7 +817,8 @@ def _process_options(options)
end

def initialize_template_class(response)
@template = response.template = ActionView::Base.new(self.class.view_paths, {}, self, formats)
@template = ActionView::Base.new(self.class.view_paths, {}, self, formats)
response.template = @template if response.respond_to?(:template=)
@template.helpers.send :include, self.class.master_helper_module
response.redirected_to = nil
@performed_render = @performed_redirect = false
Expand Down
11 changes: 3 additions & 8 deletions actionpack/lib/action_controller/testing/integration.rb
Expand Up @@ -245,7 +245,7 @@ def process(method, path, parameters = nil, headers = nil)
ActionController::Base.clear_last_instantiation!

opts = {
:method => method.to_s.upcase,
:method => method,
:params => parameters,

"SERVER_NAME" => host,
Expand Down Expand Up @@ -276,17 +276,12 @@ def process(method, path, parameters = nil, headers = nil)
mock_response = ::Rack::MockResponse.new(status, headers, body)

@request_count += 1
@request = Request.new(env)
@response = Response.from_response(mock_response)
@request = ActionDispatch::Request.new(env)
@response = ActionDispatch::TestResponse.from_response(mock_response)

@cookies.merge!(@response.cookies)
@html_document = nil

# Decorate the response with the standard behavior of the
# TestResponse so that things like assert_response can be
# used in integration tests.
@response.extend(TestResponseBehavior)

if @controller = ActionController::Base.last_instantiation
@controller.send(:set_test_assigns)
end
Expand Down
170 changes: 14 additions & 156 deletions 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.
Expand All @@ -36,55 +31,8 @@ def raw_post
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)
Expand All @@ -105,34 +53,15 @@ def assign_parameters(controller_path, action, parameters)
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

Expand All @@ -145,84 +74,13 @@ def url_encoded_request_parameters
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')
Expand Down Expand Up @@ -293,7 +151,7 @@ def process(action, parameters = nil, session = nil, flash = nil, http_method =
@response.recycle!

@html_document = nil
@request.env['REQUEST_METHOD'] = http_method
@request.request_method = http_method

@request.action = action.to_s

Expand Down Expand Up @@ -331,7 +189,7 @@ def session
end

def flash
@response.flash
@request.flash
end

def cookies
Expand All @@ -348,7 +206,7 @@ def build_request_uri(action, parameters)
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

Expand Down
2 changes: 2 additions & 0 deletions actionpack/lib/action_dispatch.rb
Expand Up @@ -47,6 +47,8 @@ module ActionDispatch
autoload :MiddlewareStack, 'action_dispatch/middleware/stack'

autoload :Assertions, 'action_dispatch/testing/assertions'
autoload :TestRequest, 'action_dispatch/testing/test_request'
autoload :TestResponse, 'action_dispatch/testing/test_response'

module Http
autoload :Headers, 'action_dispatch/http/headers'
Expand Down
1 change: 0 additions & 1 deletion actionpack/lib/action_dispatch/extensions/rack/utils.rb
Expand Up @@ -83,7 +83,6 @@ def method_missing(method_name, *args, &block) #:nodoc:
end
end

EOL = "\r\n"
MULTIPART_BOUNDARY = "AaB03x"

def self.parse_multipart(env)
Expand Down

0 comments on commit 00d1a57

Please sign in to comment.