navidrome/tests/matchers.go

69 lines
1.7 KiB
Go
Raw Normal View History

package tests
import (
2016-03-03 06:46:23 +01:00
"bytes"
"crypto/md5"
"encoding/json"
"encoding/xml"
"fmt"
2016-03-27 04:43:13 +02:00
"github.com/cloudsonic/sonic-server/server/subsonic/responses"
2016-03-27 04:43:13 +02:00
"github.com/smartystreets/goconvey/convey"
)
func ShouldMatchXML(actual interface{}, expected ...interface{}) string {
xml, err := xml.Marshal(actual)
if err != nil {
return fmt.Sprintf("Malformed XML: %v", err)
}
2016-03-27 04:43:13 +02:00
return convey.ShouldEqual(string(xml), expected[0].(string))
}
func ShouldMatchJSON(actual interface{}, expected ...interface{}) string {
json, err := json.Marshal(actual)
if err != nil {
return fmt.Sprintf("Malformed JSON: %v", err)
}
s := UnindentJSON(json)
2016-03-27 04:43:13 +02:00
return convey.ShouldEqual(s, expected[0].(string))
}
2016-03-03 02:50:16 +01:00
func ShouldContainJSON(actual interface{}, expected ...interface{}) string {
a := UnindentJSON(actual.(*bytes.Buffer).Bytes())
2016-03-27 04:43:13 +02:00
return convey.ShouldContainSubstring(a, expected[0].(string))
2016-03-03 02:50:16 +01:00
}
func ShouldReceiveError(actual interface{}, expected ...interface{}) string {
v := responses.Subsonic{}
err := xml.Unmarshal(actual.(*bytes.Buffer).Bytes(), &v)
if err != nil {
return fmt.Sprintf("Malformed XML: %v", err)
}
2016-03-27 04:43:13 +02:00
return convey.ShouldEqual(v.Error.Code, expected[0].(int))
}
2016-03-03 18:08:44 +01:00
func ShouldMatchMD5(actual interface{}, expected ...interface{}) string {
a := fmt.Sprintf("%x", md5.Sum(actual.([]byte)))
2016-03-27 04:43:13 +02:00
return convey.ShouldEqual(a, expected[0].(string))
2016-03-03 18:08:44 +01:00
}
func ShouldBeAValid(actual interface{}, expected ...interface{}) string {
v := responses.Subsonic{}
err := json.Unmarshal(actual.(*bytes.Buffer).Bytes(), &v)
if err != nil {
return fmt.Sprintf("Malformed response: %v", err)
}
return ""
}
func UnindentJSON(j []byte) string {
var m = make(map[string]interface{})
json.Unmarshal(j, &m)
s, _ := json.Marshal(m)
return string(s)
2016-03-03 06:46:23 +01:00
}