2009-09-19 13:10:41 -04:00
|
|
|
require 'abstract_unit'
|
2009-08-25 15:14:31 -04:00
|
|
|
|
|
|
|
module MetalTest
|
2009-08-26 03:18:52 -04:00
|
|
|
class MetalMiddleware < ActionController::Middleware
|
|
|
|
def call(env)
|
2009-08-25 15:14:31 -04:00
|
|
|
if env["PATH_INFO"] =~ /authed/
|
2009-08-26 03:18:52 -04:00
|
|
|
app.call(env)
|
2009-08-25 15:14:31 -04:00
|
|
|
else
|
2009-08-26 03:18:52 -04:00
|
|
|
[401, headers, "Not authed!"]
|
2009-08-25 15:14:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Endpoint
|
|
|
|
def call(env)
|
|
|
|
[200, {}, "Hello World"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestMiddleware < ActiveSupport::TestCase
|
2010-03-19 21:04:00 -04:00
|
|
|
include RackTestUtils
|
|
|
|
|
2009-08-25 15:14:31 -04:00
|
|
|
def setup
|
|
|
|
@app = Rack::Builder.new do
|
2009-10-15 19:33:47 -04:00
|
|
|
use MetalTest::MetalMiddleware
|
|
|
|
run MetalTest::Endpoint.new
|
2009-08-25 15:14:31 -04:00
|
|
|
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)
|
|
|
|
|
2010-03-19 21:04:00 -04:00
|
|
|
assert_equal "Hello World", body_to_string(response[2])
|
2009-08-25 15:14:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "it can return a response using the normal AC::Metal techniques" do
|
|
|
|
env = Rack::MockRequest.env_for("/")
|
|
|
|
response = @app.call(env)
|
|
|
|
|
2010-03-19 21:04:00 -04:00
|
|
|
assert_equal "Not authed!", body_to_string(response[2])
|
2009-08-25 15:14:31 -04:00
|
|
|
assert_equal 401, response[0]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|