From 9d3833041d1ca61eb5b6cc10d505bc001f31145f Mon Sep 17 00:00:00 2001 From: MothOnMars <437455+MothOnMars@users.noreply.github.com> Date: Tue, 9 Jul 2019 17:18:31 -0700 Subject: [PATCH 1/3] de-dupe parameters when normalizing query --- lib/addressable/uri.rb | 2 +- spec/addressable/uri_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/addressable/uri.rb b/lib/addressable/uri.rb index 71a806bf..7378cf7f 100644 --- a/lib/addressable/uri.rb +++ b/lib/addressable/uri.rb @@ -1612,7 +1612,7 @@ def normalized_query(*flags) modified_query_class = Addressable::URI::CharacterClasses::QUERY.dup # Make sure possible key-value pair delimiters are escaped. modified_query_class.sub!("\\&", "").sub!("\\;", "") - pairs = (self.query || "").split("&", -1) + pairs = (query || "").split("&", -1).uniq pairs.delete_if(&:empty?) if flags.include?(:compacted) pairs.sort! if flags.include?(:sorted) component = pairs.map do |pair| diff --git a/spec/addressable/uri_spec.rb b/spec/addressable/uri_spec.rb index 2a938a84..9df16404 100644 --- a/spec/addressable/uri_spec.rb +++ b/spec/addressable/uri_spec.rb @@ -4323,6 +4323,28 @@ 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 normalized query of 'a=1'" do + expect(@uri.normalized_query).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 normalized query of 'a=1'" do + expect(@uri.normalized_query).to eq("a=1&a=2") + end +end + describe Addressable::URI, "when parsed from " + "'http://example.com/sound%2bvision'" do before do From aa0ed7f6a93b86cce2e7603c6f7ee4d392cb5662 Mon Sep 17 00:00:00 2001 From: MothOnMars <437455+MothOnMars@users.noreply.github.com> Date: Fri, 30 Aug 2019 23:44:47 -0700 Subject: [PATCH 2/3] fix spec description --- spec/addressable/uri_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/addressable/uri_spec.rb b/spec/addressable/uri_spec.rb index 9df16404..ab471264 100644 --- a/spec/addressable/uri_spec.rb +++ b/spec/addressable/uri_spec.rb @@ -4340,7 +4340,7 @@ def to_s @uri = Addressable::URI.parse("http://example.com/?a=1&a=2") end - it "should have a normalized query of 'a=1'" do + it "should have a normalized query of 'a=1&a=2'" do expect(@uri.normalized_query).to eq("a=1&a=2") end end From 314df8fe01e6f9a41910eb58368e710dc878e3f8 Mon Sep 17 00:00:00 2001 From: MothOnMars <437455+MothOnMars@users.noreply.github.com> Date: Mon, 2 Sep 2019 18:10:30 -0700 Subject: [PATCH 3/3] only de-dupe params when compacting --- lib/addressable/uri.rb | 4 ++-- spec/addressable/uri_spec.rb | 14 ++++++-------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/addressable/uri.rb b/lib/addressable/uri.rb index 7378cf7f..283d6137 100644 --- a/lib/addressable/uri.rb +++ b/lib/addressable/uri.rb @@ -1612,8 +1612,8 @@ def normalized_query(*flags) 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).uniq - pairs.delete_if(&:empty?) if flags.include?(:compacted) + 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(pair, modified_query_class, "+") diff --git a/spec/addressable/uri_spec.rb b/spec/addressable/uri_spec.rb index ab471264..7f162c11 100644 --- a/spec/addressable/uri_spec.rb +++ b/spec/addressable/uri_spec.rb @@ -4323,25 +4323,23 @@ def to_s end end -describe Addressable::URI, "when parsed from " + - "'http://example.com/?a=1&a=1'" do +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 normalized query of 'a=1'" do - expect(@uri.normalized_query).to eq("a=1") + 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 +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 normalized query of 'a=1&a=2'" do - expect(@uri.normalized_query).to eq("a=1&a=2") + 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