navidrome/server/subsonic/media_retrieval_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

78 lines
1.8 KiB
Go
Raw Normal View History

package subsonic
2020-01-09 02:45:07 +01:00
2020-01-10 03:58:03 +01:00
import (
"context"
2020-01-10 03:58:03 +01:00
"errors"
"io"
"net/http/httptest"
2020-01-24 01:44:08 +01:00
"github.com/deluan/navidrome/model"
2020-01-10 03:58:03 +01:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("MediaRetrievalController", func() {
var controller *MediaRetrievalController
var artwork *fakeArtwork
2020-01-10 03:58:03 +01:00
var w *httptest.ResponseRecorder
BeforeEach(func() {
artwork = &fakeArtwork{}
controller = NewMediaRetrievalController(artwork)
2020-01-10 03:58:03 +01:00
w = httptest.NewRecorder()
})
Describe("GetCoverArt", func() {
It("should return data for that id", func() {
artwork.data = "image data"
r := newGetRequest("id=34", "size=128")
2020-01-10 03:58:03 +01:00
_, err := controller.GetCoverArt(w, r)
Expect(err).To(BeNil())
Expect(artwork.recvId).To(Equal("34"))
Expect(artwork.recvSize).To(Equal(128))
Expect(w.Body.String()).To(Equal(artwork.data))
2020-01-10 03:58:03 +01:00
})
It("should fail if missing id parameter", func() {
r := newGetRequest()
2020-01-10 03:58:03 +01:00
_, err := controller.GetCoverArt(w, r)
2020-10-27 20:23:29 +01:00
Expect(err).To(MatchError("required 'id' parameter is missing"))
2020-01-10 03:58:03 +01:00
})
It("should fail when the file is not found", func() {
artwork.err = model.ErrNotFound
r := newGetRequest("id=34", "size=128")
2020-01-10 03:58:03 +01:00
_, err := controller.GetCoverArt(w, r)
Expect(err).To(MatchError("Artwork not found"))
2020-01-10 03:58:03 +01:00
})
It("should fail when there is an unknown error", func() {
artwork.err = errors.New("weird error")
r := newGetRequest("id=34", "size=128")
2020-01-10 03:58:03 +01:00
_, err := controller.GetCoverArt(w, r)
2020-10-27 20:23:29 +01:00
Expect(err).To(MatchError("weird error"))
2020-01-10 03:58:03 +01:00
})
})
})
type fakeArtwork struct {
data string
err error
recvId string
recvSize int
}
func (c *fakeArtwork) Get(ctx context.Context, id string, size int, out io.Writer) error {
if c.err != nil {
return c.err
}
c.recvId = id
c.recvSize = size
_, err := out.Write([]byte(c.data))
return err
}