Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,7 @@ def controller_path | |
# and response object available. You might wish to control the | ||
# environment and response manually for performance reasons. | ||
|
||
attr_internal :status, :headers, :content_type | ||
attr_internal :status, :headers, :content_type, :app, :response | ||
|
||
def initialize(*) | ||
@_headers = {} | ||
|
@@ -75,7 +75,34 @@ def call(name, env) | |
|
||
# :api: private | ||
def to_a | ||
[status, headers, response_body] | ||
response ? response.to_a : [status, headers, response_body] | ||
end | ||
|
||
class ActionEndpoint | ||
def initialize(controller, action) | ||
@controller, @action = controller, action | ||
end | ||
|
||
def call(env) | ||
controller = @controller.new.call(@action, env) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
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 | ||
|
@@ -89,9 +116,12 @@ def to_a | |
# Proc:: A rack application | ||
def self.action(name) | ||
@actions ||= {} | ||
@actions[name.to_s] ||= proc do |env| | ||
new.call(name, env) | ||
end | ||
@actions[name.to_s] ||= ActionEndpoint.new(self, name) | ||
end | ||
|
||
def self.middleware(name) | ||
@middlewares ||= {} | ||
@middlewares[name.to_s] ||= ActionMiddleware.new(self, name) | ||
This comment has been minimized.
Sorry, something went wrong.
jonathan
|
||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") | ||
|
||
module MetalTest | ||
class MetalMiddleware < ActionController::Metal | ||
def index | ||
if env["PATH_INFO"] =~ /authed/ | ||
self.response = app.call(env) | ||
else | ||
self.response_body = "Not authed!" | ||
self.status = 401 | ||
end | ||
end | ||
end | ||
|
||
class Endpoint | ||
def call(env) | ||
[200, {}, "Hello World"] | ||
end | ||
end | ||
|
||
class TestMiddleware < ActiveSupport::TestCase | ||
def setup | ||
@app = Rack::Builder.new do | ||
use MetalMiddleware.middleware(:index) | ||
run Endpoint.new | ||
end.to_app | ||
end | ||
|
||
test "it can call the next app by using @app" do | ||
env = Rack::MockRequest.env_for("/authed") | ||
response = @app.call(env) | ||
|
||
assert_equal "Hello World", response[2] | ||
end | ||
|
||
test "it can return a response using the normal AC::Metal techniques" do | ||
env = Rack::MockRequest.env_for("/") | ||
response = @app.call(env) | ||
|
||
assert_equal "Not authed!", response[2] | ||
assert_equal 401, response[0] | ||
end | ||
end | ||
end | ||
|
3 comments
on commit c7ba911
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
jonathan: +1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
jonathan, you are correct about the "controller = ". For the ActionMiddleware#new comment, #new is an instance method so ActionMiddleware.new does not call that method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, ok. Cool.
Maybe I'm missing something but, I don't think you need the assignment here.