diff --git a/lib/protocol/http/header/priority.rb b/lib/protocol/http/header/priority.rb index 6b21723..b4522a5 100644 --- a/lib/protocol/http/header/priority.rb +++ b/lib/protocol/http/header/priority.rb @@ -29,13 +29,15 @@ def << value # The default urgency level if not specified. DEFAULT_URGENCY = 3 - # Returns the urgency level if specified. 0 is the highest priority, and 7 is the lowest. + # The urgency level, if specified using `u=`. 0 is the highest priority, and 7 is the lowest. + # + # Note that when duplicate Dictionary keys are encountered, all but the last instance are ignored. # # @returns [Integer | Nil] the urgency level if specified, or `nil` if not present. def urgency(default = DEFAULT_URGENCY) - if value = self.find { |value| value.start_with?("u=") } + if value = self.reverse_find{|value| value.start_with?("u=")} _, level = value.split("=", 2) - return level.to_i + return Integer(level) end return default diff --git a/lib/protocol/http/header/split.rb b/lib/protocol/http/header/split.rb index 7ecfa6d..5742e39 100644 --- a/lib/protocol/http/header/split.rb +++ b/lib/protocol/http/header/split.rb @@ -39,6 +39,16 @@ def << value def to_s join(",") end + + protected + + def reverse_find(&block) + reverse_each do |value| + return value if block.call(value) + end + + return nil + end end end end diff --git a/test/protocol/http/header/priority.rb b/test/protocol/http/header/priority.rb index a236b4a..e7527fd 100644 --- a/test/protocol/http/header/priority.rb +++ b/test/protocol/http/header/priority.rb @@ -55,10 +55,9 @@ end with "u=2, u=5" do - it "prioritizes the first urgency directive" do + it "prioritizes the last urgency directive" do expect(header).to have_attributes( - # First occurrence takes precedence - urgency: be == 2, + urgency: be == 5, ) end end