Repository navigation
Expand file tree
/
Copy pathheader_test.go
More file actions
154 lines (148 loc) · 5.85 KB
/
Copy pathheader_test.go
File metadata and controls
154 lines (148 loc) · 5.85 KB
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
package main
import (
"bytes"
"encoding/json"
"image/png"
"net/http"
"net/http/httptest"
"regexp"
"strings"
"testing"
)
// Every surface renders the same SiteHeader with one current section.
func TestOneHeaderEverywhere(t *testing.T) {
h := handler()
current := regexp.MustCompile(`<nav class="site-nav"[\s\S]*?</nav>`)
for _, tc := range []struct{ path, section string }{
{"/", ""},
{"/docs", `<a href="/docs" aria-current="page">Docs</a>`},
{"/docs/install", `<a href="/docs" aria-current="page">Docs</a>`},
{"/catalog", `<a href="/catalog" aria-current="page">Components</a>`},
{"/catalog/button", `<a href="/catalog" aria-current="page">Components</a>`},
{"/examples", `<summary data-current="true">`},
{"/dashboard", `<summary data-current="true">`},
{"/playground", `<a href="/playground" aria-current="page">Playground</a>`},
} {
w := httptest.NewRecorder()
h.ServeHTTP(w, httptest.NewRequest("GET", tc.path, nil))
body := w.Body.String()
if w.Code != 200 {
t.Fatalf("%s: %d", tc.path, w.Code)
}
if strings.Count(body, `<header class="site-header">`) != 1 || strings.Contains(body, `class="app-header"`) || strings.Contains(body, `class="masthead"`) {
t.Errorf("%s: expected exactly one shared header", tc.path)
}
for _, label := range []string{">Docs</a>", ">Components</a>", "Examples", ">Playground</a>", `aria-label="TendUI home"`, `class="site-version"`, headerStylesheetURL} {
if !strings.Contains(body, label) {
t.Errorf("%s: header missing %s", tc.path, label)
}
}
nav := current.FindString(body)
if tc.section == "" {
if strings.Contains(nav, "aria-current") || strings.Contains(nav, "data-current") {
t.Errorf("%s: home marks no section", tc.path)
}
} else if !strings.Contains(nav, tc.section) {
t.Errorf("%s: wrong current section", tc.path)
}
if strings.Contains(body, "<script") {
t.Errorf("%s: unexpected script", tc.path)
}
}
}
func TestThemeIsSharedAndDefaultsDark(t *testing.T) {
h := handler()
for _, path := range []string{"/", "/docs", "/catalog", "/playground"} {
for _, tc := range []struct{ cookie, want string }{{"", `data-theme="dark"`}, {"light", `data-theme="light"`}, {"dark", `data-theme="dark"`}} {
r := httptest.NewRequest("GET", path, nil)
if tc.cookie != "" {
r.AddCookie(&http.Cookie{Name: "tendui_theme", Value: tc.cookie})
}
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
if !strings.Contains(w.Body.String(), tc.want) {
t.Errorf("%s with %q: want %s", path, tc.cookie, tc.want)
}
}
}
r := httptest.NewRequest("GET", "/docs", nil)
r.AddCookie(&http.Cookie{Name: "tendui_theme", Value: "system"})
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
if strings.Contains(w.Body.String(), "data-theme=") || w.Header().Get("Vary") != "Cookie" {
t.Fatal("system theme follows the browser and responses vary by cookie")
}
}
func TestThemeToggleReturnsToHostPages(t *testing.T) {
h := handler()
for _, path := range []string{"/", "/docs/install", "/catalog"} {
r := httptest.NewRequest("POST", "https://tendui.com/api/theme/toggle", nil)
r.Host = "tendui.com"
r.Header.Set("Referer", "https://tendui.com"+path)
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
if w.Code != http.StatusSeeOther || w.Header().Get("Location") != path {
t.Errorf("%s: got %d %q", path, w.Code, w.Header().Get("Location"))
}
if cookies := w.Result().Cookies(); len(cookies) != 1 || cookies[0].Value != "light" || !cookies[0].Secure {
t.Errorf("%s: first toggle should save a secure light cookie", path)
}
}
}
func TestDocsGuidesMoveIntoCompactMenu(t *testing.T) {
w := httptest.NewRecorder()
handler().ServeHTTP(w, httptest.NewRequest("GET", "/docs/install", nil))
body := w.Body.String()
section := regexp.MustCompile(`<div class="site-sheet-section">[\s\S]*?</div>`).FindString(body)
if !strings.Contains(section, `<a href="/docs/install" aria-current="page">Installation</a>`) || strings.Contains(section, "/catalog") || strings.Contains(section, `"/examples"`) {
t.Fatalf("compact menu should list only guides: %s", section)
}
}
// Navigation must not lose the tab icon on pages hosted by the example handler.
func TestSiteIconsAcrossSurfaces(t *testing.T) {
h := handler()
for _, path := range []string{"/", "/docs", "/examples", "/catalog", "/catalog/button", "/playground", "/forms"} {
w := httptest.NewRecorder()
h.ServeHTTP(w, httptest.NewRequest("GET", path, nil))
body := w.Body.String()
for _, want := range []string{brandURL("favicon.ico"), brandURL("favicon-32.png"), brandURL("favicon.svg"), brandURL("apple-touch-icon.png"), `href="/site.webmanifest"`} {
if strings.Count(body, want) != 1 {
t.Errorf("%s: expected one icon declaration for %s", path, want)
}
}
}
for _, path := range []string{"/favicon.ico", "/apple-touch-icon.png", brandURL("favicon.ico"), brandURL("favicon-32.png")} {
w := httptest.NewRecorder()
h.ServeHTTP(w, httptest.NewRequest("GET", path, nil))
if w.Code != 200 || w.Body.Len() == 0 {
t.Errorf("%s: unavailable icon (%d)", path, w.Code)
}
}
w := httptest.NewRecorder()
h.ServeHTTP(w, httptest.NewRequest("GET", "/site.webmanifest", nil))
var manifest struct {
Name string
Icons []struct{ Src, Sizes string }
}
if err := json.Unmarshal(w.Body.Bytes(), &manifest); err != nil {
t.Fatal(err)
}
if manifest.Name != "Tend UI" || len(manifest.Icons) != 2 {
t.Fatalf("invalid manifest: %+v", manifest)
}
if !strings.Contains(w.Header().Get("Content-Security-Policy"), "manifest-src 'self'") {
t.Fatal("manifest blocked by CSP")
}
for i, icon := range manifest.Icons {
r := httptest.NewRecorder()
h.ServeHTTP(r, httptest.NewRequest("GET", icon.Src, nil))
config, err := png.DecodeConfig(bytes.NewReader(r.Body.Bytes()))
if err != nil {
t.Fatal(err)
}
size := []int{192, 512}[i]
if r.Code != 200 || config.Width != size || config.Height != size {
t.Errorf("invalid app icon: %+v", config)
}
}
}