Permalink
Browse files

GET routes accepts HEAD requests

  • Loading branch information...
1 parent ef69944 commit 530024aa03e78cd232f9aefed6fa5d3184588cf4 @spastorino spastorino committed Jul 23, 2012
Showing with 37 additions and 0 deletions.
  1. +18 −0 lib/journey/router.rb
  2. +19 −0 test/test_router.rb
View
@@ -128,6 +128,7 @@ def find_routes env
routes = filter_routes(req.path_info) + custom_routes.find_all { |r|
r.path.match(req.path_info)
}
+ routes.concat head_routes(routes)
routes.sort_by(&:precedence).find_all { |r|
r.constraints.all? { |k,v| v === req.send(k) } &&
@@ -141,5 +142,22 @@ def find_routes env
[match_data, r.defaults.merge(info), r]
}
end
+
+ def head_routes(routes)
+ precedence = (routes.map(&:precedence).max || 0) + 1
+ routes = routes.select { |r|
+ r.verb === "GET" && !(r.verb === "HEAD")
+ }.map! { |r|
+ Route.new(r.name,
+ r.app,
+ r.path,
+ r.conditions.merge(:request_method => "HEAD"),
+ r.defaults).tap do |route|
+ route.precedence = r.precedence + precedence
+ end
+ }
+ routes.flatten!
+ routes
+ end
end
end
View
@@ -478,6 +478,25 @@ def test_recognize_literal
assert called
end
+ def test_recognize_head_request_as_get_route
+ path = Path::Pattern.new "/books(/:action(.:format))"
+ app = Object.new
+ conditions = {
+ :request_method => 'GET'
+ }
+ @router.routes.add_route(app, path, conditions, {})
+
+ env = rails_env 'PATH_INFO' => '/books/list.rss',
+ "REQUEST_METHOD" => "HEAD"
+
+ called = false
+ @router.recognize(env) do |r, _, params|
+ called = true
+ end
+
+ assert called
+ end
+
def test_recognize_cares_about_verbs
path = Path::Pattern.new "/books(/:action(.:format))"
app = Object.new

0 comments on commit 530024a

Please sign in to comment.