Permalink
Browse files

make #provides respect Content-Type. fixes #408.

  • Loading branch information...
rkh committed Jan 2, 2012
1 parent d9c0828 commit e5d511fbe0ace00df64493be313b31871447a6fb
Showing with 42 additions and 1 deletion.
  1. +3 −0 CHANGES
  2. +3 −1 lib/sinatra/base.rb
  3. +11 −0 test/filter_test.rb
  4. +25 −0 test/routing_test.rb
View
@@ -5,6 +5,9 @@
* Add :status option support to send_file. (Konstantin Haase)
+ * The `provides` condition now respects an earlier set content type.
+ (Konstantin Haase)
+
* Exception#code is only used when :use_code is enabled and displays a warning.
Moreover, it will be ignored if the value is not between 400 and 599. You
should use Exception#http_status instead. (Konstantin Haase)
View
@@ -1163,7 +1163,9 @@ def provides(*types)
types.map! { |t| mime_types(t) }
types.flatten!
condition do
- if type = request.preferred_type(types)
+ if type = response['Content-Type']
+ types.include? type or types.include? type[/^[^;]+/]
+ elsif type = request.preferred_type(types)
content_type(type)
true
else
View
@@ -425,4 +425,15 @@ class AfterFilterTest < Test::Unit::TestCase
get '/foo', {}, { 'HTTP_USER_AGENT' => 'foo' }
assert ran
end
+
+ it 'only triggeres provides condition if conforms with current Content-Type' do
+ mock_app do
+ before(:provides => :txt) { @type = 'txt' }
+ before(:provides => :html) { @type = 'html' }
+ get('/') { @type }
+ end
+
+ get '/', {}, { 'HTTP_ACCEPT' => '*' }
+ assert_body 'txt'
+ end
end
View
@@ -718,6 +718,31 @@ class RoutingTest < Test::Unit::TestCase
assert !ok?
end
+ it "filters by current Content-Type" do
+ mock_app do
+ before('/txt') { content_type :txt }
+ get('*', :provides => :txt) { 'txt' }
+
+ before('/html') { content_type :html }
+ get('*', :provides => :html) { 'html' }
+ end
+
+ get '/', {}, { 'HTTP_ACCEPT' => '*' }
+ assert ok?
+ assert_equal 'text/plain;charset=utf-8', response.headers['Content-Type']
+ assert_body 'txt'
+
+ get '/txt', {}, { 'HTTP_ACCEPT' => 'text/plain' }
+ assert ok?
+ assert_equal 'text/plain;charset=utf-8', response.headers['Content-Type']
+ assert_body 'txt'
+
+ get '/', {}, { 'HTTP_ACCEPT' => 'text/html' }
+ assert ok?
+ assert_equal 'text/html;charset=utf-8', response.headers['Content-Type']
+ assert_body 'html'
+ end
+
it "allows multiple mime types for accept header" do
types = ['image/jpeg', 'image/pjpeg']

0 comments on commit e5d511f

Please sign in to comment.