-
Notifications
You must be signed in to change notification settings - Fork 3
/
linksbuilder_test.go
245 lines (208 loc) · 7.05 KB
/
linksbuilder_test.go
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package andrew_test
import (
"fmt"
"io"
"net/http"
"regexp"
"testing"
"testing/fstest"
"time"
"github.com/google/go-cmp/cmp"
"github.com/playtechnique/andrew"
)
func TestAndrewTableOfContentsIsGeneratedCorrectlyInContentrootDirectory(t *testing.T) {
t.Parallel()
contentRoot := fstest.MapFS{
"index.html": &fstest.MapFile{Data: []byte(`
<!doctype HTML>
<head> </head>
<body>
{{ .AndrewTableOfContents }}
</body>
`)},
"pages/1-2-3.html": &fstest.MapFile{Data: []byte(`
<!doctype HTML>
<head>
<title>1-2-3 Page</title>
</head>
`)},
}
s := newTestAndrewServer(t, contentRoot)
resp, err := http.Get(s.BaseUrl + "/index.html")
if err != nil {
t.Fatal(err)
}
received, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
expected, err := regexp.Compile(".*<a class=\"andrewtableofcontentslink\" id=\"andrewtableofcontentslink0\" href=\"pages/1-2-3.html\">1-2-3 Page</a>.*")
if err != nil {
t.Fatal(err)
}
if expected.FindString(string(received)) == "" {
t.Fatalf("Diff of Expected and Actual: %s", cmp.Diff(expected, received))
}
}
func TestAndrewTableOfContentsIsGeneratedCorrectlyInAChildDirectory(t *testing.T) {
t.Parallel()
contentRoot := fstest.MapFS{
"parentDir/index.html": &fstest.MapFile{Data: []byte(`
<!doctype HTML>
<head> </head>
<body>
{{ .AndrewTableOfContents }}
</body>
`)},
"parentDir/childDir/1-2-3.html": &fstest.MapFile{Data: []byte(`
<!doctype HTML>
<head>
<title>1-2-3 Page</title>
</head>
`)},
}
s := newTestAndrewServer(t, contentRoot)
resp, err := http.Get(s.BaseUrl + "/parentDir/index.html")
if err != nil {
t.Fatal(err)
}
received, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
expected, err := regexp.Compile(".*<a class=\"andrewtableofcontentslink\" id=\"andrewtableofcontentslink0\" href=\"childDir/1-2-3.html\">1-2-3 Page</a>")
if err != nil {
t.Fatal(err)
}
if expected.FindString(string(received)) == "" {
t.Fatalf("Diff of Expected and Actual: %s", cmp.Diff(expected, received))
}
}
// TestArticlesOrderInAndrewTableOfContentsIsOverridable is verifying that
// when a page contains an andrew-publish-time meta element then the list of links andrew
// generates for the {{.AndrewTableOfContents}} are
// sorted by the meta element, then the mtime, not using the ascii sorting order.
// This test requires having several files which are in one order when sorted
// by modtime and in another order by andrew-publish-time time, so that we can tell
// what file attribute andrew is actually sorting on.
func TestArticlesOrderInAndrewTableOfContentsIsOverridable(t *testing.T) {
expected, err := regexp.Compile("(?s).*b_newest.html.*c_newer.html.*a_older.html.*")
if err != nil {
t.Fatal(err)
}
now := time.Now().UTC()
newer := now.Add(24 * time.Hour)
newest := now.Add(48 * time.Hour)
contentRoot := fstest.MapFS{
"index.html": &fstest.MapFile{Data: []byte(`
{{ .AndrewTableOfContents }}
`)},
"a_older.html": &fstest.MapFile{ModTime: now},
"b_newest.html": &fstest.MapFile{ModTime: newest, Data: []byte(fmt.Sprintf(`<meta name="andrew-publish-time" content="%s">`, newest.Format("2006-01-02")))},
"c_newer.html": &fstest.MapFile{ModTime: newer},
}
server := andrew.Server{SiteFiles: contentRoot}
page, err := andrew.NewPage(server, "index.html")
if err != nil {
t.Fatal(err)
}
received := page.Content
if expected.FindString(received) == "" {
t.Error(cmp.Diff(expected, received))
}
}
// TestInvalidMetaContentDoesNotCrashTheWebServer checks that if there's
// garbage data inside a meta element named andrew-publish-at that we do
// something sensible rather than crashing the web server and emitting a 502.
func TestInvalidAndrewPublishTimeContentDoesNotCrashTheWebServer(t *testing.T) {
t.Parallel()
contentRoot := fstest.MapFS{
"index.html": &fstest.MapFile{Data: []byte(`
<!doctype HTML>
<head> </head>
<body>
{{ .AndrewTableOfContents }}
</body>
`)},
"a.html": &fstest.MapFile{Data: []byte(`
<!doctype HTML>
<head>
<meta name="andrew-publish-time" content="<no value>"
</head>
`)},
}
s := newTestAndrewServer(t, contentRoot)
resp, err := http.Get(s.BaseUrl)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
t.Errorf("Expected http 200, received %d", resp.StatusCode)
}
}
// TestArticlesOrderInAndrewTableOfContentsIsOverridable is verifying that
// when a page contains an andrew-publish-time meta element then the list of links andrew
// generates for the {{.AndrewTableOfContents}} are
// sorted by the meta element, then the mtime, not using the ascii sorting order.
// This test requires having several files which are in one order when sorted
// by modtime and in another order by andrew-publish-time time, so that we can tell
// what file attribute andrew is actually sorting on.
func TestOneArticleAppearsUnderParentDirectoryForAndrewTableOfContentsWithDirectories(t *testing.T) {
expected := `<div class="AndrewTableOfContentsWithDirectories">
<ul>
<li><a class="andrewtableofcontentslink" id="andrewtableofcontentslink0" href="otherPage.html">otherPage.html</a> - <span class="andrew-page-publish-date">0001-01-01</span></li>
</ul>
</div>
`
contentRoot := fstest.MapFS{
"groupedContents.html": &fstest.MapFile{Data: []byte(`{{.AndrewTableOfContentsWithDirectories}}`)},
"otherPage.html": &fstest.MapFile{},
}
s := newTestAndrewServer(t, contentRoot)
resp, err := http.Get(s.BaseUrl + "/groupedContents.html")
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected status %q", resp.Status)
}
received, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if expected != string(received) {
t.Errorf("Expected:\n" + expected + "\n Received:\n" + string(received))
}
}
func TestFullHTMLReturnedByAndrewTableOfContents(t *testing.T) {
expected := `<div class="AndrewTableOfContents">
<ul>
<li><a class="andrewtableofcontentslink" id="andrewtableofcontentslink0" href="groupedContents.html">groupedContents.html</a> - <span class="andrew-page-publish-date">0001-01-01</span></li>
<li><a class="andrewtableofcontentslink" id="andrewtableofcontentslink1" href="parentDir/childDir/1-2-3.html">1-2-3.html</a> - <span class="andrew-page-publish-date">0001-01-01</span></li>
<li><a class="andrewtableofcontentslink" id="andrewtableofcontentslink2" href="parentDir/displayme.html">displayme.html</a> - <span class="andrew-page-publish-date">0001-01-01</span></li>
</ul>
</div>
`
contentRoot := fstest.MapFS{
"groupedContents.html": &fstest.MapFile{Data: []byte(`{{.AndrewTableOfContents}}`)},
"parentDir/index.html": &fstest.MapFile{},
"parentDir/styles.css": &fstest.MapFile{},
"parentDir/displayme.html": &fstest.MapFile{},
"parentDir/childDir/1-2-3.html": &fstest.MapFile{},
}
s := newTestAndrewServer(t, contentRoot)
resp, err := http.Get(s.BaseUrl + "/groupedContents.html")
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected status %q", resp.Status)
}
received, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if expected != string(received) {
t.Errorf("Expected:\n" + expected + "\n Received:\n" + string(received))
}
}