fix: allowed ConcatenateJSON with empty input

This commit is contained in:
wener 2021-10-28 00:44:20 +08:00
parent 55ec7d9dd2
commit d1d01b6b6b
2 changed files with 39 additions and 1 deletions

View file

@ -29,6 +29,14 @@ func ConcatenateJSON(first, second []byte) ([]byte, error) {
if !bytes.HasPrefix(second, []byte{'{'}) { if !bytes.HasPrefix(second, []byte{'{'}) {
return nil, fmt.Errorf("jws: invalid JSON %s", second) return nil, fmt.Errorf("jws: invalid JSON %s", second)
} }
// check empty
if len(first) == 2 {
return second, nil
}
if len(second) == 2 {
return first, nil
}
first[len(first)-1] = ',' first[len(first)-1] = ','
first = append(first, second[1:]...) first = append(first, second[1:]...)
return first, nil return first, nil

View file

@ -44,6 +44,36 @@ func TestConcatenateJSON(t *testing.T) {
[]byte(`{"some": "thing","another": "thing"}`), []byte(`{"some": "thing","another": "thing"}`),
false, false,
}, },
{
"first empty",
args{
[]byte(`{}`),
[]byte(`{"some": "thing"}`),
},
[]byte(`{"some": "thing"}`),
false,
},
{
"second empty",
args{
[]byte(`{"some": "thing"}`),
[]byte(`{}`),
},
[]byte(`{"some": "thing"}`),
false,
},
{
"both empty",
args{
[]byte(`{}`),
[]byte(`{}`),
},
[]byte(`{}`),
false,
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
@ -53,7 +83,7 @@ func TestConcatenateJSON(t *testing.T) {
return return
} }
if !bytes.Equal(got, tt.want) { if !bytes.Equal(got, tt.want) {
t.Errorf("ConcatenateJSON() got = %v, want %v", got, tt.want) t.Errorf("ConcatenateJSON() got = %v, want %v", string(got), tt.want)
} }
}) })
} }