diff --git a/adapters/lastfm/agent_test.go b/adapters/lastfm/agent_test.go index f2db44fa..185bfbb2 100644 --- a/adapters/lastfm/agent_test.go +++ b/adapters/lastfm/agent_test.go @@ -6,6 +6,7 @@ import ( "errors" "io" "net/http" + "net/url" "os" "strconv" "time" @@ -265,7 +266,8 @@ var _ = Describe("lastfmAgent", func() { Expect(err).ToNot(HaveOccurred()) 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("sk")).To(Equal("SK-1")) Expect(sentParams.Get("track")).To(Equal(track.Title)) @@ -293,7 +295,8 @@ var _ = Describe("lastfmAgent", func() { err := agent.NowPlaying(ctx, "user-1", track, 0) 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("albumArtist")).To(Equal("First Album Artist")) }) @@ -309,7 +312,8 @@ var _ = Describe("lastfmAgent", func() { Expect(err).ToNot(HaveOccurred()) 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("sk")).To(Equal("SK-1")) 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}) 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("albumArtist")).To(Equal("First Album Artist")) }) diff --git a/adapters/lastfm/client.go b/adapters/lastfm/client.go index f15613a1..9d991e79 100644 --- a/adapters/lastfm/client.go +++ b/adapters/lastfm/client.go @@ -198,8 +198,15 @@ func (c *client) makeRequest(ctx context.Context, method string, params url.Valu c.sign(params) } - req, _ := http.NewRequestWithContext(ctx, method, apiBaseUrl, nil) - req.URL.RawQuery = params.Encode() + 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() + } log.Trace(ctx, fmt.Sprintf("Sending Last.fm %s request", req.Method), "url", req.URL) resp, err := c.hc.Do(req) diff --git a/adapters/lastfm/client_test.go b/adapters/lastfm/client_test.go index bd319747..c218721a 100644 --- a/adapters/lastfm/client_test.go +++ b/adapters/lastfm/client_test.go @@ -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() { It("adds an api_sig param with the signature", func() { params := url.Values{}