|
7 | 7 |
|
8 | 8 | "github.com/stretchr/testify/assert" |
9 | 9 |
|
| 10 | + "github.com/mallardduck/teapot-router/internal/testutil" |
10 | 11 | "github.com/mallardduck/teapot-router/pkg/teapot" |
11 | 12 | ) |
12 | 13 |
|
@@ -58,12 +59,10 @@ func TestChiAccessor(t *testing.T) { |
58 | 59 | r.Chi().NotFound(func(w http.ResponseWriter, r *http.Request) { |
59 | 60 | notFoundCalled = true |
60 | 61 | w.WriteHeader(404) |
61 | | - _, _ = w.Write([]byte("Custom 404")) |
| 62 | + testutil.StringResponseWriterBuilder("Custom 404")(w, r) |
62 | 63 | }) |
63 | 64 |
|
64 | | - r.GET("/exists", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
65 | | - w.WriteHeader(200) |
66 | | - })) |
| 65 | + r.GET("/exists", testutil.OKResponseHandler) |
67 | 66 |
|
68 | 67 | // Test normal route works |
69 | 68 | req := httptest.NewRequest("GET", "/exists", nil) |
@@ -103,19 +102,13 @@ func TestRouterWith(t *testing.T) { |
103 | 102 | } |
104 | 103 |
|
105 | 104 | // Route without middleware |
106 | | - r.GET("/public", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
107 | | - _, _ = w.Write([]byte("PUBLIC")) |
108 | | - })) |
| 105 | + r.Func().GET("/public", testutil.StringResponseWriterBuilder("PUBLIC")) |
109 | 106 |
|
110 | 107 | // Route with mw1 |
111 | | - r.With(mw1).GET("/protected", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
112 | | - _, _ = w.Write([]byte("PROTECTED")) |
113 | | - })) |
| 108 | + r.With(mw1).Func().GET("/protected", testutil.StringResponseWriterBuilder("PROTECTED")) |
114 | 109 |
|
115 | 110 | // Route with mw1 and mw2 |
116 | | - r.With(mw1, mw2).GET("/admin", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
117 | | - _, _ = w.Write([]byte("ADMIN")) |
118 | | - })) |
| 111 | + r.With(mw1, mw2).Func().GET("/admin", testutil.StringResponseWriterBuilder("ADMIN")) |
119 | 112 |
|
120 | 113 | // Test public route |
121 | 114 | calls = nil |
@@ -195,19 +188,13 @@ func TestMiddlewareGroup(t *testing.T) { |
195 | 188 | } |
196 | 189 |
|
197 | 190 | // Public routes (no middleware) |
198 | | - r.GET("/public", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
199 | | - _, _ = w.Write([]byte("PUBLIC")) |
200 | | - })).Name("public") |
| 191 | + r.Func().GET("/public", testutil.StringResponseWriterBuilder("PUBLIC")).Name("public") |
201 | 192 |
|
202 | 193 | // Protected routes (with auth + logging middleware) |
203 | 194 | r.MiddlewareGroup(func(r *teapot.Router) { |
204 | | - r.GET("/admin", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
205 | | - _, _ = w.Write([]byte("ADMIN")) |
206 | | - })).Name("admin") |
| 195 | + r.Func().GET("/admin", testutil.StringResponseWriterBuilder("ADMIN")).Name("admin") |
207 | 196 |
|
208 | | - r.GET("/dashboard", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
209 | | - _, _ = w.Write([]byte("DASHBOARD")) |
210 | | - })).Name("dashboard") |
| 197 | + r.Func().GET("/dashboard", testutil.StringResponseWriterBuilder("DASHBOARD")).Name("dashboard") |
211 | 198 | }, auth, logging) |
212 | 199 |
|
213 | 200 | // Test public route (no middleware) |
@@ -297,9 +284,7 @@ func TestMiddlewareGroupWithNamedGroup(t *testing.T) { |
297 | 284 |
|
298 | 285 | r.MiddlewareGroup(func(r *teapot.Router) { |
299 | 286 | r.NamedGroup("/api", "api", func(r *teapot.Router) { |
300 | | - r.GET("/users", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
301 | | - _, _ = w.Write([]byte("USERS")) |
302 | | - })).Name("users") |
| 287 | + r.Func().GET("/users", testutil.StringResponseWriterBuilder("USERS")).Name("users") |
303 | 288 | }) |
304 | 289 | }, auth) |
305 | 290 |
|
|
0 commit comments