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.7 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"
)
type fakeCover struct {
data string
err error
recvId string
recvSize int
}
func (c *fakeCover) Get(ctx context.Context, id string, size int, out io.Writer) error {
2020-01-10 03:58:03 +01:00
if c.err != nil {
return c.err
}
c.recvId = id
c.recvSize = size
2020-04-26 18:35:26 +02:00
_, err := out.Write([]byte(c.data))
return err
2020-01-10 03:58:03 +01:00
}
var _ = Describe("MediaRetrievalController", func() {
var controller *MediaRetrievalController
var cover *fakeCover
var w *httptest.ResponseRecorder
BeforeEach(func() {
cover = &fakeCover{}
controller = NewMediaRetrievalController(cover)
w = httptest.NewRecorder()
})
Describe("GetCoverArt", func() {
It("should return data for that id", func() {
cover.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(cover.recvId).To(Equal("34"))
Expect(cover.recvSize).To(Equal(128))
Expect(w.Body.String()).To(Equal(cover.data))
})
It("should fail if missing id parameter", func() {
r := newGetRequest()
2020-01-10 03:58:03 +01:00
_, err := controller.GetCoverArt(w, r)
Expect(err).To(MatchError("id parameter required"))
})
It("should fail when the file is not found", func() {
2020-01-15 04:22:34 +01:00
cover.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("Cover not found"))
})
It("should fail when there is an unknown error", func() {
cover.err = errors.New("weird error")
r := newGetRequest("id=34", "size=128")
2020-01-10 03:58:03 +01:00
_, err := controller.GetCoverArt(w, r)
Expect(err).To(MatchError("Internal Error"))
})
})
})