oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package webread
2
3import (
4 "fmt"
5 "strings"
6 "testing"
7
8 "amadan.net/oficina/equipo/internal/websource"
9)
10
11func mustURL(t testing.TB, raw string) websource.URL {
12 t.Helper()
13 u, err := websource.Parse(raw)
14 if err != nil {
15 t.Fatalf("Parse(%q): %v", raw, err)
16 }
17 return u
18}
19
20const shopPage = `<!DOCTYPE html>
21<html><head>
22<title>The Shop &amp; Co</title>
23<meta name="description" content="Cuts and shaves since 1990">
24<meta name="theme-color" content="#1a2b3c">
25<meta property="og:title" content="The Shop">
26<meta property="og:image" content="/og.png">
27<link rel="stylesheet" href="/a.css">
28<link rel="icon" href="/favicon.png">
29<link rel="apple-touch-icon" href="https://cdn.example.net/touch.png">
30<style>body { color: #112233 }</style>
31<script>var x = "<a href='/script-link'>no</a>";</script>
32</head>
33<body>
34<header><img src="/img/Logo.png" alt="Shop"><img src="/hero.jpg" alt="hero"></header>
35<h1>Welcome</h1>
36<p>Open Monday to
37Saturday.</p>
38<!-- <a href="/comment-link">hidden</a> -->
39<a href="/about">About</a> <a href="services?x=1#top">Services</a>
40<a href="https://other.example.org/">Elsewhere</a>
41<a href="javascript:alert(1)">bad</a>
42<a href="http://127.0.0.1/">loop</a>
43<img src=/brand.svg class="site-logo">
44</body></html>`
45
46func TestExtractHTML(t *testing.T) {
47 base := mustURL(t, "https://shop.example.com/")
48 p := extractHTML([]byte(shopPage), base)
49 if p.title != "The Shop & Co" {
50 t.Errorf("title %q", p.title)
51 }
52 metas := map[string]string{}
53 for _, m := range p.metas {
54 metas[m.name] = m.content
55 }
56 if metas["description"] != "Cuts and shaves since 1990" || metas["theme-color"] != "#1a2b3c" || metas["og:title"] != "The Shop" || metas["og:image"] != "/og.png" {
57 t.Errorf("metas %v", metas)
58 }
59 if !strings.Contains(p.text, "Welcome") || !strings.Contains(p.text, "Open Monday to Saturday.") {
60 t.Errorf("text %q", p.text)
61 }
62 if strings.Contains(p.text, "var x") || strings.Contains(p.text, "hidden") {
63 t.Errorf("script or comment in text %q", p.text)
64 }
65 if !strings.Contains(p.styles, "#112233") {
66 t.Errorf("styles %q", p.styles)
67 }
68 var got []string
69 for _, r := range p.refs {
70 s := kindName[r.kind] + " "
71 if r.err != nil {
72 s += "!" + websource.Reason(r.err)
73 } else {
74 s += r.url.String()
75 }
76 if r.logo {
77 s += " logo"
78 }
79 got = append(got, s)
80 }
81 want := []string{
82 "og https://shop.example.com/og.png",
83 "stylesheet https://shop.example.com/a.css",
84 "icon https://shop.example.com/favicon.png",
85 "icon https://cdn.example.net/touch.png",
86 "img https://shop.example.com/img/Logo.png logo",
87 "img https://shop.example.com/hero.jpg",
88 "link https://shop.example.com/about",
89 "link https://shop.example.com/services?x=1",
90 "link https://other.example.org/",
91 "link !ip_literal",
92 "img https://shop.example.com/brand.svg logo",
93 }
94 if strings.Join(got, "\n") != strings.Join(want, "\n") {
95 t.Errorf("refs:\n%s\nwant:\n%s", strings.Join(got, "\n"), strings.Join(want, "\n"))
96 }
97}
98
99func TestExtractBaseHref(t *testing.T) {
100 p := extractHTML([]byte(`<base href="https://shop.example.com/sub/"><a href="page">x</a>`), mustURL(t, "https://shop.example.com/"))
101 if len(p.refs) != 1 || p.refs[0].url.String() != "https://shop.example.com/sub/page" {
102 t.Fatalf("refs %+v", p.refs)
103 }
104 // A base that fails the contract is ignored.
105 p = extractHTML([]byte(`<base href="http://127.0.0.1/"><a href="page">x</a>`), mustURL(t, "https://shop.example.com/"))
106 if len(p.refs) != 1 || p.refs[0].url.String() != "https://shop.example.com/page" {
107 t.Fatalf("refs %+v", p.refs)
108 }
109}
110
111func TestExtractBounds(t *testing.T) {
112 var b strings.Builder
113 for i := range MaxLinks + 50 {
114 fmt.Fprintf(&b, `<a href="/p%d">p</a>`, i)
115 }
116 b.WriteString(strings.Repeat("word ", MaxPageText))
117 p := extractHTML([]byte(b.String()), mustURL(t, "https://shop.example.com/"))
118 if len(p.refs) != MaxLinks {
119 t.Errorf("%d refs, want %d", len(p.refs), MaxLinks)
120 }
121 if len(p.text) > MaxPageText {
122 t.Errorf("text %d bytes", len(p.text))
123 }
124 // Unterminated constructs end the scan without a panic.
125 for _, s := range []string{"<", "<a", `<a href="x`, "<!--", "<script>", "<title>t", "</", "<!doctype", "<a href=x"} {
126 extractHTML([]byte(s), mustURL(t, "https://shop.example.com/"))
127 }
128}
129
130func TestExpansionSelection(t *testing.T) {
131 var b strings.Builder
132 for i := range 8 {
133 fmt.Fprintf(&b, `<link rel="stylesheet" href="/s%d.css">`, i)
134 }
135 for i := range 5 {
136 fmt.Fprintf(&b, `<img src="/logo%d.png">`, i)
137 }
138 b.WriteString(`<img src="/photo.png"><a href="/">home</a><a href="https://elsewhere.example.org/x">x</a>`)
139 for i := range 8 {
140 fmt.Fprintf(&b, `<a href="/page%d">p</a>`, i)
141 }
142 b.WriteString(`<link rel="shortcut icon" href="data:image/png;base64,AA"><link rel="icon" href="https://shop.example.com:8080/i.png">`)
143 page := mustURL(t, "https://shop.example.com/")
144 got := expansions(extractHTML([]byte(b.String()), page), page)
145 var lines []string
146 for _, c := range got {
147 if c.err != nil {
148 lines = append(lines, "!"+websource.Reason(c.err))
149 } else {
150 lines = append(lines, c.url.EscapedPath)
151 }
152 }
153 want := "/s0.css /s1.css /s2.css /s3.css /s4.css /logo0.png /logo1.png /logo2.png /page0 /page1 /page2 /page3 /page4 !port_refused"
154 if strings.Join(lines, " ") != want {
155 t.Fatalf("expansion\n got %s\nwant %s", strings.Join(lines, " "), want)
156 }
157}
158