fix(lastfm): send parameters in request body for POST requests in scrobble and updateNowPlaying methods
Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
@@ -265,7 +266,8 @@ var _ = Describe("lastfmAgent", func() {
|
|||||||
|
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
Expect(httpClient.SavedRequest.Method).To(Equal(http.MethodPost))
|
Expect(httpClient.SavedRequest.Method).To(Equal(http.MethodPost))
|
||||||
sentParams := httpClient.SavedRequest.URL.Query()
|
body, _ := io.ReadAll(httpClient.SavedRequest.Body)
|
||||||
|
sentParams, _ := url.ParseQuery(string(body))
|
||||||
Expect(sentParams.Get("method")).To(Equal("track.updateNowPlaying"))
|
Expect(sentParams.Get("method")).To(Equal("track.updateNowPlaying"))
|
||||||
Expect(sentParams.Get("sk")).To(Equal("SK-1"))
|
Expect(sentParams.Get("sk")).To(Equal("SK-1"))
|
||||||
Expect(sentParams.Get("track")).To(Equal(track.Title))
|
Expect(sentParams.Get("track")).To(Equal(track.Title))
|
||||||
@@ -293,7 +295,8 @@ var _ = Describe("lastfmAgent", func() {
|
|||||||
err := agent.NowPlaying(ctx, "user-1", track, 0)
|
err := agent.NowPlaying(ctx, "user-1", track, 0)
|
||||||
|
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
sentParams := httpClient.SavedRequest.URL.Query()
|
body, _ := io.ReadAll(httpClient.SavedRequest.Body)
|
||||||
|
sentParams, _ := url.ParseQuery(string(body))
|
||||||
Expect(sentParams.Get("artist")).To(Equal("First Artist"))
|
Expect(sentParams.Get("artist")).To(Equal("First Artist"))
|
||||||
Expect(sentParams.Get("albumArtist")).To(Equal("First Album Artist"))
|
Expect(sentParams.Get("albumArtist")).To(Equal("First Album Artist"))
|
||||||
})
|
})
|
||||||
@@ -309,7 +312,8 @@ var _ = Describe("lastfmAgent", func() {
|
|||||||
|
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
Expect(httpClient.SavedRequest.Method).To(Equal(http.MethodPost))
|
Expect(httpClient.SavedRequest.Method).To(Equal(http.MethodPost))
|
||||||
sentParams := httpClient.SavedRequest.URL.Query()
|
body, _ := io.ReadAll(httpClient.SavedRequest.Body)
|
||||||
|
sentParams, _ := url.ParseQuery(string(body))
|
||||||
Expect(sentParams.Get("method")).To(Equal("track.scrobble"))
|
Expect(sentParams.Get("method")).To(Equal("track.scrobble"))
|
||||||
Expect(sentParams.Get("sk")).To(Equal("SK-1"))
|
Expect(sentParams.Get("sk")).To(Equal("SK-1"))
|
||||||
Expect(sentParams.Get("track")).To(Equal(track.Title))
|
Expect(sentParams.Get("track")).To(Equal(track.Title))
|
||||||
@@ -334,7 +338,8 @@ var _ = Describe("lastfmAgent", func() {
|
|||||||
err := agent.Scrobble(ctx, "user-1", scrobbler.Scrobble{MediaFile: *track, TimeStamp: ts})
|
err := agent.Scrobble(ctx, "user-1", scrobbler.Scrobble{MediaFile: *track, TimeStamp: ts})
|
||||||
|
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
sentParams := httpClient.SavedRequest.URL.Query()
|
body, _ := io.ReadAll(httpClient.SavedRequest.Body)
|
||||||
|
sentParams, _ := url.ParseQuery(string(body))
|
||||||
Expect(sentParams.Get("artist")).To(Equal("First Artist"))
|
Expect(sentParams.Get("artist")).To(Equal("First Artist"))
|
||||||
Expect(sentParams.Get("albumArtist")).To(Equal("First Album Artist"))
|
Expect(sentParams.Get("albumArtist")).To(Equal("First Album Artist"))
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -198,8 +198,15 @@ func (c *client) makeRequest(ctx context.Context, method string, params url.Valu
|
|||||||
c.sign(params)
|
c.sign(params)
|
||||||
}
|
}
|
||||||
|
|
||||||
req, _ := http.NewRequestWithContext(ctx, method, apiBaseUrl, nil)
|
var req *http.Request
|
||||||
|
if method == http.MethodPost {
|
||||||
|
body := strings.NewReader(params.Encode())
|
||||||
|
req, _ = http.NewRequestWithContext(ctx, method, apiBaseUrl, body)
|
||||||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
} else {
|
||||||
|
req, _ = http.NewRequestWithContext(ctx, method, apiBaseUrl, nil)
|
||||||
req.URL.RawQuery = params.Encode()
|
req.URL.RawQuery = params.Encode()
|
||||||
|
}
|
||||||
|
|
||||||
log.Trace(ctx, fmt.Sprintf("Sending Last.fm %s request", req.Method), "url", req.URL)
|
log.Trace(ctx, fmt.Sprintf("Sending Last.fm %s request", req.Method), "url", req.URL)
|
||||||
resp, err := c.hc.Do(req)
|
resp, err := c.hc.Do(req)
|
||||||
|
|||||||
@@ -178,6 +178,74 @@ var _ = Describe("client", func() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Describe("scrobble", func() {
|
||||||
|
It("sends parameters in request body for POST", func() {
|
||||||
|
httpClient.Res = http.Response{
|
||||||
|
Body: io.NopCloser(bytes.NewBufferString(`{"scrobbles":{"scrobble":{"ignoredMessage":{"code":"0"}},"@attr":{"accepted":1}}}`)),
|
||||||
|
StatusCode: 200,
|
||||||
|
}
|
||||||
|
|
||||||
|
info := ScrobbleInfo{
|
||||||
|
artist: "U2",
|
||||||
|
track: "One",
|
||||||
|
album: "Achtung Baby",
|
||||||
|
trackNumber: 1,
|
||||||
|
duration: 276,
|
||||||
|
albumArtist: "U2",
|
||||||
|
}
|
||||||
|
err := client.scrobble(context.Background(), "SESSION_KEY", info)
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
|
||||||
|
req := httpClient.SavedRequest
|
||||||
|
Expect(req.Method).To(Equal(http.MethodPost))
|
||||||
|
Expect(req.Header.Get("Content-Type")).To(Equal("application/x-www-form-urlencoded"))
|
||||||
|
Expect(req.URL.RawQuery).To(BeEmpty())
|
||||||
|
|
||||||
|
body, _ := io.ReadAll(req.Body)
|
||||||
|
bodyParams, _ := url.ParseQuery(string(body))
|
||||||
|
Expect(bodyParams.Get("method")).To(Equal("track.scrobble"))
|
||||||
|
Expect(bodyParams.Get("artist")).To(Equal("U2"))
|
||||||
|
Expect(bodyParams.Get("track")).To(Equal("One"))
|
||||||
|
Expect(bodyParams.Get("sk")).To(Equal("SESSION_KEY"))
|
||||||
|
Expect(bodyParams.Get("api_key")).To(Equal("API_KEY"))
|
||||||
|
Expect(bodyParams.Get("api_sig")).ToNot(BeEmpty())
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Describe("updateNowPlaying", func() {
|
||||||
|
It("sends parameters in request body for POST", func() {
|
||||||
|
httpClient.Res = http.Response{
|
||||||
|
Body: io.NopCloser(bytes.NewBufferString(`{"nowplaying":{"ignoredMessage":{"code":"0"}}}`)),
|
||||||
|
StatusCode: 200,
|
||||||
|
}
|
||||||
|
|
||||||
|
info := ScrobbleInfo{
|
||||||
|
artist: "U2",
|
||||||
|
track: "One",
|
||||||
|
album: "Achtung Baby",
|
||||||
|
trackNumber: 1,
|
||||||
|
duration: 276,
|
||||||
|
albumArtist: "U2",
|
||||||
|
}
|
||||||
|
err := client.updateNowPlaying(context.Background(), "SESSION_KEY", info)
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
|
||||||
|
req := httpClient.SavedRequest
|
||||||
|
Expect(req.Method).To(Equal(http.MethodPost))
|
||||||
|
Expect(req.Header.Get("Content-Type")).To(Equal("application/x-www-form-urlencoded"))
|
||||||
|
Expect(req.URL.RawQuery).To(BeEmpty())
|
||||||
|
|
||||||
|
body, _ := io.ReadAll(req.Body)
|
||||||
|
bodyParams, _ := url.ParseQuery(string(body))
|
||||||
|
Expect(bodyParams.Get("method")).To(Equal("track.updateNowPlaying"))
|
||||||
|
Expect(bodyParams.Get("artist")).To(Equal("U2"))
|
||||||
|
Expect(bodyParams.Get("track")).To(Equal("One"))
|
||||||
|
Expect(bodyParams.Get("sk")).To(Equal("SESSION_KEY"))
|
||||||
|
Expect(bodyParams.Get("api_key")).To(Equal("API_KEY"))
|
||||||
|
Expect(bodyParams.Get("api_sig")).ToNot(BeEmpty())
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
Describe("sign", func() {
|
Describe("sign", func() {
|
||||||
It("adds an api_sig param with the signature", func() {
|
It("adds an api_sig param with the signature", func() {
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
|
|||||||
Reference in New Issue
Block a user