Skip to content

Commit

Permalink
Rack::Utils.unescape should take a target encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Oct 4, 2011
1 parent 8c638c2 commit bd3dcda
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/rack/utils.rb
Expand Up @@ -32,9 +32,16 @@ def escape_path(s)
end
module_function :escape_path

# Unescapes a URI escaped string.
def unescape(s)
URI.decode_www_form_component(s)
# Unescapes a URI escaped string with +encoding+. +encoding+ will be the
# target encoding of the string returned, and it defaults to UTF-8
if defined?(::Encoding)
def unescape(s, encoding = Encoding::UTF_8)
URI.decode_www_form_component(s, encoding)
end
else
def unescape(s, encoding = nil)
URI.decode_www_form_component(s, encoding)
end
end
module_function :unescape

Expand Down
10 changes: 10 additions & 0 deletions test/spec_utils.rb
Expand Up @@ -11,6 +11,16 @@ def kcodeu
$KCODE = default_kcode if one8
end

should "round trip binary data" do
r = [218, 0].pack 'CC'
if defined?(::Encoding)
z = Rack::Utils.unescape(Rack::Utils.escape(r), Encoding::BINARY)
else
z = Rack::Utils.unescape(Rack::Utils.escape(r))
end
r.should.equal z
end

should "escape correctly" do
Rack::Utils.escape("fo<o>bar").should.equal "fo%3Co%3Ebar"
Rack::Utils.escape("a space").should.equal "a+space"
Expand Down

0 comments on commit bd3dcda

Please sign in to comment.