Skip to content

Commit

Permalink
Handle invalid quality values. Close fnando#315.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando committed Oct 30, 2017
1 parent 244c4af commit 4b7d9ca
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## UNRELEASED

- Add Google Site Verification to the bot list.
- Handle invalid quality values that look like numbers.

## v2.5.2

Expand Down
10 changes: 8 additions & 2 deletions lib/browser/accept_language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,20 @@ def region
end

def quality
@quality ||= Float(quality_value || 1.0)
@quality ||= begin
Float(quality_value || 1.0)
rescue ArgumentError
0.1
end
end

private

def quality_value
qvalue = part[/;q=([\d.]+)/, 1]
qvalue =~ /\A0\.0?\z/ ? "0.0" : qvalue
qvalue = qvalue =~ /\A0\.0?\z/ ? "0.0" : qvalue
qvalue = qvalue.gsub(/\.+/, ".") if qvalue
qvalue
end
end
end
8 changes: 8 additions & 0 deletions test/unit/accept_language_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,12 @@ def assert_language(item, expect = {})
assert_language result[1], code: "ru", region: nil, quality: 0.9
assert_language result[2], code: "de", region: nil, quality: 0.8
end

test "handles invalid quality values that look like a number" do
accept_language = "fr-CH, fr;q=0.9, en;q=0.8, de;q=0..7, *;q=0.5"
result = Browser::AcceptLanguage.parse(accept_language)

assert_equal 5, result.size
assert_language result[3], code: "de", region: nil, quality: 0.7
end
end

0 comments on commit 4b7d9ca

Please sign in to comment.