aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorYehuda Katz <wycats@gmail.com>2009-08-26 00:18:52 -0700
committerYehuda Katz <wycats@gmail.com>2009-08-26 00:18:52 -0700
commit9408fcd2e858ae48dd30d9e8d1bb1dcbbfffb840 (patch)
treec16d280b988d662041fc5834d07078d9da6f2b37 /actionpack
parent78129b1731a1e6f3b091e996bcf55917d84b5f0e (diff)
downloadrails-9408fcd2e858ae48dd30d9e8d1bb1dcbbfffb840.tar.gz
rails-9408fcd2e858ae48dd30d9e8d1bb1dcbbfffb840.tar.bz2
rails-9408fcd2e858ae48dd30d9e8d1bb1dcbbfffb840.zip
Create new ActionController::Middleware class that will work as a normal Rack middleware.
* This initial implementation is a bit hackish, but it uses a normal middleware API so it's future-proof when we improve the internals.
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/abstract_controller/base.rb1
-rw-r--r--actionpack/lib/action_controller.rb1
-rw-r--r--actionpack/lib/action_controller/metal.rb22
-rw-r--r--actionpack/lib/action_controller/middleware.rb34
-rw-r--r--actionpack/test/abstract_controller/abstract_controller_test.rb53
-rw-r--r--actionpack/test/abstract_controller/callbacks_test.rb94
-rw-r--r--actionpack/test/abstract_controller/helper_test.rb5
-rw-r--r--actionpack/test/abstract_controller/layouts_test.rb55
-rw-r--r--actionpack/test/new_base/metal_test.rb11
9 files changed, 166 insertions, 110 deletions
diff --git a/actionpack/lib/abstract_controller/base.rb b/actionpack/lib/abstract_controller/base.rb
index b93e6ce634..f5b1c9e4d1 100644
--- a/actionpack/lib/abstract_controller/base.rb
+++ b/actionpack/lib/abstract_controller/base.rb
@@ -89,7 +89,6 @@ module AbstractController
end
process_action(action_name)
- self
end
private
diff --git a/actionpack/lib/action_controller.rb b/actionpack/lib/action_controller.rb
index 37ff618edd..d27a867efe 100644
--- a/actionpack/lib/action_controller.rb
+++ b/actionpack/lib/action_controller.rb
@@ -3,6 +3,7 @@ module ActionController
autoload :ConditionalGet, "action_controller/metal/conditional_get"
autoload :HideActions, "action_controller/metal/hide_actions"
autoload :Metal, "action_controller/metal"
+ autoload :Middleware, "action_controller/middleware"
autoload :Layouts, "action_controller/metal/layouts"
autoload :RackConvenience, "action_controller/metal/rack_convenience"
autoload :Rails2Compatibility, "action_controller/metal/compatibility"
diff --git a/actionpack/lib/action_controller/metal.rb b/actionpack/lib/action_controller/metal.rb
index aad9570237..51fbba3661 100644
--- a/actionpack/lib/action_controller/metal.rb
+++ b/actionpack/lib/action_controller/metal.rb
@@ -88,23 +88,6 @@ module ActionController
end
end
- class ActionMiddleware
- def initialize(controller, action)
- @controller, @action = controller, action
- end
-
- def call(env)
- controller = @controller.new
- controller.app = @app
- controller.call(@action, env)
- end
-
- def new(app)
- @app = app
- self
- end
- end
-
# Return a rack endpoint for the given action. Memoize the endpoint, so
# multiple calls into MyController.action will return the same object
# for the same action.
@@ -118,10 +101,5 @@ module ActionController
@actions ||= {}
@actions[name.to_s] ||= ActionEndpoint.new(self, name)
end
-
- def self.middleware(name)
- @middlewares ||= {}
- @middlewares[name.to_s] ||= ActionMiddleware.new(self, name)
- end
end
end
diff --git a/actionpack/lib/action_controller/middleware.rb b/actionpack/lib/action_controller/middleware.rb
new file mode 100644
index 0000000000..5fccca0b48
--- /dev/null
+++ b/actionpack/lib/action_controller/middleware.rb
@@ -0,0 +1,34 @@
+module ActionController
+ class Middleware < Metal
+ class ActionMiddleware
+ def initialize(controller)
+ @controller = controller
+ end
+
+ def call(env)
+ controller = @controller.allocate
+ controller.app = @app
+ controller._call(env)
+ end
+
+ def app=(app)
+ @app = app
+ end
+ end
+
+ def self.new(app)
+ middleware = ActionMiddleware.new(self)
+ middleware.app = app
+ middleware
+ end
+
+ def _call(env)
+ @_env = env
+ process(:index)
+ end
+
+ def index
+ call(env)
+ end
+ end
+end \ No newline at end of file
diff --git a/actionpack/test/abstract_controller/abstract_controller_test.rb b/actionpack/test/abstract_controller/abstract_controller_test.rb
index 9438a4dfc9..7991436703 100644
--- a/actionpack/test/abstract_controller/abstract_controller_test.rb
+++ b/actionpack/test/abstract_controller/abstract_controller_test.rb
@@ -19,8 +19,9 @@ module AbstractController
class TestBasic < ActiveSupport::TestCase
test "dispatching works" do
- result = Me.new.process(:index)
- assert_equal "Hello world", result.response_body
+ controller = Me.new
+ controller.process(:index)
+ assert_equal "Hello world", controller.response_body
end
end
@@ -67,29 +68,33 @@ module AbstractController
end
class TestRenderingController < ActiveSupport::TestCase
+ def setup
+ @controller = Me2.new
+ end
+
test "rendering templates works" do
- result = Me2.new.process(:index)
- assert_equal "Hello from index.erb", result.response_body
+ @controller.process(:index)
+ assert_equal "Hello from index.erb", @controller.response_body
end
test "rendering passes ivars to the view" do
- result = Me2.new.process(:action_with_ivars)
- assert_equal "Hello from index_with_ivars.erb", result.response_body
+ @controller.process(:action_with_ivars)
+ assert_equal "Hello from index_with_ivars.erb", @controller.response_body
end
test "rendering with no template name" do
- result = Me2.new.process(:naked_render)
- assert_equal "Hello from naked_render.erb", result.response_body
+ @controller.process(:naked_render)
+ assert_equal "Hello from naked_render.erb", @controller.response_body
end
test "rendering to a rack body" do
- result = Me2.new.process(:rendering_to_body)
- assert_equal "Hello from naked_render.erb", result.response_body
+ @controller.process(:rendering_to_body)
+ assert_equal "Hello from naked_render.erb", @controller.response_body
end
test "rendering to a string" do
- result = Me2.new.process(:rendering_to_string)
- assert_equal "Hello from naked_render.erb", result.response_body
+ @controller.process(:rendering_to_string)
+ assert_equal "Hello from naked_render.erb", @controller.response_body
end
end
@@ -119,14 +124,18 @@ module AbstractController
end
class TestPrefixedViews < ActiveSupport::TestCase
+ def setup
+ @controller = Me3.new
+ end
+
test "templates are located inside their 'prefix' folder" do
- result = Me3.new.process(:index)
- assert_equal "Hello from me3/index.erb", result.response_body
+ @controller.process(:index)
+ assert_equal "Hello from me3/index.erb", @controller.response_body
end
test "templates included their format" do
- result = Me3.new.process(:formatted)
- assert_equal "Hello from me3/formatted.html.erb", result.response_body
+ @controller.process(:formatted)
+ assert_equal "Hello from me3/formatted.html.erb", @controller.response_body
end
end
@@ -168,8 +177,9 @@ module AbstractController
class TestLayouts < ActiveSupport::TestCase
test "layouts are included" do
- result = Me4.new.process(:index)
- assert_equal "Me4 Enter : Hello from me4/index.erb : Exit", result.response_body
+ controller = Me4.new
+ result = controller.process(:index)
+ assert_equal "Me4 Enter : Hello from me4/index.erb : Exit", controller.response_body
end
end
@@ -203,10 +213,11 @@ module AbstractController
end
class TestRespondToAction < ActiveSupport::TestCase
-
+
def assert_dispatch(klass, body = "success", action = :index)
- response = klass.new.process(action).response_body
- assert_equal body, response
+ controller = klass.new
+ controller.process(action)
+ assert_equal body, controller.response_body
end
test "an arbitrary method is available as an action by default" do
diff --git a/actionpack/test/abstract_controller/callbacks_test.rb b/actionpack/test/abstract_controller/callbacks_test.rb
index 817f60f7d1..8f62adce8c 100644
--- a/actionpack/test/abstract_controller/callbacks_test.rb
+++ b/actionpack/test/abstract_controller/callbacks_test.rb
@@ -19,10 +19,11 @@ module AbstractController
end
end
- class TestCallbacks < ActiveSupport::TestCase
+ class TestCallbacks1 < ActiveSupport::TestCase
test "basic callbacks work" do
- result = Callback1.new.process(:index)
- assert_equal "Hello world", result.response_body
+ controller = Callback1.new
+ result = controller.process(:index)
+ assert_equal "Hello world", controller.response_body
end
end
@@ -50,20 +51,24 @@ module AbstractController
end
end
- class TestCallbacks < ActiveSupport::TestCase
+ class TestCallbacks2 < ActiveSupport::TestCase
+ def setup
+ @controller = Callback2.new
+ end
+
test "before_filter works" do
- result = Callback2.new.process(:index)
- assert_equal "Hello world", result.response_body
+ result = @controller.process(:index)
+ assert_equal "Hello world", @controller.response_body
end
test "after_filter works" do
- result = Callback2.new.process(:index)
- assert_equal "Goodbye", result.instance_variable_get("@second")
+ @controller.process(:index)
+ assert_equal "Goodbye", @controller.instance_variable_get("@second")
end
test "around_filter works" do
- result = Callback2.new.process(:index)
- assert_equal "FIRSTSECOND", result.instance_variable_get("@aroundz")
+ @controller.process(:index)
+ assert_equal "FIRSTSECOND", @controller.instance_variable_get("@aroundz")
end
end
@@ -81,15 +86,19 @@ module AbstractController
end
end
- class TestCallbacks < ActiveSupport::TestCase
+ class TestCallbacks3 < ActiveSupport::TestCase
+ def setup
+ @controller = Callback3.new
+ end
+
test "before_filter works with procs" do
- result = Callback3.new.process(:index)
- assert_equal "Hello world", result.response_body
+ result = @controller.process(:index)
+ assert_equal "Hello world", @controller.response_body
end
test "after_filter works with procs" do
- result = Callback3.new.process(:index)
- assert_equal "Goodbye", result.instance_variable_get("@second")
+ result = @controller.process(:index)
+ assert_equal "Goodbye", @controller.instance_variable_get("@second")
end
end
@@ -116,20 +125,24 @@ module AbstractController
end
end
- class TestCallbacks < ActiveSupport::TestCase
+ class TestCallbacksWithConditions < ActiveSupport::TestCase
+ def setup
+ @controller = CallbacksWithConditions.new
+ end
+
test "when :only is specified, a before filter is triggered on that action" do
- result = CallbacksWithConditions.new.process(:index)
- assert_equal "Hello, World", result.response_body
+ @controller.process(:index)
+ assert_equal "Hello, World", @controller.response_body
end
test "when :only is specified, a before filter is not triggered on other actions" do
- result = CallbacksWithConditions.new.process(:sekrit_data)
- assert_equal "true", result.response_body
+ @controller.process(:sekrit_data)
+ assert_equal "true", @controller.response_body
end
test "when :except is specified, an after filter is not triggered on that action" do
- result = CallbacksWithConditions.new.process(:index)
- assert_nil result.instance_variable_get("@authenticated")
+ result = @controller.process(:index)
+ assert_nil @controller.instance_variable_get("@authenticated")
end
end
@@ -156,20 +169,24 @@ module AbstractController
end
end
- class TestCallbacks < ActiveSupport::TestCase
+ class TestCallbacksWithArrayConditions < ActiveSupport::TestCase
+ def setup
+ @controller = CallbacksWithArrayConditions.new
+ end
+
test "when :only is specified with an array, a before filter is triggered on that action" do
- result = CallbacksWithArrayConditions.new.process(:index)
- assert_equal "Hello, World", result.response_body
+ result = @controller.process(:index)
+ assert_equal "Hello, World", @controller.response_body
end
test "when :only is specified with an array, a before filter is not triggered on other actions" do
- result = CallbacksWithArrayConditions.new.process(:sekrit_data)
- assert_equal "true", result.response_body
+ result = @controller.process(:sekrit_data)
+ assert_equal "true", @controller.response_body
end
test "when :except is specified with an array, an after filter is not triggered on that action" do
- result = CallbacksWithArrayConditions.new.process(:index)
- assert_nil result.instance_variable_get("@authenticated")
+ result = @controller.process(:index)
+ assert_nil @controller.instance_variable_get("@authenticated")
end
end
@@ -181,15 +198,19 @@ module AbstractController
end
end
- class TestCallbacks < ActiveSupport::TestCase
+ class TestCallbacksWithChangedConditions < ActiveSupport::TestCase
+ def setup
+ @controller = ChangedConditions.new
+ end
+
test "when a callback is modified in a child with :only, it works for the :only action" do
- result = ChangedConditions.new.process(:index)
- assert_equal "Hello world", result.response_body
+ result = @controller.process(:index)
+ assert_equal "Hello world", @controller.response_body
end
test "when a callback is modified in a child with :only, it does not work for other actions" do
- result = ChangedConditions.new.process(:not_index)
- assert_equal "", result.response_body
+ result = @controller.process(:not_index)
+ assert_equal "", @controller.response_body
end
end
@@ -207,8 +228,9 @@ module AbstractController
class TestHalting < ActiveSupport::TestCase
test "when a callback sets the response body, the action should not be invoked" do
- result = SetsResponseBody.new.process(:index)
- assert_equal "Success", result.response_body
+ controller = SetsResponseBody.new
+ controller.process(:index)
+ assert_equal "Success", controller.response_body
end
end
diff --git a/actionpack/test/abstract_controller/helper_test.rb b/actionpack/test/abstract_controller/helper_test.rb
index e9a60c0307..34a10cecc9 100644
--- a/actionpack/test/abstract_controller/helper_test.rb
+++ b/actionpack/test/abstract_controller/helper_test.rb
@@ -34,8 +34,9 @@ module AbstractController
class TestHelpers < ActiveSupport::TestCase
def test_helpers
- result = MyHelpers1.new.process(:index)
- assert_equal "Hello World : Included", result.response_body
+ controller = MyHelpers1.new
+ controller.process(:index)
+ assert_equal "Hello World : Included", controller.response_body
end
end
diff --git a/actionpack/test/abstract_controller/layouts_test.rb b/actionpack/test/abstract_controller/layouts_test.rb
index 42f73faa61..995aac7fad 100644
--- a/actionpack/test/abstract_controller/layouts_test.rb
+++ b/actionpack/test/abstract_controller/layouts_test.rb
@@ -143,13 +143,15 @@ module AbstractControllerTests
class TestBase < ActiveSupport::TestCase
test "when no layout is specified, and no default is available, render without a layout" do
- result = Blank.new.process(:index)
- assert_equal "Hello blank!", result.response_body
+ controller = Blank.new
+ controller.process(:index)
+ assert_equal "Hello blank!", controller.response_body
end
test "when layout is specified as a string, render with that layout" do
- result = WithString.new.process(:index)
- assert_equal "With String Hello string!", result.response_body
+ controller = WithString.new
+ controller.process(:index)
+ assert_equal "With String Hello string!", controller.response_body
end
test "when layout is specified as a string, but the layout is missing, raise an exception" do
@@ -157,23 +159,27 @@ module AbstractControllerTests
end
test "when layout is specified as false, do not use a layout" do
- result = WithFalseLayout.new.process(:index)
- assert_equal "Hello false!", result.response_body
+ controller = WithFalseLayout.new
+ controller.process(:index)
+ assert_equal "Hello false!", controller.response_body
end
test "when layout is specified as nil, do not use a layout" do
- result = WithNilLayout.new.process(:index)
- assert_equal "Hello nil!", result.response_body
+ controller = WithNilLayout.new
+ controller.process(:index)
+ assert_equal "Hello nil!", controller.response_body
end
test "when layout is specified as a symbol, call the requested method and use the layout returned" do
- result = WithSymbol.new.process(:index)
- assert_equal "OMGHI2U Hello symbol!", result.response_body
+ controller = WithSymbol.new
+ controller.process(:index)
+ assert_equal "OMGHI2U Hello symbol!", controller.response_body
end
test "when layout is specified as a symbol and the method returns nil, don't use a layout" do
- result = WithSymbolReturningNil.new.process(:index)
- assert_equal "Hello nilz!", result.response_body
+ controller = WithSymbolReturningNil.new
+ controller.process(:index)
+ assert_equal "Hello nilz!", controller.response_body
end
test "when the layout is specified as a symbol and the method doesn't exist, raise an exception" do
@@ -185,29 +191,34 @@ module AbstractControllerTests
end
test "when a child controller does not have a layout, use the parent controller layout" do
- result = WithStringChild.new.process(:index)
- assert_equal "With String Hello string!", result.response_body
+ controller = WithStringChild.new
+ controller.process(:index)
+ assert_equal "With String Hello string!", controller.response_body
end
test "when a child controller has specified a layout, use that layout and not the parent controller layout" do
- result = WithStringOverriddenChild.new.process(:index)
- assert_equal "With Override Hello string!", result.response_body
+ controller = WithStringOverriddenChild.new
+ controller.process(:index)
+ assert_equal "With Override Hello string!", controller.response_body
end
test "when a child controller has an implied layout, use that layout and not the parent controller layout" do
- result = WithStringImpliedChild.new.process(:index)
- assert_equal "With Implied Hello string!", result.response_body
+ controller = WithStringImpliedChild.new
+ controller.process(:index)
+ assert_equal "With Implied Hello string!", controller.response_body
end
test "when a child controller specifies layout nil, do not use the parent layout" do
- result = WithNilChild.new.process(:index)
- assert_equal "Hello string!", result.response_body
+ controller = WithNilChild.new
+ controller.process(:index)
+ assert_equal "Hello string!", controller.response_body
end
test "when a grandchild has no layout specified, the child has an implied layout, and the " \
"parent has specified a layout, use the child controller layout" do
- result = WithChildOfImplied.new.process(:index)
- assert_equal "With Implied Hello string!", result.response_body
+ controller = WithChildOfImplied.new
+ controller.process(:index)
+ assert_equal "With Implied Hello string!", controller.response_body
end
test "raises an exception when specifying layout true" do
diff --git a/actionpack/test/new_base/metal_test.rb b/actionpack/test/new_base/metal_test.rb
index c7c45b5cc9..2b7720863a 100644
--- a/actionpack/test/new_base/metal_test.rb
+++ b/actionpack/test/new_base/metal_test.rb
@@ -1,13 +1,12 @@
require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper")
module MetalTest
- class MetalMiddleware < ActionController::Metal
- def index
+ class MetalMiddleware < ActionController::Middleware
+ def call(env)
if env["PATH_INFO"] =~ /authed/
- self.response = app.call(env)
+ app.call(env)
else
- self.response_body = "Not authed!"
- self.status = 401
+ [401, headers, "Not authed!"]
end
end
end
@@ -21,7 +20,7 @@ module MetalTest
class TestMiddleware < ActiveSupport::TestCase
def setup
@app = Rack::Builder.new do
- use MetalMiddleware.middleware(:index)
+ use MetalMiddleware
run Endpoint.new
end.to_app
end