Merge pull request #896 from jeremyevans/not-found-404

Unify not_found and error 404 behavior (Fixes #814)
This commit is contained in:
Zachary Scott 2016-01-31 20:01:58 +09:00
commit 78dc9d8c87
2 changed files with 16 additions and 1 deletions

View File

@ -1255,13 +1255,13 @@ module Sinatra
args = compile! "ERROR", //, block
codes = codes.map { |c| Array(c) }.flatten
codes << Exception if codes.empty?
codes << Sinatra::NotFound if codes.include?(404)
codes.each { |c| (@errors[c] ||= []) << args }
end
# Sugar for `error(404) { ... }`
def not_found(&block)
error(404, &block)
error(Sinatra::NotFound, &block)
end
# Define a named template. The block must return the template source.

View File

@ -171,6 +171,21 @@ class RoutingTest < Minitest::Test
assert_equal 404, status
end
it "uses 404 error handler for not matching route" do
mock_app {
not_found do
"nf"
end
error 404 do
"e"
end
}
get "/"
assert_equal "e", body
assert_equal 404, status
end
it 'matches empty PATH_INFO to "/" if no route is defined for ""' do
mock_app do
get '/' do