Skip to content


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

merged 1 commit into from

2 participants


I've tried to use this method like this:

Rack::Utils.best_q_match "application/json", Rack::Mime::MIME_TYPES.values

It was returning "application/vnd.lotus-1-2-3" instead of "application/json".


puts Rack::Mime::MIME_TYPES.values.first # => "application/vnd.lotus-1-2-3"
@raggi raggi merged commit 186e256 into rack:master

1 check passed

Details default The Travis CI build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 4 additions and 1 deletion.
  1. +1 −1 lib/rack/utils.rb
  2. +3 −0 test/spec_utils.rb
2 lib/rack/utils.rb
@@ -185,7 +185,7 @@ def best_q_match(q_value_header, available_mimes)
values = q_values(q_value_header) 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|
3 test/spec_utils.rb
@@ -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"
Something went wrong with that request. Please try again.