2020-01-20 00:21:44 +01:00
|
|
|
package subsonic
|
2020-01-09 02:45:07 +01:00
|
|
|
|
2020-01-10 03:58:03 +01:00
|
|
|
import (
|
2020-01-22 14:32:31 +01:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2020-01-22 14:32:31 +01:00
|
|
|
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
|
|
|
|
out.Write([]byte(c.data))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
2020-01-27 21:10:46 +01:00
|
|
|
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() {
|
2020-01-27 21:10:46 +01:00
|
|
|
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
|
2020-01-27 21:10:46 +01:00
|
|
|
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")
|
2020-01-27 21:10:46 +01:00
|
|
|
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"))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|