-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathhttp_test.go
More file actions
330 lines (292 loc) · 13.8 KB
/
http_test.go
File metadata and controls
330 lines (292 loc) · 13.8 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
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
package main
import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/pressly/chi"
)
func Test_basicAuth(t *testing.T) {
db.Reset()
// Unauthorized (no Authorization header)
r := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
cr := chi.NewRouter()
cr.Use(basicAuth("test"))
cr.Get("/", rootIndexHandler)
cr.ServeHTTP(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 401)
// Unauthorized (wrong password)
r = httptest.NewRequest("GET", "/", nil)
r.Header.Set("Authorization", "Basic YWRtaW46YWRtaW4=")
w = httptest.NewRecorder()
cr.ServeHTTP(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 401)
// Authorized
r = httptest.NewRequest("GET", "/", nil)
r.Header.Set("Authorization", "Basic YWRtaW46dGVzdA==")
w = httptest.NewRecorder()
cr.ServeHTTP(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 200)
testEqual(t, "Body contains '0 total records' = %+v, want %+v", strings.Contains(w.Body.String(), "0 total records"), true)
}
func Test_rootIndexHandler(t *testing.T) {
db.Reset()
// No records
r := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
rootIndexHandler(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 200)
testEqual(t, "Body contains '0 total records' = %+v, want %+v", strings.Contains(w.Body.String(), "0 total records"), true)
// A record
if err := db.put("test.test", &Record{Paused: true}); err != nil {
t.Errorf("failed to put: %+v", err)
}
r = httptest.NewRequest("GET", "/", nil)
w = httptest.NewRecorder()
rootIndexHandler(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 200)
testEqual(t, "Content-Type header = %+v, want %+v", w.Header().Get("Content-Type"), "text/html; charset=utf-8")
testEqual(t, "Body contains '1 total records' = %+v, want %+v", strings.Contains(w.Body.String(), "1 total records"), true)
// Search record
r = httptest.NewRequest("GET", "/?q=te", nil)
w = httptest.NewRecorder()
rootIndexHandler(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 422)
r = httptest.NewRequest("GET", "/?q=tes", nil)
w = httptest.NewRecorder()
rootIndexHandler(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 200)
testEqual(t, "Content-Type header = %+v, want %+v", w.Header().Get("Content-Type"), "text/html; charset=utf-8")
testEqual(t, "Body contains 'Found 1 of 1 total records' = %+v, want %+v", strings.Contains(w.Body.String(), "Found <span id=\"data-count\">1</span> of <span id=\"total-count\">1</span> total records"), true)
testEqual(t, "Body contains 'test.test' = %+v, want %+v", strings.Contains(w.Body.String(), "<div class=\"column key\">test.test</div>"), true)
// List paused records
r = httptest.NewRequest("GET", "/?p=1", nil)
w = httptest.NewRecorder()
rootIndexHandler(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 200)
testEqual(t, "Content-Type header = %+v, want %+v", w.Header().Get("Content-Type"), "text/html; charset=utf-8")
testEqual(t, "Body contains '1 of 1 total records' = %+v, want %+v", strings.Contains(w.Body.String(), "<span id=\"data-count\">1</span> of <span id=\"total-count\">1</span> total records"), true)
testEqual(t, "Body contains 'test.test' = %+v, want %+v", strings.Contains(w.Body.String(), "<div class=\"column key\">test.test</div>"), true)
}
func Test_recordsCreateHandler(t *testing.T) {
db.Reset()
// Invalid
r := &http.Request{
Method: "POST",
URL: &url.URL{Path: "/records/"},
Form: url.Values{"key": {"test"}, "paused": {"1"}},
}
w := httptest.NewRecorder()
recordsCreateHandler(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 422)
// Valid
r = &http.Request{
Method: "POST",
URL: &url.URL{Path: "/records/"},
Form: url.Values{"key": {"test.test"}, "paused": {"1"}},
}
w = httptest.NewRecorder()
recordsCreateHandler(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 302)
testEqual(t, "Location header = %+v, want %+v", w.Header().Get("Location"), "/records/test.test")
// verify record created in db
rec, _ := db.get("test.test")
testEqual(t, "get() = %+v, want %+v", *rec, Record{Paused: true})
}
func Test_recordsReadHandler(t *testing.T) {
db.Reset()
// No records
r := httptest.NewRequest("GET", "/records/test.test", nil)
rctx := chi.NewRouteContext()
rctx.URLParams.Set("key", "test.test")
w := httptest.NewRecorder()
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
recordsReadHandler(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 404)
// A record
if err := db.put("test.test", &Record{Paused: true}); err != nil {
t.Errorf("failed to put: %+v", err)
}
r = httptest.NewRequest("GET", "/records/test.test", nil)
rctx = chi.NewRouteContext()
rctx.URLParams.Set("key", "test.test")
w = httptest.NewRecorder()
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
recordsReadHandler(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 200)
testEqual(t, "Content-Type header = %+v, want %+v", w.Header().Get("Content-Type"), "text/html; charset=utf-8")
testEqual(t, "Body contains '1 of 1 total records' = %+v, want %+v", strings.Contains(w.Body.String(), "<span id=\"data-count\">1</span> of <span id=\"total-count\">1</span> total records"), true)
testEqual(t, "Body contains 'test.test' = %+v, want %+v", strings.Contains(w.Body.String(), "<div class=\"column key\">test.test</div>"), true)
}
func Test_exportHostsHandler(t *testing.T) {
db.Reset()
if err := db.put("test.test", &Record{}); err != nil {
t.Errorf("failed to put: %+v", err)
}
r := httptest.NewRequest("GET", "/export/hosts.txt", nil)
w := httptest.NewRecorder()
exportHostsHandler(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 200)
testEqual(t, "Content-Type header = %+v, want %+v", w.Header().Get("Content-Type"), "text/plain; charset=utf-8")
testEqual(t, "Content-Disposition header = %+v, want %+v", w.Header().Get("Content-Disposition"), "attachment; filename=hosts.txt")
testEqual(t, "Body contains '127.0.0.1 localhost' = %+v, want %+v", strings.Contains(w.Body.String(), "127.0.0.1 localhost\n"), true)
testEqual(t, "Body contains '0.0.0.0 test.test' = %+v, want %+v", strings.Contains(w.Body.String(), "0.0.0.0 test.test\n"), true)
}
func Test_apiRecordsIndexHandler(t *testing.T) {
db.Reset()
if err := db.put("test.test", &Record{Paused: true}); err != nil {
t.Errorf("failed to put: %+v", err)
}
// Bare request
r := httptest.NewRequest("GET", "/api/records/", nil)
w := httptest.NewRecorder()
apiRecordsIndexHandler(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 422)
// Search record
r = httptest.NewRequest("GET", "/api/records/?q=te", nil)
w = httptest.NewRecorder()
apiRecordsIndexHandler(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 422)
r = httptest.NewRequest("GET", "/api/records/?q=tes", nil)
w = httptest.NewRecorder()
apiRecordsIndexHandler(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 200)
testEqual(t, "Content-Type header = %+v, want %+v", w.Header().Get("Content-Type"), "application/json; charset=utf-8")
testEqual(t, "Body = %+v, want %+v", w.Body.String(), "{\"data\":{\"test.test\":{\"paused\":true}}}\n")
// List paused records
r = httptest.NewRequest("GET", "/api/records/?p=1", nil)
w = httptest.NewRecorder()
apiRecordsIndexHandler(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 200)
testEqual(t, "Content-Type header = %+v, want %+v", w.Header().Get("Content-Type"), "application/json; charset=utf-8")
testEqual(t, "Body = %+v, want %+v", w.Body.String(), "{\"data\":{\"test.test\":{\"paused\":true}}}\n")
}
func Test_apiRecordsReadHandler(t *testing.T) {
db.Reset()
// No records
r := httptest.NewRequest("GET", "/api/records/test.test", nil)
rctx := chi.NewRouteContext()
rctx.URLParams.Set("key", "test.test")
w := httptest.NewRecorder()
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
apiRecordsReadHandler(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 404)
// A record
if err := db.put("test.test", &Record{Paused: true}); err != nil {
t.Errorf("failed to put: %+v", err)
}
r = httptest.NewRequest("GET", "/api/records/test.test", nil)
rctx = chi.NewRouteContext()
rctx.URLParams.Set("key", "test.test")
w = httptest.NewRecorder()
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
apiRecordsReadHandler(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 200)
testEqual(t, "Content-Type header = %+v, want %+v", w.Header().Get("Content-Type"), "application/json; charset=utf-8")
testEqual(t, "Body = %+v, want %+v", w.Body.String(), "{\"data\":{\"test.test\":{\"paused\":true}}}\n")
}
func Test_apiRecordsUpdateHandler(t *testing.T) {
db.Reset()
// Invalid
r := httptest.NewRequest("PUT", "/api/records/test", nil)
rctx := chi.NewRouteContext()
rctx.URLParams.Set("key", "test")
w := httptest.NewRecorder()
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
apiRecordsUpdateHandler(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 422)
// Create
r = httptest.NewRequest("PUT", "/api/records/unpaused.test", nil)
rctx = chi.NewRouteContext()
rctx.URLParams.Set("key", "unpaused.test")
w = httptest.NewRecorder()
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
apiRecordsUpdateHandler(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 200)
testEqual(t, "Content-Type header = %+v, want %+v", w.Header().Get("Content-Type"), "application/json; charset=utf-8")
testEqual(t, "Body = %+v, want %+v", w.Body.String(), "{\"data\":{\"unpaused.test\":{\"paused\":false}}}\n")
// verify record created in db
rec, _ := db.get("unpaused.test")
testEqual(t, "get() = %+v, want %+v", *rec, Record{Paused: false})
// Create paused
r = httptest.NewRequest("PUT", "/api/records/paused.test", strings.NewReader("{\"paused\":true}"))
rctx = chi.NewRouteContext()
rctx.URLParams.Set("key", "paused.test")
w = httptest.NewRecorder()
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
apiRecordsUpdateHandler(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 200)
testEqual(t, "Content-Type header = %+v, want %+v", w.Header().Get("Content-Type"), "application/json; charset=utf-8")
testEqual(t, "Body = %+v, want %+v", w.Body.String(), "{\"data\":{\"paused.test\":{\"paused\":true}}}\n")
// verify record created in db
rec, _ = db.get("paused.test")
testEqual(t, "get() = %+v, want %+v", *rec, Record{Paused: true})
// Update
r = httptest.NewRequest("PUT", "/api/records/unpaused.test", strings.NewReader("{\"paused\":true}"))
rctx = chi.NewRouteContext()
rctx.URLParams.Set("key", "unpaused.test")
w = httptest.NewRecorder()
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
apiRecordsUpdateHandler(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 200)
testEqual(t, "Body = %+v, want %+v", w.Body.String(), "{\"data\":{\"unpaused.test\":{\"paused\":true}}}\n")
// verify record updated in db
rec, _ = db.get("unpaused.test")
testEqual(t, "get() = %+v, want %+v", *rec, Record{Paused: true})
}
func Test_apiRecordsDeleteHandler(t *testing.T) {
db.Reset()
if err := db.put("test.test", &Record{}); err != nil {
t.Errorf("failed to put: %+v", err)
}
r := httptest.NewRequest("DELETE", "/api/records/test.test", nil)
rctx := chi.NewRouteContext()
rctx.URLParams.Set("key", "test.test")
w := httptest.NewRecorder()
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
apiRecordsDeleteHandler(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 204)
// verify record deleted in db
_, err := db.get("test.test")
testEqual(t, "get() err = %+v, want %+v", err, errRecordNotFound)
}
func Test_apiSettingsUpdateHandler(t *testing.T) {
db.Reset()
// Disable
testEqual(t, "isDisabled = %+v, want %+v", isDisabled, false)
r := httptest.NewRequest("GET", "/api/settings/", strings.NewReader("{\"disabled\":true}"))
w := httptest.NewRecorder()
apiSettingsUpdateHandler(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 200)
testEqual(t, "Body = %+v, want %+v", w.Body.String(), "{\"data\":{\"disabled\":true}}\n")
testEqual(t, "isDisabled = %+v, want %+v", isDisabled, true)
// Enable
testEqual(t, "isDisabled = %+v, want %+v", isDisabled, true)
r = httptest.NewRequest("GET", "/api/settings/", strings.NewReader("{\"disabled\":false}"))
w = httptest.NewRecorder()
apiSettingsUpdateHandler(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 200)
testEqual(t, "Body = %+v, want %+v", w.Body.String(), "{\"data\":{\"disabled\":false}}\n")
testEqual(t, "isDisabled = %+v, want %+v", isDisabled, false)
}
func Test_cssHandler(t *testing.T) {
r := httptest.NewRequest("GET", "/css/nogo.css", nil)
w := httptest.NewRecorder()
cssHandler(w, r)
testEqual(t, "Response code = %+v, want %+v", w.Code, 200)
testEqual(t, "Cache-Control header = %+v, want %+v", w.Header().Get("Cache-Control"), "public, max-age=31536000")
testEqual(t, "Content-Type header = %+v, want %+v", w.Header().Get("Content-Type"), "text/css")
testEqual(t, "Body contains 'Roboto' = %+v, want %+v", strings.Contains(w.Body.String(), "Roboto"), true)
}
func Test_isValidDomainName(t *testing.T) {
testEqual(t, "isValidDomainName(localhost.localdomain) = %+v, want %+v", isValidDomainName("localhost.localdomain"), false)
testEqual(t, "isValidDomainName(test) = %+v, want %+v", isValidDomainName("test"), false)
testEqual(t, "isValidDomainName(te.s) = %+v, want %+v", isValidDomainName("te.s"), false)
testEqual(t, "isValidDomainName(t.s) = %+v, want %+v", isValidDomainName("t.s"), false)
testEqual(t, "isValidDomainName(te.st) = %+v, want %+v", isValidDomainName("te.st"), true)
testEqual(t, "isValidDomainName(t.st) = %+v, want %+v", isValidDomainName("t.st"), true)
testEqual(t, "isValidDomainName(test.test.test.test) = %+v, want %+v", isValidDomainName("test.test.test.test"), true)
}