From bffaa888ac4a1ee60a9f93650b9184a9402eff09 Mon Sep 17 00:00:00 2001 From: Willem van Bergen Date: Thu, 15 Sep 2011 08:28:53 -0400 Subject: Custom serializers and deserializers in MessageVerifier and MessageEncryptor. By default, these classes use Marshal for serializing and deserializing messages. Unfortunately, the Marshal format is closely associated with Ruby internals and even changes between different interpreters. This makes the resulting message very hard to impossible to unserialize messages generated by these classes in other environments like node.js. This patch solves this by allowing you to set your own custom serializer and deserializer lambda functions. By default, it still uses Marshal to be backwards compatible. --- activesupport/test/message_verifier_test.rb | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'activesupport/test/message_verifier_test.rb') diff --git a/activesupport/test/message_verifier_test.rb b/activesupport/test/message_verifier_test.rb index 4821311244..83f34ebc33 100644 --- a/activesupport/test/message_verifier_test.rb +++ b/activesupport/test/message_verifier_test.rb @@ -8,6 +8,7 @@ rescue LoadError, NameError else require 'active_support/time' +require 'active_support/json' class MessageVerifierTest < Test::Unit::TestCase def setup @@ -31,6 +32,14 @@ class MessageVerifierTest < Test::Unit::TestCase assert_not_verified("#{data}--#{hash.reverse}") assert_not_verified("purejunk") end + + def test_alternative_serialization_method + @verifier.serializer = lambda { |value| ActiveSupport::JSON.encode(value) } + @verifier.deserializer = lambda { |value| ActiveSupport::JSON.decode(value) } + + message = @verifier.generate({ :foo => 123, 'bar' => Time.local(2010) }) + assert_equal @verifier.verify(message), { "foo" => 123, "bar" => "2010-01-01T00:00:00-05:00" } + end def assert_not_verified(message) assert_raise(ActiveSupport::MessageVerifier::InvalidSignature) do -- cgit v1.2.3