diff options
Diffstat (limited to 'actionpack/lib/action_controller/testing')
-rw-r--r-- | actionpack/lib/action_controller/testing/integration.rb | 25 | ||||
-rw-r--r-- | actionpack/lib/action_controller/testing/process.rb | 22 | ||||
-rw-r--r-- | actionpack/lib/action_controller/testing/test_case.rb | 13 |
3 files changed, 44 insertions, 16 deletions
diff --git a/actionpack/lib/action_controller/testing/integration.rb b/actionpack/lib/action_controller/testing/integration.rb index 0da23f9dc8..d51b9b63ff 100644 --- a/actionpack/lib/action_controller/testing/integration.rb +++ b/actionpack/lib/action_controller/testing/integration.rb @@ -5,7 +5,7 @@ require 'active_support/test_case' module ActionController module Integration #:nodoc: # An integration Session instance represents a set of requests and responses - # performed sequentially by some virtual user. Becase you can instantiate + # performed sequentially by some virtual user. Because you can instantiate # multiple sessions and run them side-by-side, you can also mimic (to some # limited extent) multiple simultaneous users interacting with your system. # @@ -26,6 +26,9 @@ module ActionController # The status message that accompanied the status code of the last request. attr_reader :status_message + # The body of the last request. + attr_reader :body + # The URI of the last request. attr_reader :path @@ -308,7 +311,11 @@ module ActionController ActionController::Base.clear_last_instantiation! - app = Rack::Lint.new(@application) + app = @application + # Rack::Lint doesn't accept String headers or bodies in Ruby 1.9 + unless RUBY_VERSION >= '1.9.0' && Rack.release <= '0.9.0' + app = Rack::Lint.new(app) + end status, headers, body = app.call(env) @request_count += 1 @@ -320,13 +327,19 @@ module ActionController @headers = Rack::Utils::HeaderHash.new(headers) - (@headers['Set-Cookie'] || []).each do |cookie| + (@headers['Set-Cookie'] || "").split("\n").each do |cookie| name, value = cookie.match(/^([^=]*)=([^;]*);/)[1,2] @cookies[name] = value end - @body = "" - body.each { |part| @body << part } + if body.is_a?(String) + @body_parts = [body] + @body = body + else + @body_parts = [] + body.each { |part| @body_parts << part.to_s } + @body = @body_parts.join + end if @controller = ActionController::Base.last_instantiation @request = @controller.request @@ -338,7 +351,7 @@ module ActionController @response = ActionDispatch::Response.new @response.status = status.to_s @response.headers.replace(@headers) - @response.body = @body + @response.body = @body_parts end # Decorate the response with the standard behavior of the diff --git a/actionpack/lib/action_controller/testing/process.rb b/actionpack/lib/action_controller/testing/process.rb index 38e37c7a18..86e193efa9 100644 --- a/actionpack/lib/action_controller/testing/process.rb +++ b/actionpack/lib/action_controller/testing/process.rb @@ -1,14 +1,21 @@ +require 'rack/session/abstract/id' module ActionController #:nodoc: class TestRequest < ActionDispatch::Request #:nodoc: attr_accessor :cookies, :session_options attr_accessor :query_parameters, :path, :session attr_accessor :host - def initialize - super(Rack::MockRequest.env_for("/")) + def self.new(env = {}) + super + end + + def initialize(env = {}) + super(Rack::MockRequest.env_for("/").merge(env)) @query_parameters = {} @session = TestSession.new + default_rack_options = Rack::Session::Abstract::ID::DEFAULT_OPTIONS + @session_options ||= {:id => generate_sid(default_rack_options[:sidbits])}.merge(default_rack_options) initialize_default_values initialize_containers @@ -106,6 +113,7 @@ module ActionController #:nodoc: end def recycle! + @env["action_controller.request.request_parameters"] = {} self.query_parameters = {} self.path_parameters = {} @headers, @request_method, @accepts, @content_type = nil, nil, nil, nil @@ -116,6 +124,10 @@ module ActionController #:nodoc: end private + def generate_sid(sidbits) + "%0#{sidbits / 4}x" % rand(2**sidbits - 1) + end + def initialize_containers @cookies = {} end @@ -246,7 +258,7 @@ module ActionController #:nodoc: def cookies cookies = {} Array(headers['Set-Cookie']).each do |cookie| - key, value = cookie.split(";").first.split("=") + key, value = cookie.split(";").first.split("=").map {|val| Rack::Utils.unescape(val)} cookies[key] = value end cookies @@ -254,11 +266,11 @@ module ActionController #:nodoc: # Returns binary content (downloadable file), converted to a String def binary_content - raise "Response body is not a Proc: #{body.inspect}" unless body.kind_of?(Proc) + raise "Response body is not a Proc: #{body_parts.inspect}" unless body_parts.kind_of?(Proc) require 'stringio' sio = StringIO.new - body.call(self, sio) + body_parts.call(self, sio) sio.rewind sio.read diff --git a/actionpack/lib/action_controller/testing/test_case.rb b/actionpack/lib/action_controller/testing/test_case.rb index 4f38f75f41..b020b755a0 100644 --- a/actionpack/lib/action_controller/testing/test_case.rb +++ b/actionpack/lib/action_controller/testing/test_case.rb @@ -127,9 +127,14 @@ module ActionController # # The exception is stored in the exception accessor for further inspection. module RaiseActionExceptions - protected - attr_accessor :exception + def self.included(base) + base.class_eval do + attr_accessor :exception + protected :exception, :exception= + end + end + protected def rescue_action_without_handler(e) self.exception = e @@ -141,9 +146,7 @@ module ActionController end end - def setup - setup_controller_request_and_response - end + setup :setup_controller_request_and_response @@controller_class = nil |