Skip to content

Commit

Permalink
Raise error on non utf-8 string in 1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
banker committed Nov 30, 2009
1 parent 8ff0d73 commit a7c75d7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/mongo/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class ConfigurationError < MongoRubyError; end
# Raised when invalid arguments are sent to Mongo Ruby methods.
class MongoArgumentError < MongoRubyError; end

# Raised when given a string is not valid utf-8 (Ruby 1.8 only).
class InvalidStringEncoding < MongoRubyError; end

# Raised on failures in connection to the database server.
class ConnectionError < MongoRubyError; end

Expand Down
7 changes: 6 additions & 1 deletion lib/mongo/util/bson.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ def self.to_utf8(str)
end
else
def self.to_utf8(str)
str # TODO Ruby 1.8 punt for now
begin
str.unpack("U*")
rescue => ex
raise InvalidStringEncoding, "String not valid utf-8: #{str}"
end
str
end
end

Expand Down
15 changes: 15 additions & 0 deletions test/test_bson.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
require 'mongo'
require 'mongo/util/ordered_hash'
require 'iconv'
require 'test/unit'

class BSONTest < Test::Unit::TestCase
Expand All @@ -20,6 +21,20 @@ def test_string
assert_equal doc, @b.deserialize
end

def test_valid_utf8_string
doc = {'doc' => "aéあ"}
@b.serialize(doc)
assert_equal doc, @b.deserialize
end

def test_invalid_string
string = Iconv.conv('iso-8859-1', 'utf-8', 'aé').first
doc = {'doc' => string}
assert_raise InvalidStringEncoding do
@b.serialize(doc)
end
end

def test_code
doc = {'$where' => Code.new('this.a.b < this.b')}
@b.serialize(doc)
Expand Down

0 comments on commit a7c75d7

Please sign in to comment.