Permalink
Browse files

Handle badly encoded Marshal'd messages.

  • Loading branch information...
1 parent 053b9ed commit e9fb63f97be1602e91ae107d41ef805cda863d0f @tomlea tomlea committed Mar 6, 2013
Showing with 25 additions and 5 deletions.
  1. +14 −5 lib/super_poller/starling_queue.rb
  2. +11 −0 test/starling_queue_test.rb
@@ -42,12 +42,21 @@ def flush
end
end
- protected
+ if "".respond_to? :force_encoding
+ def fix_encoding(v)
+ v.force_encoding('UTF-8')
+ end
+ else
+ def fix_encoding(v)
+ v
+ end
+ end
+
def coerce(message)
- if message.start_with?('{')
- MultiJson.decode(message)
- else
- Marshal.load(message)
+ unless message.start_with?('{')
+ message = MultiJson.dump(Marshal.load(message))
end
+ message = fix_encoding(message)
+ MultiJson.decode(message)
end
end
@@ -1,3 +1,4 @@
+# encoding: UTF-8
require File.join(File.dirname(__FILE__), "test_helper")
class StarlingQueueTest < Test::Unit::TestCase
@@ -41,6 +42,16 @@ class StarlingQueueTest < Test::Unit::TestCase
assert queue.empty?
end
+ if ''.respond_to? :encoding
+ should 'force encoding to UTF8' do
+ instance = StarlingQueue.allocate
+ ascii_string = Marshal.dump({"a" => "\xC3\xA3".force_encoding('ascii')})
+ object = instance.coerce(ascii_string)
+
+ assert_equal "ã", object["a"]
+ assert_equal Encoding.find('utf-8'), object["a"].encoding
+ end
+ end
end
end

0 comments on commit e9fb63f

Please sign in to comment.