summaryrefslogtreecommitdiff
path: root/internal/fetch/direct_test.go
blob: 86b9e319914360562b0bcb61e471cf77ae7c7884 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package fetch

import (
	"context"
	"fmt"
	"net/http"
	"net/http/httptest"
	"os"
	"path/filepath"
	"strings"
	"testing"
)

const sampleRSS = `<?xml version="1.0"?>
<rss version="2.0">
  <channel>
    <title>Lectures on Physics</title>
    <item>
      <title>Three - The Great Conservation Principles</title>
      <enclosure url="%s/three.mp3" type="audio/mpeg" length="10"/>
    </item>
    <item>
      <title>Two - The Relation of Mathematics &amp; Physics</title>
      <enclosure url="%s/two.mp3" type="audio/mpeg" length="10"/>
    </item>
    <item>
      <title>One - The Law of Gravitation</title>
      <enclosure url="%s/one.mp3" type="audio/mpeg" length="10"/>
    </item>
  </channel>
</rss>`

func rssServer(t *testing.T) *httptest.Server {
	t.Helper()
	var srv *httptest.Server
	srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if strings.HasSuffix(r.URL.Path, ".mp3") {
			fmt.Fprintf(w, "audio for %s", r.URL.Path)
			return
		}
		w.Header().Set("Content-Type", "application/rss+xml")
		fmt.Fprintf(w, sampleRSS, srv.URL, srv.URL, srv.URL)
	}))
	t.Cleanup(srv.Close)
	return srv
}

func TestDiscoverDirectAudioURL(t *testing.T) {
	sources, _, err := Discover(context.Background(), "https://example.org/talks/01%20-%20Intro.mp3", nil)
	if err != nil {
		t.Fatal(err)
	}
	if len(sources) != 1 {
		t.Fatalf("got %d sources", len(sources))
	}
	if sources[0].Name != "01 - Intro.mp3" {
		t.Errorf("name %q — the URL escape should be decoded", sources[0].Name)
	}
}

func TestDiscoverRSSEnclosures(t *testing.T) {
	srv := rssServer(t)
	sources, title, err := Discover(context.Background(), srv.URL+"/feed.xml", nil)
	if err != nil {
		t.Fatal(err)
	}
	if title != "Lectures on Physics" {
		t.Errorf("title %q", title)
	}
	if len(sources) != 3 {
		t.Fatalf("got %d sources", len(sources))
	}
}

// RSS is newest-first by convention, so the last item is episode 1. Numbering
// has to run in reverse or a mirrored course plays backwards.
func TestDiscoverNumbersRSSInListeningOrder(t *testing.T) {
	srv := rssServer(t)
	sources, _, err := Discover(context.Background(), srv.URL+"/feed.xml", nil)
	if err != nil {
		t.Fatal(err)
	}

	byTrack := map[int]string{}
	for _, s := range sources {
		byTrack[s.Track] = s.Name
	}
	if !strings.Contains(byTrack[1], "One - The Law of Gravitation") {
		t.Errorf("track 1 is %q, want the feed's last item", byTrack[1])
	}
	if !strings.Contains(byTrack[3], "Three - The Great Conservation") {
		t.Errorf("track 3 is %q, want the feed's first item", byTrack[3])
	}
	if !strings.HasPrefix(byTrack[1], "01 - ") {
		t.Errorf("track 1 name %q is not zero-padded", byTrack[1])
	}
}

func TestDiscoverRejectsNonFeed(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "<html><body>not a feed</body></html>")
	}))
	defer srv.Close()

	if _, _, err := Discover(context.Background(), srv.URL, nil); err == nil {
		t.Error("expected an error for a page that is not a feed")
	}
}

func TestDiscoverRejectsBadURL(t *testing.T) {
	for _, u := range []string{"", "not-a-url", "/just/a/path"} {
		if _, _, err := Discover(context.Background(), u, nil); err == nil {
			t.Errorf("%q was accepted", u)
		}
	}
}

func TestDownloadWritesFiles(t *testing.T) {
	srv := rssServer(t)
	dir := t.TempDir()

	sources, _, err := Discover(context.Background(), srv.URL+"/feed.xml", nil)
	if err != nil {
		t.Fatal(err)
	}
	if err := Download(context.Background(), sources, dir, nil, nil); err != nil {
		t.Fatal(err)
	}

	entries, _ := os.ReadDir(dir)
	if len(entries) != 3 {
		t.Fatalf("got %d files", len(entries))
	}
	for _, e := range entries {
		if strings.HasSuffix(e.Name(), ".part") {
			t.Errorf("partial file %q left behind", e.Name())
		}
	}
}

// Re-running must not re-download; this is what makes a long series resumable.
func TestDownloadSkipsExisting(t *testing.T) {
	var hits int
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		hits++
		fmt.Fprint(w, "audio")
	}))
	defer srv.Close()

	dir := t.TempDir()
	sources := []Source{{URL: srv.URL + "/a.mp3", Name: "01 - A.mp3", Track: 1}}

	for i := 0; i < 3; i++ {
		if err := Download(context.Background(), sources, dir, nil, nil); err != nil {
			t.Fatal(err)
		}
	}
	if hits != 1 {
		t.Errorf("downloaded %d times, want 1", hits)
	}
}

// A failed download must not leave something the feed generator would treat as
// a complete episode.
func TestDownloadLeavesNothingOnFailure(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		http.Error(w, "gone", http.StatusNotFound)
	}))
	defer srv.Close()

	dir := t.TempDir()
	err := Download(context.Background(), []Source{{URL: srv.URL + "/a.mp3", Name: "01 - A.mp3"}}, dir, nil, nil)
	if err == nil {
		t.Fatal("expected an error")
	}
	if entries, _ := os.ReadDir(dir); len(entries) != 0 {
		t.Errorf("left %d file(s) behind after a failed download", len(entries))
	}
}

func TestSanitiseRemovesPathCharacters(t *testing.T) {
	cases := []struct{ in, want string }{
		{"A/B: The Sequel", "AB The Sequel"},
		{"What? Why!", "What Why!"},
		{"  spaced   out  ", "spaced out"},
		// Separators are dropped and the leading dots trimmed, so a name that
		// tried to traverse comes out as an ordinary filename.
		{"../../etc/passwd", "etcpasswd"},
		{"", "episode"},
		{"...", "episode"},
	}
	for _, c := range cases {
		if got := sanitise(c.in); got != c.want {
			t.Errorf("sanitise(%q) = %q, want %q", c.in, got, c.want)
		}
	}
}

// A sanitised name must never be able to escape the download directory.
func TestSanitisedNamesStayInDirectory(t *testing.T) {
	dir := t.TempDir()
	for _, in := range []string{"../escape", "../../escape", "/absolute", "a/b/c"} {
		joined := filepath.Join(dir, sanitise(in)+".mp3")
		if !strings.HasPrefix(joined, dir+string(filepath.Separator)) {
			t.Errorf("sanitise(%q) produced a path outside the directory: %s", in, joined)
		}
	}
}

func TestExtOfPrefersURLThenMIME(t *testing.T) {
	sources, _, err := Discover(context.Background(), "https://example.org/a/file.opus", nil)
	if err != nil {
		t.Fatal(err)
	}
	if filepath.Ext(sources[0].Name) != ".opus" {
		t.Errorf("name %q", sources[0].Name)
	}
}

// An enclosure URL with no useful extension has to fall back to the declared
// MIME type, or the file lands as .mp3 and no player reads its tags.
func TestExtOfFallsBackToMIMEType(t *testing.T) {
	var srv *httptest.Server
	srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if strings.HasPrefix(r.URL.Path, "/stream") {
			fmt.Fprint(w, "audio")
			return
		}
		fmt.Fprintf(w, `<?xml version="1.0"?><rss version="2.0"><channel><title>T</title>
		  <item><title>Only</title><enclosure url="%s/stream?id=7" type="audio/mp4"/></item>
		</channel></rss>`, srv.URL)
	}))
	defer srv.Close()

	sources, _, err := Discover(context.Background(), srv.URL+"/feed.xml", nil)
	if err != nil {
		t.Fatal(err)
	}
	if filepath.Ext(sources[0].Name) != ".m4a" {
		t.Errorf("name %q, want a .m4a extension from the audio/mp4 type", sources[0].Name)
	}
}