-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandler_test.go
More file actions
217 lines (196 loc) · 5.76 KB
/
handler_test.go
File metadata and controls
217 lines (196 loc) · 5.76 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
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
package ukuleleweb
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/peterbourgon/diskv/v3"
)
// noRedirectClient is an HTTP client that does not follow redirects.
var noRedirectClient = &http.Client{
CheckRedirect: func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
},
}
func testServer(t *testing.T) *httptest.Server {
t.Helper()
h := NewServer(&Config{
Store: diskv.New(diskv.Options{
BasePath: t.TempDir(),
CacheSizeMax: 1024 * 1024,
}),
})
ts := httptest.NewServer(h)
t.Cleanup(ts.Close)
return ts
}
func TestInvalidPageName(t *testing.T) {
ts := testServer(t)
for _, tt := range []struct {
name, method, path string
}{
{"ViewInvalidPage", "GET", "/notapage"},
{"EditInvalidPage", "GET", "/edit/notapage"},
{"SaveInvalidPage", "POST", "/edit/notapage"},
{"RawInvalidPage", "GET", "/notapage.md"},
} {
t.Run(tt.name, func(t *testing.T) {
req, err := http.NewRequest(tt.method, ts.URL+tt.path, nil)
if err != nil {
t.Fatalf("http.NewRequest(%q, %q): %v", tt.method, tt.path, err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("http.DefaultClient.Do(%s %s): %v", tt.method, tt.path, err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusNotFound {
t.Errorf("Status code = %d, want %d", resp.StatusCode, http.StatusNotFound)
}
})
}
}
func TestRootRedirectsToMainPage(t *testing.T) {
ts := testServer(t)
resp, err := noRedirectClient.Get(ts.URL + "/")
if err != nil {
t.Fatalf("GET /: %v", err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusMovedPermanently {
t.Errorf("Status code = %d, want %d", resp.StatusCode, http.StatusMovedPermanently)
}
if got := resp.Header.Get("Location"); got != "/MainPage" {
t.Errorf("Location = %q, want %q", got, "/MainPage")
}
}
func TestPreview(t *testing.T) {
ts := testServer(t)
resp, err := http.Post(ts.URL+"/preview", "text/plain", strings.NewReader("Hello *World*!"))
if err != nil {
t.Fatalf("POST /preview: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("Status code = %d, want %d", resp.StatusCode, http.StatusOK)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("io.ReadAll: %v", err)
}
if got, want := string(body), "<p>Hello <em>World</em>!</p>\n"; got != want {
t.Errorf("Body = %q, want %q", got, want)
}
}
func TestSavePage(t *testing.T) {
ts := testServer(t)
resp, err := noRedirectClient.Post(ts.URL+"/edit/TestPage", "application/x-www-form-urlencoded", strings.NewReader("content=hello"))
if err != nil {
t.Fatalf("POST /edit/TestPage: %v", err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusFound {
t.Errorf("Status code = %d, want %d", resp.StatusCode, http.StatusFound)
}
if got := resp.Header.Get("Location"); got != "/TestPage" {
t.Errorf("Location = %q, want %q", got, "/TestPage")
}
// Verify the saved content is served back.
resp, err = http.Get(ts.URL + "/TestPage")
if err != nil {
t.Fatalf("GET /TestPage: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("io.ReadAll: %v", err)
}
if !strings.Contains(string(body), "hello") {
t.Errorf("GET /TestPage body does not contain saved content %q", "hello")
}
}
func TestRawMarkdown(t *testing.T) {
ts := testServer(t)
// Save a page first.
resp, err := noRedirectClient.Post(ts.URL+"/edit/TestPage", "application/x-www-form-urlencoded", strings.NewReader("content=Hello+*World*!"))
if err != nil {
t.Fatalf("POST /edit/TestPage: %v", err)
}
resp.Body.Close()
// Fetch the raw markdown.
resp, err = http.Get(ts.URL + "/TestPage.md")
if err != nil {
t.Fatalf("GET /TestPage.md: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("Status code = %d, want %d", resp.StatusCode, http.StatusOK)
}
if got := resp.Header.Get("Content-Type"); got != "text/markdown; charset=utf-8" {
t.Errorf("Content-Type = %q, want %q", got, "text/markdown; charset=utf-8")
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("io.ReadAll: %v", err)
}
if got, want := string(body), "Hello *World*!"; got != want {
t.Errorf("Body = %q, want %q", got, want)
}
}
func TestContentNegotiationMarkdown(t *testing.T) {
ts := testServer(t)
resp, err := noRedirectClient.Post(ts.URL+"/edit/TestPage", "application/x-www-form-urlencoded", strings.NewReader("content=Hello+*World*!"))
if err != nil {
t.Fatalf("POST /edit/TestPage: %v", err)
}
resp.Body.Close()
req, err := http.NewRequest("GET", ts.URL+"/TestPage", nil)
if err != nil {
t.Fatalf("http.NewRequest: %v", err)
}
req.Header.Set("Accept", "text/markdown")
resp, err = http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("GET /TestPage with Accept text/markdown: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("Status code = %d, want %d", resp.StatusCode, http.StatusOK)
}
if got, want := resp.Header.Get("Content-Type"), "text/markdown; charset=utf-8"; got != want {
t.Errorf("Content-Type = %q, want %q", got, want)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("io.ReadAll: %v", err)
}
if got, want := string(body), "Hello *World*!"; got != want {
t.Errorf("Body = %q, want %q", got, want)
}
}
func TestIsPageName(t *testing.T) {
for _, pn := range []string{
"PageName",
"AlsoPageName",
"WönderfülPägeNäme",
// XXX: It's unclear to me why this is not recognized. Unicode shenanigans?
// "ÄtschiBätschi",
} {
if !isPageName(pn) {
t.Errorf("isPageName(%q) = false, want true", pn)
}
}
}
func TestIsNotPageName(t *testing.T) {
for _, pn := range []string{
"foo PageName bar",
"/AlsoPageName/",
"Oneword",
"123",
} {
if isPageName(pn) {
t.Errorf("isPageName(%q) = true, want false", pn)
}
}
}