@@ -2,6 +2,7 @@ package scw
22
33import (
44 "encoding/json"
5+ "io/ioutil"
56 "testing"
67 "time"
78
@@ -123,3 +124,45 @@ func TestTimeSeries_UnmarshallJSON(t *testing.T) {
123124 })
124125 }
125126}
127+
128+ func TestFile_UnmarshalJSON (t * testing.T ) {
129+
130+ type testCase struct {
131+ json string
132+ name string
133+ contentType string
134+ content []byte
135+ }
136+
137+ run := func (c * testCase ) func (t * testing.T ) {
138+ return func (t * testing.T ) {
139+ f := File {}
140+ err := json .Unmarshal ([]byte (c .json ), & f )
141+ testhelpers .AssertNoError (t , err )
142+ testhelpers .Equals (t , c .name , f .Name )
143+ testhelpers .Equals (t , c .contentType , f .ContentType )
144+ s , err := ioutil .ReadAll (f .Content )
145+ testhelpers .AssertNoError (t , err )
146+ testhelpers .Equals (t , c .content , s )
147+ }
148+ }
149+
150+ t .Run ("empty" , run (& testCase {
151+ json : `{}` ,
152+ content : []byte {},
153+ }))
154+
155+ t .Run ("strint_content" , run (& testCase {
156+ json : `{"name": "test", "content_type":"text/plain", "content": "dGVzdDQyCg=="}` ,
157+ name : "test" ,
158+ contentType : "text/plain" ,
159+ content : []byte ("test42\n " ),
160+ }))
161+
162+ t .Run ("binary_content" , run (& testCase {
163+ json : `{"name": "test", "content_type":"text/plain", "content": "AAAACg=="}` ,
164+ name : "test" ,
165+ contentType : "text/plain" ,
166+ content : []byte ("\x00 \x00 \x00 \n " ),
167+ }))
168+ }
0 commit comments