diff options
author | Andrew Newdigate <andrew@gitlab.com> | 2017-10-18 21:56:42 +0100 |
---|---|---|
committer | Andrew Newdigate <andrew@gitlab.com> | 2017-10-18 21:56:42 +0100 |
commit | a86b8c0a1db8a66f4b156cd60551525a3841548f (patch) | |
tree | 352816bb5d4783824976feb44fbff0c46421ebab | |
parent | 1663cb9127187787aa114bc8a5cde7ba7e80c5ec (diff) | |
download | gitlab-ce-an/equifax.tar.gz |
Don't re-encode UTF8 as UTF8an/equifax
-rw-r--r-- | lib/gitlab/encoding_helper.rb | 3 | ||||
-rw-r--r-- | spec/lib/gitlab/encoding_helper_spec.rb | 16 |
2 files changed, 16 insertions, 3 deletions
diff --git a/lib/gitlab/encoding_helper.rb b/lib/gitlab/encoding_helper.rb index 7b3483a7f96..406d648e64c 100644 --- a/lib/gitlab/encoding_helper.rb +++ b/lib/gitlab/encoding_helper.rb @@ -50,6 +50,9 @@ module Gitlab end def encode_utf8(message) + return nil if message == nil + return message if message.encoding == Encoding::UTF_8 + detect = CharlockHolmes::EncodingDetector.detect(message) if detect && detect[:encoding] begin diff --git a/spec/lib/gitlab/encoding_helper_spec.rb b/spec/lib/gitlab/encoding_helper_spec.rb index 3793f1590c5..2d288f1fc23 100644 --- a/spec/lib/gitlab/encoding_helper_spec.rb +++ b/spec/lib/gitlab/encoding_helper_spec.rb @@ -82,6 +82,16 @@ describe Gitlab::EncodingHelper do describe '#encode_utf8' do [ [ + "nil", + nil, + nil + ], + [ + "empty string", + "".encode("ASCII-8BIT"), + "".encode("UTF-8") + ], + [ "encodes valid utf8 encoded string to utf8", "λ, λ, λ".encode("UTF-8"), "λ, λ, λ".encode("UTF-8") @@ -93,7 +103,7 @@ describe Gitlab::EncodingHelper do ], [ "encodes valid ISO-8859-1 encoded string to utf8", - "Rüby ist eine Programmiersprache. Wir verlängern den text damit ICU die Sprache erkennen kann.".encode("ISO-8859-1", "UTF-8"), + "Rüby ist eine Programmiersprache. Wir verlängern den text damit ICU die Sprache erkennen kann.".encode("ISO-8859-1"), "Rüby ist eine Programmiersprache. Wir verlängern den text damit ICU die Sprache erkennen kann.".encode("UTF-8") ], [ @@ -104,9 +114,9 @@ describe Gitlab::EncodingHelper do ] ].each do |description, test_string, xpect| it description do - r = ext_class.encode_utf8(test_string.force_encoding('UTF-8')) + r = ext_class.encode_utf8(test_string) expect(r).to eq(xpect) - expect(r.encoding.name).to eq('UTF-8') + expect(r.encoding.name).to eq('UTF-8') if !xpect.nil? end end |