forked from micheartin/docgen-yes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markup_test.go
246 lines (228 loc) · 7.53 KB
/
markup_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
package docgen_test
import (
"context"
"fmt"
"net/http"
"testing"
"github.com/go-chi/chi/v5"
"github.com/teal-finance/docgen-yes"
)
func TestMarkupRoutesDoc(t *testing.T) {
type args struct {
r chi.Router
opts docgen.MarkupOpts
}
tests := []struct {
name string
args args
want string
expectFail bool
}{
{"baseMarkupRoutesDocTest", args{setupRouter(), *buildOptions()}, "will fail", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := docgen.MarkupRoutesDoc(tt.args.r, tt.args.opts); got != tt.want && !tt.expectFail {
t.Errorf("MarkupRoutesDoc() = %v, want %v", got, tt.want)
}
})
}
}
func TestMarkupDoc_String(t *testing.T) {
type fields struct {
Opts docgen.MarkupOpts
Router chi.Router
Doc docgen.Doc
Routes map[string]docgen.DocRouter
FormattedHTML string
RouteHTML string
}
tests := []struct {
name string
fields fields
want string
expectFail bool
}{
{
"baseMarkupToStringTest",
fields{
Opts: docgen.MarkupOpts{
ProjectPath: "",
Intro: "",
RouteText: "",
ForceRelativeLinks: false,
URLMap: map[string]string{},
},
Router: nil,
Doc: docgen.Doc{Router: docgen.DocRouter{
Middlewares: []docgen.DocMiddleware{},
Routes: map[string]docgen.DocRoute{},
}},
Routes: map[string]docgen.DocRouter{},
FormattedHTML: "",
RouteHTML: "",
},
"will fail", true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mu := &docgen.MarkupDoc{
Opts: tt.fields.Opts,
Router: tt.fields.Router,
Doc: tt.fields.Doc,
Routes: tt.fields.Routes,
FormattedHTML: tt.fields.FormattedHTML,
RouteHTML: tt.fields.RouteHTML,
// Buf: tt.fields.buf,
}
if got := mu.String(); got != tt.want && !tt.expectFail {
t.Errorf("MarkupDoc.String() = %v, want %v", got, tt.want)
}
})
}
}
func buildOptions() *docgen.MarkupOpts {
return &docgen.MarkupOpts{
ProjectPath: "",
Intro: "",
RouteText: "",
ForceRelativeLinks: false,
URLMap: map[string]string{},
}
}
func setupRouter() chi.Router {
// TODO: use mock?
var r, sr3 *chi.Mux
r = chi.NewRouter()
r.Use(RequestID)
// Some inline middleware, 1
// We just love Go's ast tools
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
})
})
r.Group(func(r chi.Router) {
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), "session.user", "anonymous")
next.ServeHTTP(w, r.WithContext(ctx))
})
})
r.Get("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("fav"))
})
r.Get("/hubs/{hubID}/view", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
s := fmt.Sprintf("/hubs/%s/view reqid:%s session:%s", chi.URLParam(r, "hubID"),
ctx.Value("requestID"), ctx.Value("session.user"))
w.Write([]byte(s))
})
r.Get("/hubs/{hubID}/view/*", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
s := fmt.Sprintf("/hubs/%s/view/%s reqid:%s session:%s", chi.URLParamFromCtx(ctx, "hubID"),
chi.URLParam(r, "*"), ctx.Value("requestID"), ctx.Value("session.user"))
w.Write([]byte(s))
})
})
r.Group(func(r chi.Router) {
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), "session.user", "elvis")
next.ServeHTTP(w, r.WithContext(ctx))
})
})
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
s := fmt.Sprintf("/ reqid:%s session:%s", ctx.Value("requestID"), ctx.Value("session.user"))
w.Write([]byte(s))
})
r.Get("/suggestions", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
s := fmt.Sprintf("/suggestions reqid:%s session:%s", ctx.Value("requestID"), ctx.Value("session.user"))
w.Write([]byte(s))
})
r.Get("/woot/{wootID}/*", func(w http.ResponseWriter, r *http.Request) {
s := fmt.Sprintf("/woot/%s/%s", chi.URLParam(r, "wootID"), chi.URLParam(r, "*"))
w.Write([]byte(s))
})
r.Route("/hubs", func(r chi.Router) {
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
})
})
r.Route("/{hubID}", func(r chi.Router) {
r.Get("/", hubIndexHandler)
r.Get("/touch", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
s := fmt.Sprintf("/hubs/%s/touch reqid:%s session:%s", chi.URLParam(r, "hubID"),
ctx.Value("requestID"), ctx.Value("session.user"))
w.Write([]byte(s))
})
sr3 = chi.NewRouter()
sr3.Get("/", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
s := fmt.Sprintf("/hubs/%s/webhooks reqid:%s session:%s", chi.URLParam(r, "hubID"),
ctx.Value("requestID"), ctx.Value("session.user"))
w.Write([]byte(s))
})
sr3.Route("/{webhookID}", func(r chi.Router) {
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
s := fmt.Sprintf("/hubs/%s/webhooks/%s reqid:%s session:%s", chi.URLParam(r, "hubID"),
chi.URLParam(r, "webhookID"), ctx.Value("requestID"), ctx.Value("session.user"))
w.Write([]byte(s))
})
})
// TODO: /webooks is not coming up as a subrouter here...
// we kind of want to wrap a Router... ?
// perhaps add .Router() to the middleware inline thing..
// and use that always.. or, can detect in that method..
r.Mount("/webhooks", chi.Chain(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), "hook", true)))
})
}).Handler(sr3))
// HMMMM.. only let Mount() for just a Router..?
// r.Mount("/webhooks", Use(...).Router(sr3))
// ... could this work even....?
// HMMMMMMMMMMMMMMMMMMMMMMMM...
// even if Mount() were to record all subhandlers mounted, we still couldn't get at the
// routes
r.Route("/posts", func(r chi.Router) {
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
s := fmt.Sprintf("/hubs/%s/posts reqid:%s session:%s", chi.URLParam(r, "hubID"),
ctx.Value("requestID"), ctx.Value("session.user"))
w.Write([]byte(s))
})
})
})
})
r.Route("/folders/", func(r chi.Router) {
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
s := fmt.Sprintf("/folders/ reqid:%s session:%s",
ctx.Value("requestID"), ctx.Value("session.user"))
w.Write([]byte(s))
})
r.Get("/public", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
s := fmt.Sprintf("/folders/public reqid:%s session:%s",
ctx.Value("requestID"), ctx.Value("session.user"))
w.Write([]byte(s))
})
r.Get("/in", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}).ServeHTTP)
r.With(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), "search", true)))
})
}).Get("/search", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("searching.."))
})
})
})
return r
}