From 3c76054b695479521e1fffdc338a4a0c0c617730 Mon Sep 17 00:00:00 2001 From: Matt Silverlock Date: Sat, 16 May 2015 17:50:49 +0000 Subject: Added a JSON encoder/decoder to securecookie. A new "Serializer" interface with serialize/deserialize methods allows custom encoders to be specified. encoding/gob remains the default for compatibility/ease-of-use reasons, but the (often faster) encoding/json is now an option. Fixed typo - TestEncription => TestEncryption --- securecookie_test.go | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'securecookie_test.go') diff --git a/securecookie_test.go b/securecookie_test.go index 241ff10..76368a9 100644 --- a/securecookie_test.go +++ b/securecookie_test.go @@ -101,7 +101,7 @@ func TestAuthentication(t *testing.T) { } } -func TestEncription(t *testing.T) { +func TestEncryption(t *testing.T) { block, err := aes.NewCipher([]byte("1234567890123456")) if err != nil { t.Fatalf("Block could not be created") @@ -121,18 +121,41 @@ func TestEncription(t *testing.T) { } } -func TestSerialization(t *testing.T) { +func TestGobSerialization(t *testing.T) { var ( + sz GobEncoder serialized []byte deserialized map[string]string err error ) for _, value := range testCookies { - if serialized, err = serialize(value); err != nil { + if serialized, err = sz.Serialize(value); err != nil { t.Error(err) } else { deserialized = make(map[string]string) - if err = deserialize(serialized, &deserialized); err != nil { + if err = sz.Deserialize(serialized, &deserialized); err != nil { + t.Error(err) + } + if fmt.Sprintf("%v", deserialized) != fmt.Sprintf("%v", value) { + t.Errorf("Expected %v, got %v.", value, deserialized) + } + } + } +} + +func TestJSONSerialization(t *testing.T) { + var ( + sz JSONEncoder + serialized []byte + deserialized map[string]string + err error + ) + for _, value := range testCookies { + if serialized, err = sz.Serialize(value); err != nil { + t.Error(err) + } else { + deserialized = make(map[string]string) + if err = sz.Deserialize(serialized, &deserialized); err != nil { t.Error(err) } if fmt.Sprintf("%v", deserialized) != fmt.Sprintf("%v", value) { -- cgit v1.2.3