-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmounting_completeness_test.go
More file actions
172 lines (146 loc) · 5.2 KB
/
mounting_completeness_test.go
File metadata and controls
172 lines (146 loc) · 5.2 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
package structpages
import (
"fmt"
"net/http"
"testing"
)
// Reproduce the exact user issue: EventDeletePage has no handler and no children
type userReproduceIssue struct {
AuthenticatedPages userAuthenticatedPages `route:"/"`
}
type userAuthenticatedPages struct {
EventPages userEventPages `route:"/events "`
SignoutHandler userSignoutHandler `route:"/signout Sign Out"`
IndexPage userIndexPage `route:"/{$} Home"`
AttachmentPages userAttachmentPages `route:"/attachments Attachments"`
ActionPages userActionPages `route:"/actions Actions"`
HistoryPages userHistoryPages `route:"/history History"`
}
type userEventPages struct {
EventItemPage userEventItemPage `route:"/event/{id} Event Details"`
EventEditPage userEventEditPage `route:"/event/{id}/edit Event Edit"`
EventDeletePage userEventDeletePage `route:"/event/{id} Event Delete"` // No handler, no children
}
func (userEventPages) Page() testComponent {
return testComponent{content: "events"}
}
// Pages with handlers
type (
userSignoutHandler struct{}
userIndexPage struct{}
userAttachmentPages struct{}
userActionPages struct{}
userHistoryPages struct{}
userEventItemPage struct{}
userEventEditPage struct{}
)
func (userSignoutHandler) Page() testComponent {
return testComponent{content: "signout"}
}
func (userIndexPage) Page() testComponent { return testComponent{content: "home"} }
func (userAttachmentPages) Page() testComponent {
return testComponent{content: "attachments"}
}
func (userActionPages) Page() testComponent {
return testComponent{content: "actions"}
}
func (userHistoryPages) Page() testComponent {
return testComponent{content: "history"}
}
func (userEventItemPage) Page() testComponent {
return testComponent{content: "event-item"}
}
func (userEventEditPage) Page() testComponent {
return testComponent{content: "event-edit"}
}
// This page type has NO Page() method and NO children - this causes the error
type userEventDeletePage struct{}
func printRoutes(counter *int) MiddlewareFunc {
return func(h http.Handler, pn *PageNode) http.Handler {
*counter++
fmt.Printf("✓ Registered route #%d: %-30s -> %s\n", *counter, pn.FullRoute(), pn.Name)
return h
}
}
// Test that reproduces the exact user issue and verifies the fix
func TestUserIssueReproduction(t *testing.T) {
mux := http.NewServeMux()
var routeCount int
sp, err := Mount(mux, userReproduceIssue{}, "/", "Test App", WithMiddlewares(printRoutes(&routeCount)))
// After the fix, this should NOT fail - EventDeletePage should just be skipped
if err != nil {
t.Fatalf("Expected no error after fix, but got: %v", err)
}
_ = sp
// Assert that we have the expected number of routes registered
// Expected routes:
// 1. /events -> EventPages (parent with handler)
// 2. /events/event/{id} -> EventItemPage
// 3. /events/event/{id}/edit -> EventEditPage
// 4. /signout -> SignoutHandler
// 5. /{$} -> IndexPage
// 6. /attachments -> AttachmentPages
// 7. /actions -> ActionPages
// 8. /history -> HistoryPages
// Note: EventDeletePage is NOT registered because it has no handler and no children
expectedRouteCount := 8
if routeCount != expectedRouteCount {
t.Errorf("Expected %d routes to be registered, but got %d", expectedRouteCount, routeCount)
}
t.Logf("Successfully registered %d routes", routeCount)
t.Logf("✓ EventDeletePage was correctly skipped (no handler, no children)")
t.Logf("✓ All other routes registered successfully despite EventDeletePage issue")
// Test that all routes are properly registered
tests := []struct {
name string
path string
expected string
}{
{"Events route", "/events", "events"},
{"Event item route", "/events/event/123", "event-item"},
{"Event edit route", "/events/event/123/edit", "event-edit"},
{"Signout route", "/signout", "signout"},
{"Root route", "/", "home"},
{"Attachments route", "/attachments", "attachments"},
{"Actions route", "/actions", "actions"},
{"History route", "/history", "history"},
// Note: EventDeletePage route should NOT be registered since it has no handler
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, _ := http.NewRequest("GET", tt.path, http.NoBody)
w := &responseRecorder{}
mux.ServeHTTP(w, req)
if w.code != http.StatusOK {
t.Errorf("Expected status 200, got %d for path %s", w.code, tt.path)
return
}
if w.body != tt.expected {
t.Errorf("Expected body %q, got %q for path %s", tt.expected, w.body, tt.path)
}
})
}
// Test that unregistered routes return 404
t.Run("Unregistered route should return 404", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/events/nonexistent", http.NoBody)
w := &responseRecorder{}
mux.ServeHTTP(w, req)
if w.code != http.StatusNotFound {
t.Errorf("Expected unregistered route to return 404, got %d", w.code)
}
})
}
// Simple response recorder for testing
type responseRecorder struct {
code int
body string
}
func (r *responseRecorder) Header() http.Header { return make(http.Header) }
func (r *responseRecorder) Write(data []byte) (int, error) {
r.body = string(data)
if r.code == 0 {
r.code = 200
}
return len(data), nil
}
func (r *responseRecorder) WriteHeader(code int) { r.code = code }