open-vault/sdk/logical/token_test.go
Calvin Leung Huang 522fa83568 sdk/logical: handle empty token type string values as TokenTypeDefault (#7273)
* sdk/logical: handle empty token type string values as TokenTypeDefault

* add test case for missing token_type value
2019-08-14 09:45:40 -04:00

44 lines
756 B
Go

package logical
import (
"encoding/json"
"testing"
)
func TestJSONSerialization(t *testing.T) {
tt := TokenTypeDefaultBatch
s, err := json.Marshal(tt)
if err != nil {
t.Fatal(err)
}
var utt TokenType
err = json.Unmarshal(s, &utt)
if err != nil {
t.Fatal(err)
}
if tt != utt {
t.Fatalf("expected %v, got %v", tt, utt)
}
utt = TokenTypeDefault
err = json.Unmarshal([]byte(`"default-batch"`), &utt)
if err != nil {
t.Fatal(err)
}
if tt != utt {
t.Fatalf("expected %v, got %v", tt, utt)
}
// Test on an empty value, which should unmarshal into TokenTypeDefault
tt = TokenTypeDefault
err = json.Unmarshal([]byte(`""`), &utt)
if err != nil {
t.Fatal(err)
}
if tt != utt {
t.Fatalf("expected %v, got %v", tt, utt)
}
}