Retry calls to Last.FM without MBIDs when if returns artist invalid (#1138)
* Call Last.FM's getInfo again without mbid when artist is not found * Call Last.FM's getSimilar again without mbid when artist is not found * Call Last.FM's getTopTracks again without mbid when artist is not found
This commit is contained in:
+39
-85
@@ -8,49 +8,71 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/navidrome/navidrome/tests"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Client", func() {
|
||||
var httpClient *fakeHttpClient
|
||||
var httpClient *tests.FakeHttpClient
|
||||
var client *Client
|
||||
|
||||
BeforeEach(func() {
|
||||
httpClient = &fakeHttpClient{}
|
||||
httpClient = &tests.FakeHttpClient{}
|
||||
client = NewClient("API_KEY", "pt", httpClient)
|
||||
})
|
||||
|
||||
Describe("ArtistGetInfo", func() {
|
||||
It("returns an artist for a successful response", func() {
|
||||
f, _ := os.Open("tests/fixtures/lastfm.artist.getinfo.json")
|
||||
httpClient.res = http.Response{Body: f, StatusCode: 200}
|
||||
httpClient.Res = http.Response{Body: f, StatusCode: 200}
|
||||
|
||||
artist, err := client.ArtistGetInfo(context.TODO(), "U2", "123")
|
||||
Expect(err).To(BeNil())
|
||||
Expect(artist.Name).To(Equal("U2"))
|
||||
Expect(httpClient.savedRequest.URL.String()).To(Equal(apiBaseUrl + "?api_key=API_KEY&artist=U2&format=json&lang=pt&mbid=123&method=artist.getInfo"))
|
||||
Expect(httpClient.SavedRequest.URL.String()).To(Equal(apiBaseUrl + "?api_key=API_KEY&artist=U2&format=json&lang=pt&mbid=123&method=artist.getInfo"))
|
||||
})
|
||||
|
||||
It("fails if Last.FM returns an error", func() {
|
||||
httpClient.res = http.Response{
|
||||
It("fails if Last.FM returns an http status != 200", func() {
|
||||
httpClient.Res = http.Response{
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(`Internal Server Error`)),
|
||||
StatusCode: 500,
|
||||
}
|
||||
|
||||
_, err := client.ArtistGetInfo(context.TODO(), "U2", "123")
|
||||
Expect(err).To(MatchError("last.fm http status: (500)"))
|
||||
})
|
||||
|
||||
It("fails if Last.FM returns an http status != 200", func() {
|
||||
httpClient.Res = http.Response{
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(`{"error":3,"message":"Invalid Method - No method with that name in this package"}`)),
|
||||
StatusCode: 400,
|
||||
}
|
||||
|
||||
_, err := client.ArtistGetInfo(context.TODO(), "U2", "123")
|
||||
Expect(err).To(MatchError("last.fm error(3): Invalid Method - No method with that name in this package"))
|
||||
Expect(err).To(MatchError(&Error{Code: 3, Message: "Invalid Method - No method with that name in this package"}))
|
||||
})
|
||||
|
||||
It("fails if Last.FM returns an error", func() {
|
||||
httpClient.Res = http.Response{
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(`{"error":6,"message":"The artist you supplied could not be found"}`)),
|
||||
StatusCode: 200,
|
||||
}
|
||||
|
||||
_, err := client.ArtistGetInfo(context.TODO(), "U2", "123")
|
||||
Expect(err).To(MatchError(&Error{Code: 6, Message: "The artist you supplied could not be found"}))
|
||||
})
|
||||
|
||||
It("fails if HttpClient.Do() returns error", func() {
|
||||
httpClient.err = errors.New("generic error")
|
||||
httpClient.Err = errors.New("generic error")
|
||||
|
||||
_, err := client.ArtistGetInfo(context.TODO(), "U2", "123")
|
||||
Expect(err).To(MatchError("generic error"))
|
||||
})
|
||||
|
||||
It("fails if returned body is not a valid JSON", func() {
|
||||
httpClient.res = http.Response{
|
||||
httpClient.Res = http.Response{
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(`<xml>NOT_VALID_JSON</xml>`)),
|
||||
StatusCode: 200,
|
||||
}
|
||||
@@ -64,92 +86,24 @@ var _ = Describe("Client", func() {
|
||||
Describe("ArtistGetSimilar", func() {
|
||||
It("returns an artist for a successful response", func() {
|
||||
f, _ := os.Open("tests/fixtures/lastfm.artist.getsimilar.json")
|
||||
httpClient.res = http.Response{Body: f, StatusCode: 200}
|
||||
httpClient.Res = http.Response{Body: f, StatusCode: 200}
|
||||
|
||||
artists, err := client.ArtistGetSimilar(context.TODO(), "U2", "123", 2)
|
||||
similar, err := client.ArtistGetSimilar(context.TODO(), "U2", "123", 2)
|
||||
Expect(err).To(BeNil())
|
||||
Expect(len(artists)).To(Equal(2))
|
||||
Expect(httpClient.savedRequest.URL.String()).To(Equal(apiBaseUrl + "?api_key=API_KEY&artist=U2&format=json&limit=2&mbid=123&method=artist.getSimilar"))
|
||||
})
|
||||
|
||||
It("fails if Last.FM returns an error", func() {
|
||||
httpClient.res = http.Response{
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(`{"error":3,"message":"Invalid Method - No method with that name in this package"}`)),
|
||||
StatusCode: 400,
|
||||
}
|
||||
|
||||
_, err := client.ArtistGetSimilar(context.TODO(), "U2", "123", 2)
|
||||
Expect(err).To(MatchError("last.fm error(3): Invalid Method - No method with that name in this package"))
|
||||
})
|
||||
|
||||
It("fails if HttpClient.Do() returns error", func() {
|
||||
httpClient.err = errors.New("generic error")
|
||||
|
||||
_, err := client.ArtistGetSimilar(context.TODO(), "U2", "123", 2)
|
||||
Expect(err).To(MatchError("generic error"))
|
||||
})
|
||||
|
||||
It("fails if returned body is not a valid JSON", func() {
|
||||
httpClient.res = http.Response{
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(`<xml>NOT_VALID_JSON</xml>`)),
|
||||
StatusCode: 200,
|
||||
}
|
||||
|
||||
_, err := client.ArtistGetSimilar(context.TODO(), "U2", "123", 2)
|
||||
Expect(err).To(MatchError("invalid character '<' looking for beginning of value"))
|
||||
Expect(len(similar.Artists)).To(Equal(2))
|
||||
Expect(httpClient.SavedRequest.URL.String()).To(Equal(apiBaseUrl + "?api_key=API_KEY&artist=U2&format=json&limit=2&mbid=123&method=artist.getSimilar"))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("ArtistGetTopTracks", func() {
|
||||
It("returns top tracks for a successful response", func() {
|
||||
f, _ := os.Open("tests/fixtures/lastfm.artist.gettoptracks.json")
|
||||
httpClient.res = http.Response{Body: f, StatusCode: 200}
|
||||
httpClient.Res = http.Response{Body: f, StatusCode: 200}
|
||||
|
||||
tracks, err := client.ArtistGetTopTracks(context.TODO(), "U2", "123", 2)
|
||||
top, err := client.ArtistGetTopTracks(context.TODO(), "U2", "123", 2)
|
||||
Expect(err).To(BeNil())
|
||||
Expect(len(tracks)).To(Equal(2))
|
||||
Expect(httpClient.savedRequest.URL.String()).To(Equal(apiBaseUrl + "?api_key=API_KEY&artist=U2&format=json&limit=2&mbid=123&method=artist.getTopTracks"))
|
||||
})
|
||||
|
||||
It("fails if Last.FM returns an error", func() {
|
||||
httpClient.res = http.Response{
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(`{"error":3,"message":"Invalid Method - No method with that name in this package"}`)),
|
||||
StatusCode: 400,
|
||||
}
|
||||
|
||||
_, err := client.ArtistGetTopTracks(context.TODO(), "U2", "123", 2)
|
||||
Expect(err).To(MatchError("last.fm error(3): Invalid Method - No method with that name in this package"))
|
||||
})
|
||||
|
||||
It("fails if HttpClient.Do() returns error", func() {
|
||||
httpClient.err = errors.New("generic error")
|
||||
|
||||
_, err := client.ArtistGetTopTracks(context.TODO(), "U2", "123", 2)
|
||||
Expect(err).To(MatchError("generic error"))
|
||||
})
|
||||
|
||||
It("fails if returned body is not a valid JSON", func() {
|
||||
httpClient.res = http.Response{
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(`<xml>NOT_VALID_JSON</xml>`)),
|
||||
StatusCode: 200,
|
||||
}
|
||||
|
||||
_, err := client.ArtistGetTopTracks(context.TODO(), "U2", "123", 2)
|
||||
Expect(err).To(MatchError("invalid character '<' looking for beginning of value"))
|
||||
Expect(len(top.Track)).To(Equal(2))
|
||||
Expect(httpClient.SavedRequest.URL.String()).To(Equal(apiBaseUrl + "?api_key=API_KEY&artist=U2&format=json&limit=2&mbid=123&method=artist.getTopTracks"))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
type fakeHttpClient struct {
|
||||
res http.Response
|
||||
err error
|
||||
savedRequest *http.Request
|
||||
}
|
||||
|
||||
func (c *fakeHttpClient) Do(req *http.Request) (*http.Response, error) {
|
||||
c.savedRequest = req
|
||||
if c.err != nil {
|
||||
return nil, c.err
|
||||
}
|
||||
return &c.res, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user