summary refs log tree commit
diff options
context:
space:
mode:
authorJames Tucker <jftucker@gmail.com>2014-07-06 13:26:12 -0700
committerJames Tucker <jftucker@gmail.com>2014-07-06 13:26:31 -0700
commit4da6ef9f5b6a98c289ab84fe789bf32137001b9d (patch)
treef66c4cd688573e1aa7fd1ae0d602e8ff6efa17fb
parent1b2fb3c805e7983d5f979823f93a5ebb190643d3 (diff)
downloadrack-4da6ef9f5b6a98c289ab84fe789bf32137001b9d.tar.gz
Rack::Utils#best_q_match returns nil with no match
-rw-r--r--lib/rack/utils.rb5
-rw-r--r--test/spec_utils.rb3
2 files changed, 6 insertions, 2 deletions
diff --git a/lib/rack/utils.rb b/lib/rack/utils.rb
index a8cf7cc1..1be818b4 100644
--- a/lib/rack/utils.rb
+++ b/lib/rack/utils.rb
@@ -184,13 +184,14 @@ module Rack
     def best_q_match(q_value_header, available_mimes)
       values = q_values(q_value_header)
 
-      values.map do |req_mime, quality|
+      matches = values.map do |req_mime, quality|
         match = available_mimes.find { |am| Rack::Mime.match?(am, req_mime) }
         next unless match
         [match, quality]
       end.compact.sort_by do |match, quality|
         (match.split('/', 2).count('*') * -10) + quality
-      end.last.first
+      end.last
+      matches && matches.first
     end
     module_function :best_q_match
 
diff --git a/test/spec_utils.rb b/test/spec_utils.rb
index b79ae1df..38ffc861 100644
--- a/test/spec_utils.rb
+++ b/test/spec_utils.rb
@@ -306,6 +306,9 @@ describe Rack::Utils do
     # 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"
+
+    # When there are no matches, return nil:
+    Rack::Utils.best_q_match("application/json", %w[text/html text/plain]).should.equal nil
   end
 
   should "escape html entities [&><'\"/]" do