Browse files

Escape special characters for Windows filesystems. Issue #163

  • Loading branch information...
1 parent 7c57671 commit ed0168f3bbe9b5f4a05eb2301ca6ea9fc60b6c5b @drbrain drbrain committed Nov 8, 2011
Showing with 38 additions and 2 deletions.
  1. +2 −0 lib/mechanize/parser.rb
  2. +36 −2 test/test_mechanize_parser.rb
View
2 lib/mechanize/parser.rb
@@ -123,6 +123,8 @@ def extract_filename full_path = @full_path
filename = "_#{filename}"
end
+ filename = filename.tr '<>:"\/\\|?*', '_'
+
@filename = if full_path then
File.join @uri.host, path, filename
else
View
38 test/test_mechanize_parser.rb
@@ -162,15 +162,49 @@ def test_extract_filename_host
assert_equal 'example/index.html', @parser.extract_filename(true)
end
+ def test_extract_filename_special_character
+ @parser.response = {}
+
+ invisible = "\t\n\v\f\r"
+
+ invisible.chars.each do |char|
+ @parser.uri = URI "http://example/#{char}"
+
+ assert_equal 'index.html', @parser.extract_filename, char.inspect
+ end
+
+ escaped = "<>\"\\|"
+
+ escaped.chars.each do |char|
+ escaped_char = CGI.escape char
+
+ @parser.uri = URI "http://example/#{escaped_char}"
+
+ assert_equal "#{escaped_char}.html", @parser.extract_filename, char
+ end
+
+ @parser.uri = URI "http://example/?"
+
+ assert_equal 'index.html_', @parser.extract_filename, 'empty query'
+
+ @parser.uri = URI "http://example/:"
+
+ assert_equal '_.html', @parser.extract_filename, 'colon'
+
+ @parser.uri = URI "http://example/*"
+
+ assert_equal '_.html', @parser.extract_filename, 'asterisk'
+ end
+
def test_extract_filename_uri_query
@parser.response = {}
@parser.uri = URI 'http://example/?id=5'
- assert_equal 'index.html?id=5', @parser.extract_filename
+ assert_equal 'index.html_id=5', @parser.extract_filename
@parser.uri += '/foo.html?id=5'
- assert_equal 'foo.html?id=5', @parser.extract_filename
+ assert_equal 'foo.html_id=5', @parser.extract_filename
end
def test_extract_filename_uri_slash

0 comments on commit ed0168f

Please sign in to comment.