Permalink
Browse files

Ensure Rack::Utils.best_q_match to respect requested content type

  • Loading branch information...
1 parent 8dc31fe commit e06a25423979e0443e6252cbe0cb4b2b41fa89f0 @jodosha committed Feb 13, 2014
Showing with 4 additions and 1 deletion.
  1. +1 −1 lib/rack/utils.rb
  2. +3 −0 test/spec_utils.rb
View
@@ -185,7 +185,7 @@ def best_q_match(q_value_header, available_mimes)
values = q_values(q_value_header)
values.map do |req_mime, quality|
- match = available_mimes.first { |am| Rack::Mime.match?(am, req_mime) }
+ match = available_mimes.find { |am| Rack::Mime.match?(am, req_mime) }
next unless match
[match, quality]
end.compact.sort_by do |match, quality|
View
@@ -300,6 +300,9 @@ def kcodeu
# Higher quality matches are preferred
Rack::Utils.best_q_match("text/*;q=0.5,text/plain;q=1.0", %w[text/plain text/html]).should.equal "text/plain"
+ # Respect requested content type
+ Rack::Utils.best_q_match("application/json", %w[application/vnd.lotus-1-2-3 application/json]).should.equal "application/json"
+
# All else equal, the available mimes are preferred in order
Rack::Utils.best_q_match("text/*", %w[text/html text/plain]).should.equal "text/html"
Rack::Utils.best_q_match("text/plain,text/html", %w[text/html text/plain]).should.equal "text/html"

0 comments on commit e06a254

Please sign in to comment.