Small fixes to response marshaling, introduced tests for response formats

This commit is contained in:
Deluan
2016-03-02 17:23:26 -05:00
parent b9fb5eb7ca
commit dde130e84e
10 changed files with 154 additions and 41 deletions
+7 -3
View File
@@ -9,7 +9,11 @@ type Subsonic struct {
Error *Error `xml:",omitempty" json:"error,omitempty"`
License *License `xml:",omitempty" json:"license,omitempty"`
MusicFolders *MusicFolders `xml:",omitempty" json:"musicFolders,omitempty"`
ArtistIndex *Indexes `xml:",omitempty" json:"indexes,omitempty"`
Indexes *Indexes `xml:",omitempty" json:"indexes,omitempty"`
}
type JsonWrapper struct {
Subsonic Subsonic `json:"subsonic-response"`
}
type Error struct {
@@ -31,7 +35,7 @@ type MusicFolder struct {
type MusicFolders struct {
XMLName xml.Name `xml:"musicFolders" json:"-"`
Folders []MusicFolder `xml:"musicFolders" json:"musicFolder"`
Folders []MusicFolder `xml:"musicFolders" json:"musicFolder,omitempty"`
}
type Artist struct {
@@ -48,7 +52,7 @@ type Index struct {
type Indexes struct {
XMLName xml.Name `xml:"indexes" json:"-"`
Index []Index `xml:"indexes" json:"index"`
Index []Index `xml:"indexes" json:"index,omitempty"`
LastModified string `xml:"lastModified,attr" json:"lastModified"`
IgnoredArticles string `xml:"ignoredArticles,attr" json:"ignoredArticles"`
}
+90
View File
@@ -0,0 +1,90 @@
package responses
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
. "github.com/deluan/gosonic/tests"
)
func TestSubsonicResponses(t *testing.T) {
response := &Subsonic{Status: "ok", Version: "1.0.0"}
Convey("Subject: Subsonic Responses", t, func(){
Convey("EmptyResponse", func() {
Convey("XML", func() {
So(response, ShouldMatchXML, `<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.0.0"></subsonic-response>`)
})
Convey("JSON", func() {
So(response, ShouldMatchJSON, `{"status":"ok","version":"1.0.0"}`)
})
})
Convey("License", func() {
response.License = &License{Valid: true}
Convey("XML", func() {
So(response, ShouldMatchXML, `<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.0.0"><license valid="true"></license></subsonic-response>`)
})
Convey("JSON", func() {
So(response, ShouldMatchJSON, `{"license":{"valid":true},"status":"ok","version":"1.0.0"}`)
})
})
Convey("MusicFolders", func() {
response.MusicFolders = &MusicFolders{}
Convey("With data", func() {
folders := make([]MusicFolder, 2)
folders[0] = MusicFolder{Id: "111", Name: "aaa"}
folders[1] = MusicFolder{Id: "222", Name: "bbb"}
response.MusicFolders.Folders = folders
Convey("XML", func() {
So(response, ShouldMatchXML, `<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.0.0"><musicFolders><musicFolder id="111" name="aaa"></musicFolder><musicFolder id="222" name="bbb"></musicFolder></musicFolders></subsonic-response>`)
})
Convey("JSON", func() {
So(response, ShouldMatchJSON, `{"musicFolders":{"musicFolder":[{"id":"111","name":"aaa"},{"id":"222","name":"bbb"}]},"status":"ok","version":"1.0.0"}`)
})
})
Convey("Without data", func() {
Convey("XML", func() {
So(response, ShouldMatchXML, `<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.0.0"><musicFolders></musicFolders></subsonic-response>`)
})
Convey("JSON", func() {
So(response, ShouldMatchJSON, `{"musicFolders":{},"status":"ok","version":"1.0.0"}`)
})
})
})
Convey("Indexes", func() {
artists := make([]Artist, 1)
artists[0] = Artist{Id: "111", Name: "aaa"}
response.Indexes = &Indexes{LastModified:"1", IgnoredArticles:"A"}
Convey("With data", func() {
index := make([]Index, 1)
index[0] = Index{Name: "A", Artists: artists}
response.Indexes.Index = index
Convey("XML", func() {
So(response, ShouldMatchXML, `<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.0.0"><indexes lastModified="1" ignoredArticles="A"><index name="A"><artist id="111" name="aaa"></artist></index></indexes></subsonic-response>`)
})
Convey("JSON", func() {
So(response, ShouldMatchJSON, `{"indexes":{"ignoredArticles":"A","index":[{"artist":[{"id":"111","name":"aaa"}],"name":"A"}],"lastModified":"1"},"status":"ok","version":"1.0.0"}`)
})
})
Convey("Without data", func() {
Convey("XML", func() {
So(response, ShouldMatchXML, `<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.0.0"><indexes lastModified="1" ignoredArticles="A"></indexes></subsonic-response>`)
})
Convey("JSON", func() {
So(response, ShouldMatchJSON, `{"indexes":{"ignoredArticles":"A","lastModified":"1"},"status":"ok","version":"1.0.0"}`)
})
})
})
Reset(func() {
response = &Subsonic{Status: "ok", Version: "1.0.0"}
})
})
}