Browse files

Updated Encodings.param_encode(string) so it intelligently encodes an…

…d quotes strings (leaves no-space ascii unquoted and unencoded, encodes or quotes others)
  • Loading branch information...
1 parent d596fa6 commit 4f2baf20dc20a4cf166722fd18d0c28926bd4950 @mikel committed Nov 18, 2009
Showing with 34 additions and 4 deletions.
  1. +5 −0 CHANGELOG.rdoc
  2. +8 −1 lib/mail/encodings/encodings.rb
  3. +21 −3 spec/mail/encodings/encodings_spec.rb
View
5 CHANGELOG.rdoc
@@ -1,3 +1,8 @@
+== Wed Nov 18 04:26:21 UTC 2009 Mikel Lindsaar <raasdnil@gmail.com>
+
+* Changed Encodings.param_encode(string) so it intelligently encodes and quotes needed
+ items and leaves plain, no special char, US-ASCII alone unquoted.
+
== Sat Nov 14 08:20:21 UTC 2009 Mikel Lindsaar <raasdnil@gmail.com>
* Resolved Issue #10 - empty/nil cc/bcc field causes exception (Mail::Field::ParseError)
View
9 lib/mail/encodings/encodings.rb
@@ -41,7 +41,14 @@ def Encodings.get_encoding( str )
#
# Mail::Encodings.param_encode("This is fun") #=> "us-ascii'en'This%20is%20fun"
def Encodings.param_encode(str)
- RubyVer.param_encode(str)
+ case
+ when str.ascii_only? && str =~ TOKEN_UNSAFE
+ %Q{"#{str}"}
+ when str.ascii_only?
+ str
+ else
+ RubyVer.param_encode(str)
+ end
end
# Decodes a parameter value using URI Escaping.
View
24 spec/mail/encodings/encodings_spec.rb
@@ -263,11 +263,29 @@
end
it "should encode a string" do
- string = "This is even more "
+ string = "This is あ string"
if RUBY_VERSION >= '1.9'
- Mail::Encodings.param_encode(string).should == "utf-8'en'This%20is%20even%20more%20"
+ Mail::Encodings.param_encode(string).should == "utf-8'en'This%20is%20%20%E3%81%82%20string"
else
- Mail::Encodings.param_encode(string).should == "utf8'en'This%20is%20even%20more%20"
+ Mail::Encodings.param_encode(string).should == "utf8'en'This%20is%20%20%E3%81%82%20string"
+ end
+ end
+
+ it "should just quote US-ASCII with spaces" do
+ string = "This is even more"
+ if RUBY_VERSION >= '1.9'
+ Mail::Encodings.param_encode(string).should == '"This is even more"'
+ else
+ Mail::Encodings.param_encode(string).should == '"This is even more"'
+ end
+ end
+
+ it "should leave US-ASCII without spaces alone" do
+ string = "fun"
+ if RUBY_VERSION >= '1.9'
+ Mail::Encodings.param_encode(string).should == 'fun'
+ else
+ Mail::Encodings.param_encode(string).should == 'fun'
end
end

0 comments on commit 4f2baf2

Please sign in to comment.