Merge pull request #1649 from jkowens/fix-1647

Respect content type set in superclass before filter
This commit is contained in:
Jordan Owens 2020-10-18 17:23:54 -04:00 committed by GitHub
commit 177392f44b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 5 deletions

View File

@ -934,6 +934,7 @@ module Sinatra
@params = IndifferentHash.new
@request = Request.new(env)
@response = Response.new
@pinned_response = nil
template_cache.clear if settings.reload_templates
invoke { dispatch! }
@ -994,11 +995,11 @@ module Sinatra
# Run filters defined on the class and all superclasses.
# Accepts an optional block to call after each filter is applied.
def filter!(type, base = settings)
filter! type, base.superclass if base.superclass.respond_to?(:filters)
def filter!(type, base = settings, &block)
filter!(type, base.superclass, &block) if base.superclass.respond_to?(:filters)
base.filters[type].each do |args|
result = process_route(*args)
yield result if block_given?
block.call(result) if block_given?
end
end
@ -1006,7 +1007,7 @@ module Sinatra
def route!(base = settings, pass_block = nil)
if routes = base.routes[@request.request_method]
routes.each do |pattern, conditions, block|
@response.delete_header('Content-Type') unless @pinned_response
response.delete_header('Content-Type') unless @pinned_response
returned_pass_block = process_route(pattern, conditions) do |*args|
env['sinatra.route'] = "#{@request.request_method} #{pattern}"
@ -1124,7 +1125,7 @@ module Sinatra
invoke do
static! if settings.static? && (request.get? || request.head?)
filter! :before do
@pinned_response = !@response['Content-Type'].nil?
@pinned_response = !response['Content-Type'].nil?
end
route!
end

View File

@ -262,6 +262,17 @@ class AfterFilterTest < Minitest::Test
assert_equal 8, count
end
it "respects content type set in superclass filter" do
base = Class.new(Sinatra::Base)
base.before { content_type :json }
mock_app(base) do
get('/foo'){ {foo: :bar}.to_json }
end
get '/foo'
assert_equal 'application/json', response.headers['Content-Type']
end
it 'does not run after filter when serving static files' do
ran_filter = false
mock_app do