Skip to content

Commit

Permalink
Dependencies now work correctly. Added specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesG committed Nov 27, 2011
1 parent b5d9ce6 commit 7ff7a20
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 27 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
source "http://rubygems.org"
gemspec

gem 'escape_utils', :group=>:development, :platforms => [:ruby_19]
gem 'escape_utils', :group=>:development, :platforms => [:mri_19]
gem 'simplecov', :group=>:development, :platforms => [:mri_19]
33 changes: 7 additions & 26 deletions lib/uri_template/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,8 @@ module StringEncoding
# @param String
# @return String
# @visibility public
def to_ascii_force_encoding(str)
if str.frozen?
return str.encode(Encoding::ASCII)
end
str.force_encoding(Encoding::ASCII)
def to_ascii_encode(str)
str.encode(Encoding::ASCII)
end

# @method to_utf8(string)
Expand All @@ -93,17 +90,6 @@ def to_ascii_force_encoding(str)
# @param String
# @return String
# @visibility public
def to_utf8_force_encoding(str)
if str.frozen?
return str.encode(Encoding::UTF_8)
end
str.force_encoding(Encoding::UTF_8)
end

def to_ascii_encode(str)
str.encode(Encoding::ASCII)
end

def to_utf8_encode(str)
str.encode(Encoding::UTF_8)
end
Expand All @@ -116,12 +102,7 @@ def to_utf8_fallback(str)
str
end

if "".respond_to?(:force_encoding)
# @private
alias_method :to_ascii, :to_ascii_force_encoding
# @private
alias_method :to_utf8, :to_utf8_force_encoding
elsif "".respond_to?(:encode)
if "".respond_to?(:encode)
# @private
alias_method :to_ascii, :to_ascii_encode
# @private
Expand Down Expand Up @@ -156,15 +137,15 @@ module Pure
PCT = /%(\h\h)/.freeze

def escape_url(s)
to_ascii( s.to_s.gsub(URL_ESCAPED){
to_utf8(s.to_s).gsub(URL_ESCAPED){
'%'+$1.unpack('H2'*$1.bytesize).join('%').upcase
} )
}
end

def escape_uri(s)
to_ascii( s.to_s.gsub(URI_ESCAPED){
to_utf8(s.to_s).gsub(URI_ESCAPED){
'%'+$1.unpack('H2'*$1.bytesize).join('%').upcase
} )
}
end

def unescape_url(s)
Expand Down
48 changes: 48 additions & 0 deletions spec/uri_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,54 @@ class << obj

end

describe "escape utils", :if => URITemplate::Utils.using_escape_utils? do

if URITemplate::Utils.using_escape_utils?

pure = Object.new
pure.extend(URITemplate::Utils::Escaping::Pure)
escape_utils = Object.new
escape_utils.extend(URITemplate::Utils::Escaping::EscapeUtils)

[
"",
"a",
" ",
"%%%",
"a".encode('ISO-8859-1'),
"öüä".encode('ISO-8859-1'),
"+",
"öäü"
].each do |str|
it "should correctly escape #{str.inspect} ( encoding: #{str.encoding} )" do
escape_utils.escape_uri(str).should == pure.escape_uri(str)
escape_utils.escape_url(str).should == pure.escape_url(str)
end
end

[
"",
"a",
" ",
"a".encode('ISO-8859-1'),
"+",
"%20%30%40",
# errors:
"%",
"%%%",
"%gh",
"%a"
].each do |str|
it "should correctly unescape #{str.inspect} ( encoding: #{str.encoding} )" do
escape_utils.unescape_uri(str).should == pure.unescape_uri(str)
escape_utils.unescape_url(str).should == pure.unescape_url(str)
end
end

end

end

end

end

0 comments on commit 7ff7a20

Please sign in to comment.