Skip to content

Commit

Permalink
Merge pull request #350 from MothOnMars/dedupe_params
Browse files Browse the repository at this point in the history
de-dupe parameters when normalizing query
  • Loading branch information
sporkmonger authored Jul 3, 2021
2 parents 6af2520 + 3d1f153 commit 3638c64
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/addressable/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1630,8 +1630,11 @@ def normalized_query(*flags)
return nil unless self.query
return @normalized_query if defined?(@normalized_query)
@normalized_query ||= begin
pairs = (self.query || "").split("&", -1)
pairs.delete_if(&:empty?) if flags.include?(:compacted)
modified_query_class = Addressable::URI::CharacterClasses::QUERY.dup
# Make sure possible key-value pair delimiters are escaped.
modified_query_class.sub!("\\&", "").sub!("\\;", "")
pairs = (query || "").split("&", -1)
pairs.delete_if(&:empty?).uniq! if flags.include?(:compacted)
pairs.sort! if flags.include?(:sorted)
component = pairs.map do |pair|
Addressable::URI.normalize_component(
Expand Down
20 changes: 20 additions & 0 deletions spec/addressable/uri_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4353,6 +4353,26 @@ def to_s
end
end

describe Addressable::URI, "when parsed from 'http://example.com/?a=1&a=1'" do
before do
@uri = Addressable::URI.parse("http://example.com/?a=1&a=1")
end

it "should have a compacted normalized query of 'a=1'" do
expect(@uri.normalized_query(:compacted)).to eq("a=1")
end
end

describe Addressable::URI, "when parsed from 'http://example.com/?a=1&a=2'" do
before do
@uri = Addressable::URI.parse("http://example.com/?a=1&a=2")
end

it "should have a compacted normalized query of 'a=1&a=2'" do
expect(@uri.normalized_query(:compacted)).to eq("a=1&a=2")
end
end

describe Addressable::URI, "when parsed from " +
"'http://example.com/sound%2bvision'" do
before do
Expand Down

0 comments on commit 3638c64

Please # to comment.