Skip to content

Commit

Permalink
Respect content type set in superclass before filter
Browse files Browse the repository at this point in the history
  • Loading branch information
jkowens committed Oct 3, 2020
1 parent 788b806 commit db2f6ec
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
5 changes: 2 additions & 3 deletions lib/sinatra/base.rb
Expand Up @@ -998,6 +998,7 @@ def filter!(type, base = settings)
filter! type, base.superclass if base.superclass.respond_to?(:filters)
base.filters[type].each do |args|
result = process_route(*args)
@pinned_response = !@response['Content-Type'].nil?
yield result if block_given?
end
end
Expand Down Expand Up @@ -1123,9 +1124,7 @@ def dispatch!

invoke do
static! if settings.static? && (request.get? || request.head?)
filter! :before do
@pinned_response = !@response['Content-Type'].nil?
end
filter! :before
route!
end
rescue ::Exception => boom
Expand Down
11 changes: 11 additions & 0 deletions test/filter_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit db2f6ec

Please sign in to comment.