-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_test.go
More file actions
575 lines (492 loc) · 16.7 KB
/
handler_test.go
File metadata and controls
575 lines (492 loc) · 16.7 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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
package main
import (
"crypto/tls"
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
func setupTestServer() (*http.ServeMux, *Store) {
store := NewStore()
mux := http.NewServeMux()
registerRoutes(mux, store)
return mux, store
}
func TestHealthEndpoint(t *testing.T) {
mux, _ := setupTestServer()
req := httptest.NewRequest("GET", "/health", nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
if ct := w.Header().Get("Content-Type"); !strings.HasPrefix(ct, "application/json") {
t.Fatalf("expected application/json content-type, got %s", ct)
}
var body map[string]string
if err := json.NewDecoder(w.Body).Decode(&body); err != nil {
t.Fatalf("failed to decode JSON: %v", err)
}
if body["status"] != "ok" {
t.Fatalf("expected status=ok, got %s", body["status"])
}
if body["service"] != "bringit" {
t.Fatalf("expected service=bringit, got %s", body["service"])
}
if body["timestamp"] == "" {
t.Fatal("expected non-empty timestamp")
}
}
func TestIndexPage(t *testing.T) {
mux, _ := setupTestServer()
req := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
if !strings.Contains(w.Body.String(), "BringIt") {
t.Fatal("expected page to contain BringIt")
}
}
func TestCreateListAndView(t *testing.T) {
mux, _ := setupTestServer()
// Create list
form := url.Values{"title": {"夏キャンプ"}, "description": {"河口湖"}}
req := httptest.NewRequest("POST", "/lists", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusSeeOther {
t.Fatalf("expected 303, got %d", w.Code)
}
loc := w.Header().Get("Location")
if !strings.HasPrefix(loc, "/lists/") {
t.Fatalf("expected redirect to /lists/..., got %s", loc)
}
// View list
req = httptest.NewRequest("GET", loc, nil)
w = httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
body := w.Body.String()
if !strings.Contains(body, "夏キャンプ") {
t.Fatal("expected list title in response")
}
if !strings.Contains(body, "河口湖") {
t.Fatal("expected description in response")
}
}
func TestAddItemAndToggle(t *testing.T) {
mux, store := setupTestServer()
l := store.CreateList("テスト", "")
token := l.ShareToken
// Add item
form := url.Values{"name": {"テント"}, "assignee": {"太郎"}, "required": {"on"}}
req := httptest.NewRequest("POST", "/lists/"+token+"/items", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusSeeOther {
t.Fatalf("expected 303, got %d", w.Code)
}
// Verify item was added
l = store.GetList(token)
if len(l.Items) != 1 {
t.Fatalf("expected 1 item, got %d", len(l.Items))
}
item := l.Items[0]
if item.Name != "テント" || item.Assignee != "太郎" || !item.Required || item.Prepared {
t.Fatalf("unexpected item state: %+v", item)
}
// Toggle prepared
req = httptest.NewRequest("POST", "/lists/"+token+"/items/"+item.ID+"/toggle-prepared", nil)
w = httptest.NewRecorder()
mux.ServeHTTP(w, req)
if !store.GetList(token).Items[0].Prepared {
t.Fatal("expected item to be prepared after toggle")
}
// Toggle required
req = httptest.NewRequest("POST", "/lists/"+token+"/items/"+item.ID+"/toggle-required", nil)
w = httptest.NewRecorder()
mux.ServeHTTP(w, req)
if store.GetList(token).Items[0].Required {
t.Fatal("expected item to be optional after toggle")
}
}
func TestDeleteItem(t *testing.T) {
mux, store := setupTestServer()
l := store.CreateList("テスト", "")
store.AddItem(l.ShareToken, "寝袋", "", true)
item := store.GetList(l.ShareToken).Items[0]
req := httptest.NewRequest("POST", "/lists/"+l.ShareToken+"/items/"+item.ID+"/delete", nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if len(store.GetList(l.ShareToken).Items) != 0 {
t.Fatal("expected item to be deleted")
}
}
func TestUpdateAssignee(t *testing.T) {
mux, store := setupTestServer()
l := store.CreateList("テスト", "")
store.AddItem(l.ShareToken, "クーラーボックス", "太郎", true)
item := store.GetList(l.ShareToken).Items[0]
form := url.Values{"assignee": {"花子"}}
req := httptest.NewRequest("POST", "/lists/"+l.ShareToken+"/items/"+item.ID+"/assignee", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if store.GetList(l.ShareToken).Items[0].Assignee != "花子" {
t.Fatal("expected assignee to be updated")
}
}
func TestNotFoundList(t *testing.T) {
mux, _ := setupTestServer()
req := httptest.NewRequest("GET", "/lists/nonexistent", nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusNotFound {
t.Fatalf("expected 404, got %d", w.Code)
}
}
func TestCreateListEmptyTitle(t *testing.T) {
mux, _ := setupTestServer()
form := url.Values{"title": {""}}
req := httptest.NewRequest("POST", "/lists", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusBadRequest {
t.Fatalf("expected 400, got %d", w.Code)
}
}
func TestCreateListWhitespaceTitle(t *testing.T) {
mux, _ := setupTestServer()
form := url.Values{"title": {" "}}
req := httptest.NewRequest("POST", "/lists", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusBadRequest {
t.Fatalf("expected 400 for whitespace-only title, got %d", w.Code)
}
}
func TestDeleteList(t *testing.T) {
mux, store := setupTestServer()
l := store.CreateList("削除テスト", "")
token := l.ShareToken
req := httptest.NewRequest("POST", "/lists/"+token+"/delete", nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusSeeOther {
t.Fatalf("expected 303, got %d", w.Code)
}
if loc := w.Header().Get("Location"); loc != "/" {
t.Fatalf("expected redirect to /, got %s", loc)
}
if store.GetList(token) != nil {
t.Fatal("expected list to be deleted from store")
}
}
func TestDeleteListNotFound(t *testing.T) {
mux, _ := setupTestServer()
req := httptest.NewRequest("POST", "/lists/nonexistent-token/delete", nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusNotFound {
t.Fatalf("expected 404, got %d", w.Code)
}
}
func TestAddItemEmptyName(t *testing.T) {
mux, store := setupTestServer()
l := store.CreateList("テスト", "")
token := l.ShareToken
form := url.Values{"name": {""}, "assignee": {"太郎"}}
req := httptest.NewRequest("POST", "/lists/"+token+"/items", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
// Should redirect without adding item
if w.Code != http.StatusSeeOther {
t.Fatalf("expected 303, got %d", w.Code)
}
if len(store.GetList(token).Items) != 0 {
t.Fatal("expected no items to be added for empty name")
}
}
func TestAddItemWhitespaceName(t *testing.T) {
mux, store := setupTestServer()
l := store.CreateList("テスト", "")
token := l.ShareToken
form := url.Values{"name": {" "}}
req := httptest.NewRequest("POST", "/lists/"+token+"/items", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusSeeOther {
t.Fatalf("expected 303, got %d", w.Code)
}
if len(store.GetList(token).Items) != 0 {
t.Fatal("expected no items to be added for whitespace-only name")
}
}
func TestTogglePreparedInvalidItem(t *testing.T) {
mux, store := setupTestServer()
l := store.CreateList("テスト", "")
token := l.ShareToken
req := httptest.NewRequest("POST", "/lists/"+token+"/items/nonexistent-id/toggle-prepared", nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
// Handler should redirect gracefully even for invalid item ID
if w.Code != http.StatusSeeOther {
t.Fatalf("expected 303 redirect for invalid item ID, got %d", w.Code)
}
}
func TestToggleRequiredInvalidItem(t *testing.T) {
mux, store := setupTestServer()
l := store.CreateList("テスト", "")
token := l.ShareToken
req := httptest.NewRequest("POST", "/lists/"+token+"/items/nonexistent-id/toggle-required", nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusSeeOther {
t.Fatalf("expected 303 redirect for invalid item ID, got %d", w.Code)
}
}
func TestUpdateAssigneeInvalidItem(t *testing.T) {
mux, store := setupTestServer()
l := store.CreateList("テスト", "")
token := l.ShareToken
form := url.Values{"assignee": {"花子"}}
req := httptest.NewRequest("POST", "/lists/"+token+"/items/nonexistent-id/assignee", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusSeeOther {
t.Fatalf("expected 303 redirect for invalid item ID, got %d", w.Code)
}
}
func TestDeleteItemInvalidItem(t *testing.T) {
mux, store := setupTestServer()
l := store.CreateList("テスト", "")
store.AddItem(l.ShareToken, "アイテム", "", true)
token := l.ShareToken
req := httptest.NewRequest("POST", "/lists/"+token+"/items/nonexistent-id/delete", nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusSeeOther {
t.Fatalf("expected 303 redirect for invalid item ID, got %d", w.Code)
}
// Existing item should still be there
if len(store.GetList(token).Items) != 1 {
t.Fatal("expected existing item to remain after deleting nonexistent ID")
}
}
func TestIndexPageNotFound(t *testing.T) {
mux, _ := setupTestServer()
req := httptest.NewRequest("GET", "/nonexistent-path", nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusNotFound {
t.Fatalf("expected 404 for unknown path, got %d", w.Code)
}
}
func TestRequestLoggerMiddleware(t *testing.T) {
mux, _ := setupTestServer()
handler := requestLogger(mux)
req := httptest.NewRequest("GET", "/health", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200 through middleware, got %d", w.Code)
}
}
func TestResponseWriterStatusCode(t *testing.T) {
w := httptest.NewRecorder()
rw := &responseWriter{ResponseWriter: w, statusCode: http.StatusOK}
rw.WriteHeader(http.StatusNotFound)
if rw.statusCode != http.StatusNotFound {
t.Fatalf("expected statusCode=404, got %d", rw.statusCode)
}
}
func TestTruncateRunes(t *testing.T) {
tests := []struct {
input string
maxLen int
expected string
}{
{"hello", 10, "hello"},
{"hello", 3, "hel"},
{"あいうえお", 3, "あいう"},
{"", 5, ""},
{"abc", 0, ""},
{"テスト文字列", 6, "テスト文字列"},
{"テスト文字列", 4, "テスト文"},
}
for _, tt := range tests {
result := truncateRunes(tt.input, tt.maxLen)
if result != tt.expected {
t.Errorf("truncateRunes(%q, %d) = %q, want %q", tt.input, tt.maxLen, result, tt.expected)
}
}
}
func TestCreateListTitleTruncation(t *testing.T) {
mux, store := setupTestServer()
// 101文字のタイトル(最大100文字に切り詰められる)
longTitle := strings.Repeat("あ", 101)
form := url.Values{"title": {longTitle}}
req := httptest.NewRequest("POST", "/lists", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusSeeOther {
t.Fatalf("expected 303, got %d", w.Code)
}
loc := w.Header().Get("Location")
token := strings.TrimPrefix(loc, "/lists/")
l := store.GetList(token)
if l == nil {
t.Fatal("expected list to be created")
}
titleRunes := []rune(l.Title)
if len(titleRunes) != maxTitleLen {
t.Fatalf("expected title to be truncated to %d runes, got %d", maxTitleLen, len(titleRunes))
}
}
func TestCreateListDescriptionTruncation(t *testing.T) {
mux, store := setupTestServer()
longDesc := strings.Repeat("x", 501)
form := url.Values{"title": {"テスト"}, "description": {longDesc}}
req := httptest.NewRequest("POST", "/lists", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
loc := w.Header().Get("Location")
token := strings.TrimPrefix(loc, "/lists/")
l := store.GetList(token)
if len(l.Description) != maxDescriptionLen {
t.Fatalf("expected description to be truncated to %d chars, got %d", maxDescriptionLen, len(l.Description))
}
}
func TestAddItemNameTruncation(t *testing.T) {
mux, store := setupTestServer()
l := store.CreateList("テスト", "")
token := l.ShareToken
longName := strings.Repeat("い", 150)
form := url.Values{"name": {longName}, "assignee": {"太郎"}}
req := httptest.NewRequest("POST", "/lists/"+token+"/items", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
items := store.GetList(token).Items
if len(items) != 1 {
t.Fatalf("expected 1 item, got %d", len(items))
}
nameRunes := []rune(items[0].Name)
if len(nameRunes) != maxItemNameLen {
t.Fatalf("expected item name to be truncated to %d runes, got %d", maxItemNameLen, len(nameRunes))
}
}
func TestAddItemAssigneeTruncation(t *testing.T) {
mux, store := setupTestServer()
l := store.CreateList("テスト", "")
token := l.ShareToken
longAssignee := strings.Repeat("う", 80)
form := url.Values{"name": {"アイテム"}, "assignee": {longAssignee}}
req := httptest.NewRequest("POST", "/lists/"+token+"/items", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
items := store.GetList(token).Items
assigneeRunes := []rune(items[0].Assignee)
if len(assigneeRunes) != maxAssigneeLen {
t.Fatalf("expected assignee to be truncated to %d runes, got %d", maxAssigneeLen, len(assigneeRunes))
}
}
func TestUpdateAssigneeTruncation(t *testing.T) {
mux, store := setupTestServer()
l := store.CreateList("テスト", "")
store.AddItem(l.ShareToken, "アイテム", "太郎", true)
item := store.GetList(l.ShareToken).Items[0]
longAssignee := strings.Repeat("え", 60)
form := url.Values{"assignee": {longAssignee}}
req := httptest.NewRequest("POST", "/lists/"+l.ShareToken+"/items/"+item.ID+"/assignee", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
updated := store.GetList(l.ShareToken).Items[0]
assigneeRunes := []rune(updated.Assignee)
if len(assigneeRunes) != maxAssigneeLen {
t.Fatalf("expected assignee to be truncated to %d runes, got %d", maxAssigneeLen, len(assigneeRunes))
}
}
func TestSecurityHeaders(t *testing.T) {
mux, _ := setupTestServer()
handler := securityHeaders(mux)
req := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
tests := map[string]string{
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "DENY",
"Referrer-Policy": "strict-origin-when-cross-origin",
"Permissions-Policy": "camera=(), microphone=(), geolocation=()",
}
for header, expected := range tests {
got := w.Header().Get(header)
if got != expected {
t.Errorf("header %s: expected %q, got %q", header, expected, got)
}
}
}
func TestBuildShareURL(t *testing.T) {
tests := []struct {
name string
proto string // X-Forwarded-Proto
useTLS bool
host string
token string
expected string
}{
{
name: "デフォルトHTTP",
host: "example.com",
token: "abc123",
expected: "http://example.com/lists/abc123",
},
{
name: "X-Forwarded-ProtoでHTTPS",
proto: "https",
host: "example.com",
token: "abc123",
expected: "https://example.com/lists/abc123",
},
{
name: "TLS接続でHTTPS",
useTLS: true,
host: "example.com:443",
token: "xyz",
expected: "https://example.com:443/lists/xyz",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req.Host = tt.host
if tt.proto != "" {
req.Header.Set("X-Forwarded-Proto", tt.proto)
}
if tt.useTLS {
req.TLS = &tls.ConnectionState{}
}
got := buildShareURL(req, tt.token)
if got != tt.expected {
t.Errorf("expected %q, got %q", tt.expected, got)
}
})
}
}