backporting #4918 to 3.2 stable; adding extra test for accept header given by googlebot

This commit is contained in:
Aditya Sanghi
2012-02-17 15:10:52 +05:30
parent a1b9acb72e
commit dffd85a192
2 changed files with 15 additions and 1 deletions

View File

@@ -82,6 +82,7 @@ module Mime
class << self
TRAILING_STAR_REGEXP = /(text|application)\/\*/
Q_SEPARATOR_REGEXP = /;\s*q=/
def lookup(string)
LOOKUP[string]
@@ -108,6 +109,7 @@ module Mime
def parse(accept_header)
if accept_header !~ /,/
accept_header = accept_header.split(Q_SEPARATOR_REGEXP).first
if accept_header =~ TRAILING_STAR_REGEXP
parse_data_with_trailing_star($1)
else
@@ -117,7 +119,7 @@ module Mime
# keep track of creation order to keep the subsequent sort stable
list, index = [], 0
accept_header.split(/,/).each do |header|
params, q = header.split(/;\s*q=/)
params, q = header.split(Q_SEPARATOR_REGEXP)
if params.present?
params.strip!

View File

@@ -69,6 +69,18 @@ class MimeTypeTest < ActiveSupport::TestCase
assert_equal expect, Mime::Type.parse(accept)
end
test "parse single media range with q" do
accept = "text/html;q=0.9"
expect = [Mime::HTML]
assert_equal expect, Mime::Type.parse(accept)
end
test "parse any media range with q" do
accept = "*/*;q=0.9"
expect = [Mime::ALL]
assert_equal expect, Mime::Type.parse(accept)
end
# Accept header send with user HTTP_USER_AGENT: Sunrise/0.42j (Windows XP)
test "parse broken acceptlines" do
accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/*,,*/*;q=0.5"