Skip to content

Commit

Permalink
empty secret json marshals ""
Browse files Browse the repository at this point in the history
Signed-off-by: Owen Diehl <[email protected]>
  • Loading branch information
owen-d committed May 10, 2021
1 parent a237946 commit b5fe917
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func (s *Secret) UnmarshalYAML(unmarshal func(interface{}) error) error {

// MarshalJSON implements the json.Marshaler interface for Secret.
func (s Secret) MarshalJSON() ([]byte, error) {
if len(s) == 0 {
return json.Marshal("")
}
return json.Marshal(secretToken)
}

Expand Down
37 changes: 26 additions & 11 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,33 @@ import (
)

func TestJSONMarshalSecret(t *testing.T) {
test := struct {
type tmp struct {
S Secret
}{
S: Secret("test"),
}

c, err := json.Marshal(test)
if err != nil {
t.Fatal(err)
for _, tc := range []struct {
desc string
data tmp
expected string
}{
{
desc: "inhabited",
// u003c -> "<"
// u003e -> ">"
data: tmp{"test"},
expected: "{\"S\":\"\\u003csecret\\u003e\"}",
},
{
desc: "empty",
data: tmp{},
expected: "{\"S\":\"\"}",
},
} {
t.Run(tc.desc, func(t *testing.T) {
c, err := json.Marshal(tc.data)
if err != nil {
t.Fatal(err)
}
require.Equal(t, tc.expected, string(c), "Secret not properly elided.")
})
}

// u003c -> "<"
// u003e -> ">"
require.Equal(t, "{\"S\":\"\\u003csecret\\u003e\"}", string(c), "Secret not properly elided.")
}

0 comments on commit b5fe917

Please sign in to comment.