oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package websource
2
3import (
4 "strings"
5 "testing"
6)
7
8func readableBase(t *testing.T) URL {
9 t.Helper()
10 u, err := Parse("https://news.example.org/today")
11 if err != nil {
12 t.Fatal(err)
13 }
14 return u
15}
16
17// A page's scripts and styles are never its text; its title is.
18func TestReadableDropsScript(t *testing.T) {
19 page := `<html><head><title>Dublin weather</title><style>body{color:red}</style>
20<script>alert("steal")</script></head><body><h1>Today</h1><p>Rain, 11&deg;C.</p>
21<script type="text/javascript">document.write("injected")</script><noscript>no js</noscript></body></html>`
22 title, text := Readable([]byte(page), "html", readableBase(t))
23 if title != "Dublin weather" {
24 t.Errorf("title %q", title)
25 }
26 for _, bad := range []string{"alert", "steal", "injected", "color:red", "no js"} {
27 if strings.Contains(text, bad) {
28 t.Errorf("text holds %q: %q", bad, text)
29 }
30 }
31 if !strings.Contains(text, "Today") || !strings.Contains(text, "Rain, 11°C.") {
32 t.Errorf("text %q", text)
33 }
34}
35
36// A feed gives its items' titles and summaries; JSON is pretty-printed;
37// XML gives its text; plain text is itself; anything else gives nothing.
38func TestReadableFeedItems(t *testing.T) {
39 rss := `<?xml version="1.0"?><rss><channel><title>Met news</title>
40<item><title>Storm warning</title><description>Winds up to &lt;b&gt;90 km/h&lt;/b&gt;.</description><link>https://x.example.org/1</link></item>
41<item><title>Calm weekend</title><description>Sun on Saturday.</description></item></channel></rss>`
42 title, text := Readable([]byte(rss), "feed", readableBase(t))
43 if title != "Met news" || !strings.Contains(text, "Storm warning\nWinds up to 90 km/h.") || !strings.Contains(text, "Calm weekend\nSun on Saturday.") || strings.Contains(text, "<b>") {
44 t.Errorf("rss: %q %q", title, text)
45 }
46 atom := `<feed xmlns="http://www.w3.org/2005/Atom"><title>Club</title><entry><title>Open late</title><summary>Until 22:00 on Fridays.</summary></entry></feed>`
47 if title, text := Readable([]byte(atom), "feed", readableBase(t)); title != "Club" || !strings.Contains(text, "Open late\nUntil 22:00 on Fridays.") {
48 t.Errorf("atom: %q %q", title, text)
49 }
50 if _, text := Readable([]byte(`{"temp":11,"sky":"rain"}`), "json", readableBase(t)); text != "{\n \"sky\": \"rain\",\n \"temp\": 11\n}" && text != "{\n \"temp\": 11,\n \"sky\": \"rain\"\n}" {
51 t.Errorf("json: %q", text)
52 }
53 if _, text := Readable([]byte(`<forecast><day>Monday</day><sky>Rain</sky></forecast>`), "xml", readableBase(t)); text != "Monday\nRain" {
54 t.Errorf("xml: %q", text)
55 }
56 if _, text := Readable([]byte("Open 9 to 5."), "text", readableBase(t)); text != "Open 9 to 5." {
57 t.Errorf("text: %q", text)
58 }
59 if title, text := Readable([]byte{0x89, 'P', 'N', 'G'}, "unsupported", readableBase(t)); title != "" || text != "" {
60 t.Errorf("unsupported: %q %q", title, text)
61 }
62 if _, text := Readable([]byte(strings.Repeat("a", 2*MaxReadable)), "text", readableBase(t)); len(text) != MaxReadable {
63 t.Errorf("text not cut: %d", len(text))
64 }
65}
66