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
package logic

import (
	"os"
	"path/filepath"
	"testing"
)

func TestBlockedFeedsUnsetBlocksNothing(t *testing.T) {
	bf, err := newBlockedFeeds("")
	if err != nil {
		t.Fatalf("no blocked_feeds_file: %v, want no error", err)
	}
	if blocked, err := bf.IsBlocked("https://blog.example.org/feed"); blocked || err != nil {
		t.Errorf("IsBlocked = %v, %v; want false, nil", blocked, err)
	}
}

func TestBlockedFeedsMissingFileIsAStartupError(t *testing.T) {
	if _, err := newBlockedFeeds(filepath.Join(t.TempDir(), "nope.txt")); err == nil {
		t.Error("a blocked_feeds_file that does not exist was accepted")
	}
}

func TestBlockedFeedsMatchesWithoutScheme(t *testing.T) {
	file := filepath.Join(t.TempDir(), "blocked.txt")
	if err := os.WriteFile(file, []byte("blog.example.org/feed\n"), 0o644); err != nil {
		t.Fatal(err)
	}
	bf, err := newBlockedFeeds(file)
	if err != nil {
		t.Fatal(err)
	}
	for url, want := range map[string]bool{
		"https://blog.example.org/feed": true,
		"http://BLOG.example.org/feed":  true,
		"https://other.example.org/rss": false,
	} {
		if got, err := bf.IsBlocked(url); got != want || err != nil {
			t.Errorf("IsBlocked(%q) = %v, %v; want %v", url, got, err, want)
		}
	}

	// Removed while running: requests fail, rather than let opted-out
	// sites through.
	if err := os.Remove(file); err != nil {
		t.Fatal(err)
	}
	if _, err := bf.IsBlocked("https://blog.example.org/feed"); err == nil {
		t.Error("a blocked_feeds_file that vanished was read as empty")
	}
}