Skip to content

Commit

Permalink
escape_path (URI escape rather than CGI escape)
Browse files Browse the repository at this point in the history
  • Loading branch information
raggi committed May 3, 2011
1 parent 9134176 commit 3e9c822
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/rack/utils.rb
Expand Up @@ -18,12 +18,19 @@ module Rack
# applications adopted from all kinds of Ruby libraries.

module Utils
# URI escapes a string.
# URI escapes a string. (CGI style space to +)
def escape(s)
CGI.escape(s.to_s)
end
module_function :escape

# Like URI escaping, but with %20 instead of +. Strictly speaking this is
# true URI escaping.
def escape_path(s)
escape(s).gsub('+', '%20')
end
module_function :escape_path

# Unescapes a URI escaped string.
def unescape(s)
CGI.unescape(s)
Expand Down
4 changes: 4 additions & 0 deletions test/spec_utils.rb
Expand Up @@ -26,6 +26,10 @@
end
end

should "escape path spaces with %20" do
Rack::Utils.escape_path("foo bar").should.equal "foo%20bar"
end

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

0 comments on commit 3e9c822

Please sign in to comment.