forked from PlayTechnique/andrew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
andrew_server_test.go
363 lines (290 loc) · 7.96 KB
/
andrew_server_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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package andrew_test
import (
"bytes"
"errors"
"io"
"io/fs"
"net"
"net/http"
"os"
"path/filepath"
"regexp"
"slices"
"testing"
"testing/fstest"
"time"
"github.com/google/go-cmp/cmp"
"github.com/playtechnique/andrew"
)
func TestServerRespondsStatusOKForExistingPage(t *testing.T) {
t.Parallel()
expected := []byte(`
<!DOCTYPE html>
<head>
<title>index title</title>
</head>
<body>
</body>
`)
s := newTestAndrewServer(t, fstest.MapFS{
"index.html": &fstest.MapFile{
Data: expected,
},
})
resp, err := http.Get(s.BaseUrl + "/index.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 !slices.Equal(received, expected) {
t.Fatalf("Expected %q, received %q", expected, received)
}
}
func TestGetForNonExistentPageGeneratesStatusNotFound(t *testing.T) {
t.Parallel()
s := newTestAndrewServer(t, fstest.MapFS{})
resp, err := http.Get(s.BaseUrl + "/page.html")
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusNotFound {
t.Fatalf("Expected a 404 response for a non-existent page, received %d", resp.StatusCode)
}
}
func TestGetForUnreadablePageGeneratesStatusForbidden(t *testing.T) {
t.Parallel()
contentRoot := t.TempDir()
// fstest.MapFS does not enforce file permissions, so we need a real file system in this test.
err := os.WriteFile(contentRoot+"/index.html", []byte{}, 0o222)
if err != nil {
t.Fatal(err)
}
s := newTestAndrewServer(t, os.DirFS(contentRoot))
resp, err := http.Get(s.BaseUrl + "/index.html")
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusForbidden {
t.Fatalf("Expected a 403 response for a page without permission, received %d", resp.StatusCode)
}
}
func Test500ErrorForUnforeseenErrorCase(t *testing.T) {
t.Parallel()
_, status := andrew.CheckPageErrors(errors.New("novel error"))
if status != http.StatusInternalServerError {
t.Errorf("Expected status 500 for unknown error, received %q", status)
}
}
func TestGetSitemapReturnsTheSitemap(t *testing.T) {
t.Parallel()
s := newTestAndrewServer(t, fstest.MapFS{})
resp, err := http.Get(s.BaseUrl + "/sitemap.xml")
if err != nil {
t.Fatal(err)
}
expected := []byte("http://www.sitemaps.org/schemas/sitemap/0.9")
received, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if !bytes.Contains(received, expected) {
t.Fatalf("Expected %q, received %q", expected, received)
}
}
func TestGettingADirectoryDefaultsToIndexHtml(t *testing.T) {
t.Parallel()
expected := []byte(`
<!DOCTYPE html>
<head>
<title>index title</title>
</head>
<body>
</body>
`)
// fstest.MapFS does not create directory-like objects, so we need a real file system in this test.
contentRoot := t.TempDir()
os.MkdirAll(contentRoot+"/pages", 0o755)
err := os.WriteFile(contentRoot+"/pages/index.html", expected, 0o755)
if err != nil {
t.Fatal(err)
}
s := newTestAndrewServer(t, os.DirFS(contentRoot))
resp, err := http.Get(s.BaseUrl + "/pages/")
if err != nil {
t.Fatal(err)
}
received, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if !slices.Equal(received, expected) {
t.Fatalf("Expected %q, received %q", expected, received)
}
}
func TestServerServesRequestedPage(t *testing.T) {
t.Parallel()
contentRoot := fstest.MapFS{
"page.html": &fstest.MapFile{Data: []byte("some text")},
}
s := newTestAndrewServer(t, contentRoot)
t.Logf("Server running on %s\n", s.BaseUrl)
resp, err := http.Get(s.BaseUrl + "/page.html")
if err != nil {
t.Fatal(err)
}
received, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(received, []byte("some text")) {
t.Fatalf("Expected %q, received %q", []byte("some text"), received)
}
}
func TestServerServesIndexPageByDefault(t *testing.T) {
t.Parallel()
expected := []byte(`
<!DOCTYPE html>
<head>
<title>index title</title>
</head>
<body>
</body>
`)
contentRoot := fstest.MapFS{
"index.html": &fstest.MapFile{Data: expected},
}
s := newTestAndrewServer(t, contentRoot)
resp, err := http.Get(s.BaseUrl)
if err != nil {
t.Fatal(err)
}
received, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if !slices.Equal(received, expected) {
t.Fatalf("Expected %q, received %q", expected, received)
}
}
func TestCorrectMimeTypeIsSetForKnownFileTypes(t *testing.T) {
t.Parallel()
expectedMimeTypes := map[string]string{
".css": "text/css; charset=utf-8",
".html": "text/html; charset=utf-8",
".js": "application/javascript; charset=utf-8",
".jpg": "image/jpeg",
".png": "image/png",
".gif": "image/gif",
".webp": "image/webp",
".ico": "image/x-icon",
}
contentRoot := fstest.MapFS{
"page.css": {},
"page.html": {},
"page.js": {},
"page.jpg": {},
"page.png": {},
"page.gif": {},
"page.webp": {},
"page.ico": {},
}
s := newTestAndrewServer(t, contentRoot)
for page := range contentRoot {
resp, err := http.Get(s.BaseUrl + "/" + page)
if err != nil {
t.Fatal(err)
}
// Read the body to prevent resource leaks
_, err = io.ReadAll(resp.Body)
if err != nil {
t.Error(err)
}
expectedMimeType := expectedMimeTypes[filepath.Ext(page)]
contentType := resp.Header.Get("Content-Type")
if contentType != expectedMimeType {
t.Errorf("Incorrect MIME type for %s: got %s, want %s", page, contentType, expectedMimeType)
}
}
}
// TestArticlesInAndrewTableOfContentsAreDefaultSortedByModTime is verifying that
// when the list of links andrew generates for the {{.AndrewTableOfContents}} are
// sorted by mtime, not using the ascii sorting order.
// This test requires having two files which are in one order when sorted
// ascii-betically and in another order by date time, so that we can tell
// what file attribute andrew is actually sorting on.
func TestArticlesInAndrewTableOfContentsAreDefaultSortedByModTime(t *testing.T) {
expectedOrder, err := regexp.Compile(`(?s).*b_newer.*a_older.*`)
if err != nil {
t.Fatal(err)
}
contentRoot := t.TempDir()
// fstest.MapFS does not enforce file permissions, so we need a real file system in this test.
// above might be wrong
err = os.WriteFile(contentRoot+"/index.html", []byte("{{.AndrewTableOfContents}}"), 0o700)
if err != nil {
t.Fatal(err)
}
err = os.WriteFile(contentRoot+"/a_older.html", []byte{}, 0o700)
if err != nil {
t.Fatal(err)
}
err = os.WriteFile(contentRoot+"/b_newer.html", []byte{}, 0o700)
if err != nil {
t.Fatal(err)
}
now := time.Now()
older := now.Add(-10 * time.Minute)
os.Chtimes(contentRoot+"/b_newer.html", now, now)
os.Chtimes(contentRoot+"/a_older.html", older, older)
server := andrew.Server{SiteFiles: os.DirFS(contentRoot)}
page, err := andrew.NewPage(server, "index.html")
if err != nil {
t.Fatal(err)
}
received := page.Content
if expectedOrder.FindString(received) == "" {
t.Fatal(cmp.Diff(expectedOrder, received))
}
}
// newTestAndrewServer starts an andrew and returns the localhost url that you can run http gets against
// to retrieve data from that server
func newTestAndrewServer(t *testing.T, contentRoot fs.FS) *andrew.Server {
t.Helper()
// Listen on IPv4 localhost on any available port
listener, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatal(err)
}
addr := listener.Addr().String()
listener.Close()
server := andrew.NewServer(contentRoot, addr, "http://"+addr)
go func() {
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
t.Log("Server stopped with error:", err)
}
}()
// Ensure server is ready by attempting to dial the server repeatedly
ready := make(chan bool)
go func() {
defer close(ready)
for i := 0; i < 10; i++ {
conn, err := net.Dial("tcp", addr)
if err == nil {
conn.Close()
ready <- true
return
}
time.Sleep(50 * time.Millisecond) // Brief sleep to wait for server to be ready
}
t.Log("Failed to connect to server after retries:", addr)
}()
// Wait for server to be confirmed ready
<-ready
return server
}