Skip to content

Commit

Permalink
GET routes accepts HEAD requests
Browse files Browse the repository at this point in the history
  • Loading branch information
spastorino committed Jul 23, 2012
1 parent ef69944 commit 530024a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/journey/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) } &&
Expand All @@ -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
19 changes: 19 additions & 0 deletions test/test_router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 530024a

Please sign in to comment.