Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Simpler 405, returns both on #recognize now

  • Loading branch information...
commit 28b8d200dc916cfe82c9ddc68f823c3380bb1e9c 1 parent f4a34f4
@joshbuddy authored
View
12 lib/http_router.rb
@@ -25,6 +25,8 @@ class HttpRouter
# Raised when there are left over options
LeftOverOptions = Class.new(RuntimeError)
+ RecognizeResponse = Struct.new(:matches, :acceptable_methods)
+
attr_reader :root, :routes, :named_routes, :nodes
attr_accessor :default_app, :url_mount, :route_class, :default_host, :default_port, :default_scheme
@@ -85,7 +87,8 @@ def add_route(route)
# Example:
# router = HttpRouter.new { extend_route { attr_accessor :controller } }
# router.add('/foo', :controller => :foo).to{|env| [200, {}, ['foo!']]}
- # router.recognize(Rack::MockRequest.env_for('/foo')).first.route.controller
+ # matches, other_methods = router.recognize(Rack::MockRequest.env_for('/foo'))
+ # matches.first.route.controller
# # ==> :foo
def extend_route(&blk)
@route_class = Class.new(Route) if @route_class == Route
@@ -116,11 +119,13 @@ def put(path, opts = {}, &app); add_with_request_method(path, :put, opts, &app);
# matching routes or nil if no match was found.
def recognize(env, &callback)
if callback
- call(env, &callback)
+ request = call(env, &callback)
+ [request.called?, request.acceptable_methods]
else
matches = []
callback ||= Proc.new {|match| matches << match}
- call(env, &callback) ? matches : nil
+ request = call(env, &callback)
+ [matches.empty? ? nil : matches, request.acceptable_methods]
end
end
@@ -273,6 +278,7 @@ def raw_call(env, &blk)
request = Request.new(rack_request.path_info, rack_request)
if blk
@root.call(request, &blk)
+ request
else
@root.call(request) or no_response(request, env)
end
View
2  lib/http_router/node/path.rb
@@ -25,7 +25,7 @@ def to_code
#{"if request.#{router.ignore_trailing_slash? ? 'path_finished?' : 'path.empty?'}" unless route.match_partially}
if callback
- called = true
+ request.called = true
callback.call(Response.new(request, #{path_ivar}))
else
env = request.rack_request.dup.env
View
2  lib/http_router/node/root.rb
@@ -33,7 +33,7 @@ def inspect_label
def compile(routes)
routes.each {|route| add_route(route)}
root.extend(root.methods_module)
- instance_eval "def call(request, &callback)\ncalled = false\n#{to_code}\ncallback ? called : nil\nend"
+ instance_eval "def call(request, &callback)\n#{to_code}\nnil\nend"
@compiled = true
end
View
6 lib/http_router/request.rb
@@ -1,8 +1,10 @@
class HttpRouter
class Request
- attr_accessor :path, :params, :rack_request, :extra_env, :continue, :passed_with
- alias_method :rack, :rack_request
+ attr_accessor :path, :params, :rack_request, :extra_env, :continue, :passed_with, :called
attr_reader :acceptable_methods
+ alias_method :rack, :rack_request
+ alias_method :called?, :called
+
def initialize(path, rack_request)
@rack_request = rack_request
@path = URI.unescape(path).split(/\//)
View
21 test/test_misc.rb
@@ -7,8 +7,9 @@ def test_cloning
r2.add('/test2', :name => :test).to(:test2)
assert_equal 2, r2.routes.size
- assert_equal nil, r1.recognize(Rack::Request.new(Rack::MockRequest.env_for('/test2')))
- assert r2.recognize(Rack::MockRequest.env_for('/test2'))
+ matches, other_methods = r1.recognize(Rack::Request.new(Rack::MockRequest.env_for('/test2')))
+ assert_equal nil, matches
+ assert r2.recognize(Rack::MockRequest.env_for('/test2')).first
assert_equal r1.routes.size, 1
assert_equal r2.routes.size, 2
@@ -22,10 +23,12 @@ def test_cloning
end
def test_reseting
- r = HttpRouter.new { add('/hi').to(:test) }
- assert !r.recognize(Rack::MockRequest.env_for('/hi')).empty?
- r.reset!
- assert_equal nil, r.recognize(Rack::MockRequest.env_for('/hi'))
+ router = HttpRouter.new
+ r = router.add('/hi').to(:test)
+ matches, other_methods = router.recognize(Rack::MockRequest.env_for('/hi'))
+ assert_equal r.route, matches.first.route
+ router.reset!
+ assert_equal nil, router.recognize(Rack::MockRequest.env_for('/hi')).first
end
def test_redirect_trailing_slash
@@ -43,11 +46,11 @@ def test_multi_recognize
add('/:var1/there')
}
response = router.recognize(Rack::MockRequest.env_for('/hi/there'))
- assert_equal [r1, r2, r3, r4], response.map{|resp| resp.path.route}
+ assert_equal [r1, r2, r3, r4], response.first.map{|resp| resp.path.route}
response = router.recognize(Rack::MockRequest.env_for('/hi/var'))
- assert_equal [r2, r3], response.map{|resp| resp.path.route}
+ assert_equal [r2, r3], response.first.map{|resp| resp.path.route}
response = router.recognize(Rack::MockRequest.env_for('/you/there'))
- assert_equal [r2, r4], response.map{|resp| resp.path.route}
+ assert_equal [r2, r4], response.first.map{|resp| resp.path.route}
end
def test_multi_name_gen
Please sign in to comment.
Something went wrong with that request. Please try again.