Permalink
Browse files

Further paranoia regarding frozen strings.

  • Loading branch information...
1 parent b5f127e commit 64f82f2f4ab7ce30dfed52e754ba14a9220d8bcd @sporkmonger committed Mar 31, 2013
Showing with 20 additions and 3 deletions.
  1. +4 −3 lib/addressable/uri.rb
  2. +16 −0 spec/addressable/uri_spec.rb
View
@@ -363,11 +363,12 @@ def self.encode_component(component, character_class=
component = component.dup
component.force_encoding(Encoding::ASCII_8BIT)
end
- component.gsub!(character_class) do |sequence|
+ # Avoiding gsub! because there are edge cases with frozen strings
+ component = component.gsub(character_class) do |sequence|
(sequence.unpack('C*').map { |c| "%" + ("%02x" % c).upcase }).join
end
if upcase_encoded.length > 0
- component.gsub!(/%(#{upcase_encoded.chars.map do |char|
+ component = component.gsub(/%(#{upcase_encoded.chars.map do |char|
char.unpack('C*').map { |c| '%02x' % c }.join
end.join('|')})/i) { |s| s.upcase }
end
@@ -1907,7 +1908,7 @@ def route_from(uri)
end
else
if uri.path != SLASH
- components[:path].gsub!(
+ components[:path] = components[:path].gsub(
Regexp.new("^" + Regexp.escape(uri.path)), EMPTY_STR)
end
end
@@ -690,6 +690,14 @@ def to_s
@uri.to_s.should == ''
end
+ it "should be frozen" do
+ @uri.should be_frozen
+ end
+
+ it "should not be frozen after duping" do
+ @uri.dup.should_not be_frozen
+ end
+
it "should not allow destructive operations" do
expect { @uri.normalize! }.to raise_error { |error|
error.message.should match(/can't modify frozen/)
@@ -814,6 +822,14 @@ def to_s
@uri.normalize.to_s.should == 'http://example.com/path?a=1#123'
end
+ it "should be frozen" do
+ @uri.should be_frozen
+ end
+
+ it "should not be frozen after duping" do
+ @uri.dup.should_not be_frozen
+ end
+
it "should not allow destructive operations" do
expect { @uri.normalize! }.to raise_error { |error|
error.message.should match(/can't modify frozen/)

0 comments on commit 64f82f2

Please sign in to comment.