github.com/kaiya/goutils@v1.0.1-0.20230226104005-4ae4a4dc3688/http/router/router_test.go (about) 1 package router 2 3 import ( 4 "net/http" 5 "testing" 6 7 "gitlab.momoso.com/cm/kit/third_party/lg" 8 ) 9 10 func TestRoute(t *testing.T) { 11 r := NewRouter() 12 r.Use(logger) 13 r.Use(ratelimit) 14 r.Add("/hello", http.HandlerFunc(helloHandler)) 15 lg.PanicError(http.ListenAndServe(":8088", r)) 16 } 17 18 func logger(next http.Handler) http.Handler { 19 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 20 lg.Infof("before logger") 21 next.ServeHTTP(w, r) 22 lg.Infof("after logger") 23 }) 24 } 25 26 func ratelimit(next http.Handler) http.Handler { 27 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 28 lg.Infof("before ratelimiter") 29 next.ServeHTTP(w, r) 30 lg.Infof("after ratelimiter") 31 }) 32 } 33 34 func helloHandler(w http.ResponseWriter, r *http.Request) { 35 w.Write([]byte("hello")) 36 lg.Info("helloHandler finished") 37 }