diff --git a/server/public/handle_streams_test.go b/server/public/handle_streams_test.go index f43d75a26..54d492f08 100644 --- a/server/public/handle_streams_test.go +++ b/server/public/handle_streams_test.go @@ -192,3 +192,52 @@ var _ = Describe("handleStream", func() { Expect(w.Code).To(Equal(http.StatusBadRequest)) }) }) + +var _ = Describe("sanitizeName", func() { + It("leaves a plain string unchanged", func() { + Expect(sanitizeName("Artist Name")).To(Equal("Artist Name")) + }) + + It("replaces a single slash with an underscore", func() { + Expect(sanitizeName("AC/DC")).To(Equal("AC_DC")) + }) + + It("replaces multiple slashes with underscores", func() { + Expect(sanitizeName("a/b/c")).To(Equal("a_b_c")) + }) +}) + +var _ = Describe("downloadFilename", func() { + var mf *model.MediaFile + + BeforeEach(func() { + mf = &model.MediaFile{ + Artist: "The Beatles", + Title: "Hey Jude", + Suffix: "flac", + UpdatedAt: time.Now(), + } + }) + + It("uses the media file suffix when format is empty", func() { + Expect(downloadFilename(mf, "")).To(Equal("The Beatles - Hey Jude.flac")) + }) + + It("uses the media file suffix when format is 'raw'", func() { + Expect(downloadFilename(mf, "raw")).To(Equal("The Beatles - Hey Jude.flac")) + }) + + It("uses the format as the extension when a real format is given", func() { + Expect(downloadFilename(mf, "mp3")).To(Equal("The Beatles - Hey Jude.mp3")) + }) + + It("sanitizes slashes in the artist name", func() { + mf.Artist = "AC/DC" + Expect(downloadFilename(mf, "")).To(Equal("AC_DC - Hey Jude.flac")) + }) + + It("sanitizes slashes in the title", func() { + mf.Title = "Love/Hate" + Expect(downloadFilename(mf, "")).To(Equal("The Beatles - Love_Hate.flac")) + }) +}) diff --git a/server/serve_index_test.go b/server/serve_index_test.go index 78f3873b8..6f0bf3ee3 100644 --- a/server/serve_index_test.go +++ b/server/serve_index_test.go @@ -312,6 +312,13 @@ var _ = Describe("addShareData", func() { addShareData(r, data, shareInfo) Expect(data["ShareDescription"]).To(Equal(shareInfo.Contents)) }) + It("should use shareInfo.Contents as ShareInfo.Description", func() { + addShareData(r, data, shareInfo) + var sd shareData + err := json.Unmarshal([]byte(data["ShareInfo"].(string)), &sd) + Expect(err).NotTo(HaveOccurred()) + Expect(sd.Description).To(Equal(shareInfo.Contents)) + }) }) }) }) diff --git a/ui/src/utils/urls.test.js b/ui/src/utils/urls.test.js index 26bdd1283..5b07cbbc3 100644 --- a/ui/src/utils/urls.test.js +++ b/ui/src/utils/urls.test.js @@ -1,4 +1,4 @@ -import { isLastFmURL } from './urls' +import { isLastFmURL, toDownloadUrl } from './urls' describe('isLastFmURL', () => { it('returns true for valid Last.fm music URLs', () => { @@ -23,3 +23,23 @@ describe('isLastFmURL', () => { expect(isLastFmURL('not-a-url')).toBe(false) }) }) + +describe('toDownloadUrl', () => { + it('appends download=true to an absolute URL', () => { + const result = toDownloadUrl('https://example.com/share/s/abc123') + expect(new URL(result).searchParams.get('download')).toBe('true') + }) + + it('appends download=true to a relative URL resolved against window.origin', () => { + const result = toDownloadUrl('/share/s/abc123') + expect(new URL(result).searchParams.get('download')).toBe('true') + expect(new URL(result).pathname).toBe('/share/s/abc123') + }) + + it('preserves existing query parameters', () => { + const result = toDownloadUrl('https://example.com/share/s/abc123?format=mp3') + const url = new URL(result) + expect(url.searchParams.get('format')).toBe('mp3') + expect(url.searchParams.get('download')).toBe('true') + }) +})