2016-03-02 23:23:26 +01:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
2016-03-03 06:46:23 +01:00
|
|
|
"bytes"
|
2016-03-04 15:09:16 +01:00
|
|
|
"crypto/md5"
|
2016-03-02 23:23:26 +01:00
|
|
|
"encoding/json"
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
2016-03-27 04:43:13 +02:00
|
|
|
|
2017-04-01 15:47:14 +02:00
|
|
|
"github.com/cloudsonic/sonic-server/api/responses"
|
2016-03-27 04:43:13 +02:00
|
|
|
"github.com/smartystreets/goconvey/convey"
|
2016-03-02 23:23:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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))
|
2016-03-02 23:23:26 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02 23:23:26 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-03-03 02:00:55 +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 02:00:55 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-03-04 15:09:16 +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 ""
|
|
|
|
}
|
|
|
|
|
2016-03-02 23:23:26 +01:00
|
|
|
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
|
|
|
}
|