Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support unicode languages #234

Merged
merged 3 commits into from
Apr 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/rollbar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,14 @@ def enforce_valid_utf8(payload)
value = object.to_s

if value.respond_to? :encode
encoded_value = value.encode('UTF-8', 'binary', :invalid => :replace, :undef => :replace, :replace => '')
options = { :invalid => :replace, :undef => :replace, :replace => '' }
ascii_encodings = [Encoding.find('US-ASCII'), Encoding.find('ASCII-8BIT')]

args = ['UTF-8']
args << 'binary' if ascii_encodings.include?(value.encoding)
args << options

encoded_value = value.encode(*args)
else
encoded_value = ::Iconv.conv('UTF-8//IGNORE', 'UTF-8', value)
end
Expand Down
33 changes: 23 additions & 10 deletions spec/rollbar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@ def backtrace

context 'with invalid utf8 encoding' do
let(:extra) do
{ :extra => "bad value 1\255" }
{ :extra => force_to_ascii("bad value 1\255") }
end

it 'removes te invalid characteres' do
Expand Down Expand Up @@ -1336,27 +1336,40 @@ def backtrace
end

context 'enforce_valid_utf8' do
context 'with utf8 string and ruby > 1.8' do
next unless String.instance_methods.include?(:force_encoding)

let(:payload) { { :foo => 'Изменение' } }

it 'just returns the same string' do
payload_copy = payload.clone
notifier.send(:enforce_valid_utf8, payload_copy)

expect(payload_copy[:foo]).to be_eql('Изменение')
end
end

it 'should replace invalid utf8 values' do
bad_key = "inner \x92bad key"
bad_key.force_encoding('ASCII-8BIT') if bad_key.respond_to?('force_encoding')
bad_key = force_to_ascii("inner \x92bad key")

payload = {
:bad_value => "bad value 1\255",
:bad_value_2 => "bad\255 value 2",
"bad\255 key" => "good value",
:bad_value => force_to_ascii("bad value 1\255"),
:bad_value_2 => force_to_ascii("bad\255 value 2"),
force_to_ascii("bad\255 key") => "good value",
:hash => {
:inner_bad_value => "\255\255bad value 3",
:inner_bad_value => force_to_ascii("\255\255bad value 3"),
bad_key.to_sym => 'inner good value',
"bad array key\255" => [
force_to_ascii("bad array key\255") => [
'good array value 1',
"bad\255 array value 1\255",
force_to_ascii("bad\255 array value 1\255"),
{
:inner_inner_bad => "bad inner \255inner value"
:inner_inner_bad => force_to_ascii("bad inner \255inner value")
}
]
}
}


payload_copy = payload.clone
notifier.send(:enforce_valid_utf8, payload_copy)

Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
RSpec.configure do |config|
config.include(NotifierHelpers)
config.include(FixtureHelpers)
config.include(EncodingHelpers)

config.color_enabled = true
config.formatter = 'documentation'
Expand Down
8 changes: 8 additions & 0 deletions spec/support/encoding_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module EncodingHelpers
def force_to_ascii(string)
return string unless string.respond_to?('force_encoding')

string.force_encoding('ASCII-8BIT')
string
end
end