11package logging
22
33import (
4+ "encoding/json"
45 "io"
56 "io/ioutil"
67 "net/http"
78 "net/http/httptest"
89 "os"
910 "testing"
1011
12+ "github.com/stretchr/testify/assert"
13+
1114 "github.com/Jeffail/gabs"
1215 signer "github.com/philips-software/go-hsdp-signer"
1316)
@@ -187,17 +190,10 @@ func TestStoreResourcesWithInvalidKeypair(t *testing.T) {
187190 }
188191
189192 resp , err := client .StoreResources (resource , len (resource ))
190- if resp == nil {
191- t .Errorf ("Unexpected nil value for response" )
192- return
193- }
193+ assert .NotNil (t , resp )
194194 _ = err .Error () // Just to up coverage
195- if resp .StatusCode != http .StatusForbidden {
196- t .Errorf ("Expected HTTP 403, Got: %d" , resp .StatusCode )
197- }
198- if err == nil {
199- t .Errorf ("Expected error response" )
200- }
195+ assert .Equal (t , http .StatusForbidden , resp .StatusCode )
196+ assert .NotNil (t , err )
201197}
202198
203199func TestConfig (t * testing.T ) {
@@ -219,3 +215,30 @@ func TestConfig(t *testing.T) {
219215 }
220216 }
221217}
218+
219+ func TestReplaceScaryCharacters (t * testing.T ) {
220+ var invalidResource = Resource {
221+ ResourceType : "LogEvent" ,
222+ Custom : []byte (`{
223+ "foo": "bar",
224+ "bad1": ";",
225+ "bad2": "<key/>",
226+ "bad3": "&",
227+ "bad4": "a\\b",
228+ "bad5": "a\b"
229+ }` ),
230+ }
231+ replaceScaryCharacters (& invalidResource )
232+
233+ var custom map [string ]interface {}
234+ err := json .Unmarshal (invalidResource .Custom , & custom )
235+ if ! assert .Nil (t , err ) {
236+ return
237+ }
238+ assert .Equal (t , "bar" , custom ["foo" ].(string ))
239+ assert .Equal (t , "💀[semicolon]" , custom ["bad1" ].(string ))
240+ assert .Equal (t , "👾[lt]key/👿[gt]" , custom ["bad2" ].(string ))
241+ assert .Equal (t , "👻[amp]amp💀[semicolon]" , custom ["bad3" ].(string ))
242+ assert .Equal (t , "a🎃[backslash]b" , custom ["bad4" ].(string ))
243+ assert .Equal (t , "a🎃[bs]" , custom ["bad5" ].(string ))
244+ }
0 commit comments