Permalink
Browse files

Rack::Utils.unescape should take a target encoding

  • Loading branch information...
1 parent 8c638c2 commit bd3dcda282bb3bc29c5a416051ad126fbd2d865e @tenderlove tenderlove committed Oct 4, 2011
Showing with 20 additions and 3 deletions.
  1. +10 −3 lib/rack/utils.rb
  2. +10 −0 test/spec_utils.rb
View
13 lib/rack/utils.rb
@@ -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
View
10 test/spec_utils.rb
@@ -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"

0 comments on commit bd3dcda

Please sign in to comment.