Skip to content

Commit

Permalink
Make testHandler test helper
Browse files Browse the repository at this point in the history
  • Loading branch information
toshi0607 committed Aug 13, 2022
1 parent ca1265c commit 57fdac1
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,12 @@ func TestMiddleware_Collectors(t *testing.T) {
}
}

func testHandler(w http.ResponseWriter, r *http.Request) {
time.Sleep(time.Duration(rand.Intn(5)) * time.Millisecond)
w.WriteHeader(http.StatusOK)
func testHandler(t *testing.T) http.HandlerFunc {
t.Helper()
return func(w http.ResponseWriter, r *http.Request) {
time.Sleep(time.Duration(rand.Intn(5)) * time.Millisecond)
w.WriteHeader(http.StatusOK)
}
}

func makeRequest(t *testing.T, r *chi.Mux, paths [][]string) string {
Expand Down Expand Up @@ -100,8 +103,8 @@ func TestMiddleware_Handler(t *testing.T) {
})
r.Use(m.Handler)
r.Handle("/metrics", promhttp.Handler())
r.Get("/healthz", testHandler)
r.Get("/users/{firstName}", testHandler)
r.Get("/healthz", testHandler(t))
r.Get("/users/{firstName}", testHandler(t))
paths := [][]string{
{"healthz"},
{"users", "bob"},
Expand Down Expand Up @@ -153,7 +156,7 @@ func TestMiddleware_HandlerWithCustomRegistry(t *testing.T) {
)
r.Use(m.Handler)
r.Handle("/metrics", promh)
r.Get("/healthz", testHandler)
r.Get("/healthz", testHandler(t))
paths := [][]string{
{"healthz"},
{"metrics"},
Expand Down Expand Up @@ -222,7 +225,7 @@ func TestMiddleware_HandlerWithBucketEnv(t *testing.T) {
})
r.Use(m.Handler)
r.Handle("/metrics", promhttp.Handler())
r.Get("/healthz", testHandler)
r.Get("/healthz", testHandler(t))
paths := [][]string{
{"healthz"},
{"metrics"},
Expand Down

0 comments on commit 57fdac1

Please sign in to comment.