diff options
author | Matt Silverlock <matt@eatsleeprepeat.net> | 2015-05-16 20:50:49 +0300 |
---|---|---|
committer | Matt Silverlock <matt@eatsleeprepeat.net> | 2015-05-20 23:49:45 +0300 |
commit | 3c76054b695479521e1fffdc338a4a0c0c617730 (patch) | |
tree | 17c21131640ad8736c88b7d8d2c6464aaf588540 /securecookie_test.go | |
parent | 2e358078af96ec4fe5b962482254f80aabc35f64 (diff) |
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
Diffstat (limited to 'securecookie_test.go')
-rw-r--r-- | securecookie_test.go | 31 |
1 files changed, 27 insertions, 4 deletions
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) { |