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

import (
	"fmt"
	"net/http"
	"testing"
)

// An account requested by a page is named after its feed, and described by
// the page; each falls back on the other. A page often names itself after
// its newest post, as apenwarr.ca does, or calls itself "Home".
func TestSiteTitles(t *testing.T) {
	const page = `<html><head>
		<link rel="alternate" type="application/rss+xml" href="/rss">
		<title>thundersnap 0.01: an undo button for everything - apenwarr</title>%s
		</head></html>`
	const pageDesc = `<meta name="description" content="Avery's log">`
	const rss = `<?xml version="1.0"?><rss version="2.0"><channel>
		<title>%s</title><link>https://apenwarr.example</link><description>%s</description>
		</channel></rss>`

	for _, c := range []struct {
		name                   string
		pageMeta               string
		feedTitle, feedDesc    string
		wantTitle, wantSummary string
	}{
		{"feed title, page description", pageDesc, " apenwarr ", "apenwarr - NITLog",
			"apenwarr", "Avery's log"},
		{"no page description: the feed's", "", "apenwarr", "Avery's &lt;b&gt;log&lt;/b&gt;",
			"apenwarr", "Avery's log"},
		{"a feed description that is its title is none", "", "apenwarr", "Apenwarr",
			"apenwarr", ""},
		{"no feed title: the page's", pageDesc, "", "",
			"thundersnap 0.01: an undo button for everything - apenwarr", "Avery's log"},
		{"a feed title in markup", pageDesc, "&lt;span&gt;Songs&lt;/span&gt; on Networks", "",
			"Songs on Networks", "Avery's log"},
	} {
		t.Run(c.name, func(t *testing.T) {
			s := newSim(t, nil)
			feed := newSimFeed(t, s.clock)
			feed.setHandler(func(w http.ResponseWriter, r *http.Request) {
				switch r.URL.Path {
				case "/rss":
					w.Header().Set("Content-Type", "application/rss+xml")
					fmt.Fprintf(w, rss, c.feedTitle, c.feedDesc)
				default:
					w.Header().Set("Content-Type", "text/html")
					fmt.Fprintf(w, page, c.pageMeta)
				}
			})
			si, _, err := s.ff.getSiteInfo(feed.URL + "/")
			if err != nil {
				t.Fatal(err)
			}
			if si.Title != c.wantTitle || si.Description != c.wantSummary {
				t.Errorf("name %q, summary %q; want %q, %q", si.Title, si.Description, c.wantTitle, c.wantSummary)
			}
		})
	}

	// A feed requested as itself is named and described by itself alone.
	s := newSim(t, nil)
	feed := newSimFeed(t, s.clock)
	feed.setHandler(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, rss, "apenwarr", "apenwarr")
	})
	si, _, err := s.ff.getSiteInfo(feed.URL + "/rss")
	if err != nil {
		t.Fatal(err)
	}
	if si.Title != "apenwarr" || si.Description != "" {
		t.Errorf("the feed itself: name %q, summary %q", si.Title, si.Description)
	}
}