diff options
-rw-r--r-- | lib/ffi_yajl/encoder.rb | 4 | ||||
-rw-r--r-- | spec/ffi_yajl/encoder_spec.rb | 28 |
2 files changed, 30 insertions, 2 deletions
diff --git a/lib/ffi_yajl/encoder.rb b/lib/ffi_yajl/encoder.rb index 58b817a..18ef701 100644 --- a/lib/ffi_yajl/encoder.rb +++ b/lib/ffi_yajl/encoder.rb @@ -8,7 +8,7 @@ module FFI_Yajl # initialization that we can do in pure ruby yajl_gen_opts = {} - yajl_gen_opts[:yajl_gen_validate_utf8] = true + yajl_gen_opts[:yajl_gen_validate_utf8] = @opts[:validate_utf8] == false ? false : true yajl_gen_opts[:yajl_gen_beautify] = false yajl_gen_opts[:yajl_gen_indent_string] = " " @@ -46,6 +46,8 @@ module FFI_Yajl raise FFI_Yajl::EncodeError, "Invalid number: cannot encode Infinity, -Infinity, or NaN" when 6 # yajl_gen_no_buf raise FFI_Yajl::EncodeError, "YAJL internal error: yajl_gen_get_buf was called, but a print callback was specified, so no internal buffer is available" + when 7 # yajl_gen_invalid_string + raise FFI_Yajl::EncodeError, "Invalid UTF-8 string: cannot encode to UTF-8" else raise FFI_Yajl::EncodeError, "Unknown YAJL Error (#{status}), please report this as a bug" end diff --git a/spec/ffi_yajl/encoder_spec.rb b/spec/ffi_yajl/encoder_spec.rb index 5c9e6b7..34a4d04 100644 --- a/spec/ffi_yajl/encoder_spec.rb +++ b/spec/ffi_yajl/encoder_spec.rb @@ -5,7 +5,9 @@ require 'date' describe "FFI_Yajl::Encoder" do - let(:encoder) { FFI_Yajl::Encoder.new } + let(:options) { {} } + + let(:encoder) { FFI_Yajl::Encoder.new(options) } it "encodes hashes in keys as strings", :ruby_gte_193 => true do ruby = { {'a' => 'b'} => 2 } @@ -146,4 +148,28 @@ describe "FFI_Yajl::Encoder" do end end + context "when encoding invalid utf-8" do + ruby = { + "automatic"=>{ + "etc"=>{ + "passwd"=>{ + "root"=>{"dir"=>"/root", "gid"=>0, "uid"=>0, "shell"=>"/bin/sh", "gecos"=>"Elan Ruusam\xc3\xa4e"}, + "glen"=>{"dir"=>"/home/glen", "gid"=>500, "uid"=>500, "shell"=>"/bin/bash", "gecos"=>"Elan Ruusam\xE4e"}, + } + }, + }, + } + + it "raises an error on invalid json" do + expect{ encoder.encode(ruby) }.to raise_error(FFI_Yajl::EncodeError, "Invalid UTF-8 string: cannot encode to UTF-8") + end + + context "when validate_utf8 is off" do + let(:options) { { :validate_utf8 => false } } + + it "does not raise an error" do + expect{ encoder.encode(ruby) }.not_to raise_error + end + end + end end |