Raise error on non utf-8 string in 1.8
This commit is contained in:
parent
8ff0d73fd8
commit
a7c75d7fae
@ -27,6 +27,9 @@ module Mongo
|
||||
# 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
|
||||
|
||||
|
@ -55,7 +55,12 @@ class BSON
|
||||
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
|
||||
|
||||
|
@ -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
|
||||
@ -20,6 +21,20 @@ class BSONTest < Test::Unit::TestCase
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user