From 31c151ce069bcd7f7b877bdfad814b2d4d2e8f9a Mon Sep 17 00:00:00 2001 From: Evstigneev Denis Date: Fri, 12 Jul 2024 11:52:44 +0300 Subject: [PATCH 1/2] add test hooks --- handler.go | 1 + util_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/handler.go b/handler.go index 489f5c7..9083678 100644 --- a/handler.go +++ b/handler.go @@ -559,6 +559,7 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { if r.Body != nil { var buf []byte buf, err = io.ReadAll(r.Body) + r.Body.Close() if err != nil && err != io.EOF { h.errorHandler(ctx, handler, w, r, err, http.StatusInternalServerError) return diff --git a/util_test.go b/util_test.go index 76c7cd3..0afa8b3 100644 --- a/util_test.go +++ b/util_test.go @@ -3,6 +3,9 @@ package http import ( "bytes" "context" + "fmt" + "go.unistack.org/micro/v3/options" + "go.unistack.org/micro/v3/server" "net/http" "strings" "testing" @@ -54,3 +57,41 @@ func TestFillrequest(t *testing.T) { t.Fatalf("FillRequest error: %#+v", req) } } + +func Test_Hook(t *testing.T) { + opts := server.Options{} + + var fn server.HandlerFunc = func(fctx context.Context, req server.Request, rsp interface{}) (err error) { + fmt.Println("1") + return nil + } + + var fn2 server.HandlerWrapper = func(server.HandlerFunc) server.HandlerFunc { + return func(ctx context.Context, req server.Request, rsp interface{}) error { + fmt.Println("2") + return nil + } + } + var fn3 server.HandlerWrapper = func(server.HandlerFunc) server.HandlerFunc { + return func(ctx context.Context, req server.Request, rsp interface{}) error { + fmt.Println("3") + return nil + } + } + var fn4 server.HandlerWrapper = func(server.HandlerFunc) server.HandlerFunc { + return func(ctx context.Context, req server.Request, rsp interface{}) error { + fmt.Println("4") + return nil + } + } + + opts.Hooks = append(opts.Hooks, fn2, fn3, fn4) + + opts.Hooks.EachNext(func(hook options.Hook) { + if h, ok := hook.(server.HandlerWrapper); ok { + fn = h(fn) + } + }) + + fn(nil, nil, nil) +} -- 2.45.2 From 581159db64e7ffb4a53b8e6794f676c327d4ff8a Mon Sep 17 00:00:00 2001 From: Evstigneev Denis Date: Sat, 21 Dec 2024 20:17:55 +0300 Subject: [PATCH 2/2] update call Hooks && linter --- handler.go | 6 +++--- request.go | 4 +--- subscriber.go | 2 +- util_test.go | 41 ----------------------------------------- 4 files changed, 5 insertions(+), 48 deletions(-) diff --git a/handler.go b/handler.go index b26a52b..d001c8e 100644 --- a/handler.go +++ b/handler.go @@ -172,7 +172,7 @@ func (h *Server) HTTPHandlerFunc(handler interface{}) (http.HandlerFunc, error) hdlr := shdlr.(*httpHandler) fh, mp, err := hdlr.handlers.Search(http.MethodPost, "/"+microMethod) if err == nil { - match = true + // match = true for k, v := range mp { matches[k] = v } @@ -278,7 +278,7 @@ func (h *Server) HTTPHandlerFunc(handler interface{}) (http.HandlerFunc, error) } // wrap the handler func - h.opts.Hooks.EachNext(func(hook options.Hook) { + h.opts.Hooks.EachPrev(func(hook options.Hook) { if h, ok := hook.(server.HookHandler); ok { fn = h(fn) } @@ -646,7 +646,7 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { return err } - h.opts.Hooks.EachNext(func(hook options.Hook) { + h.opts.Hooks.EachPrev(func(hook options.Hook) { if h, ok := hook.(server.HookHandler); ok { fn = h(fn) } diff --git a/request.go b/request.go index 1b301b2..40a7ff9 100644 --- a/request.go +++ b/request.go @@ -1,8 +1,6 @@ package http import ( - "io" - "go.unistack.org/micro/v3/codec" "go.unistack.org/micro/v3/metadata" "go.unistack.org/micro/v3/server" @@ -14,7 +12,7 @@ var ( ) type rpcRequest struct { - rw io.ReadWriter + // rw io.ReadWriter payload interface{} codec codec.Codec header metadata.Metadata diff --git a/subscriber.go b/subscriber.go index 8d7d97a..ed0ad06 100644 --- a/subscriber.go +++ b/subscriber.go @@ -152,7 +152,7 @@ func (s *Server) createSubHandler(sb *httpSubscriber, opts server.Options) broke return nil } - opts.Hooks.EachNext(func(hook options.Hook) { + opts.Hooks.EachPrev(func(hook options.Hook) { if h, ok := hook.(server.HookSubHandler); ok { fn = h(fn) } diff --git a/util_test.go b/util_test.go index 4e11ec3..f19e867 100644 --- a/util_test.go +++ b/util_test.go @@ -3,9 +3,6 @@ package http import ( "bytes" "context" - "fmt" - "go.unistack.org/micro/v3/options" - "go.unistack.org/micro/v3/server" "net/http" "strings" "testing" @@ -101,41 +98,3 @@ func TestFillrequest(t *testing.T) { t.Fatalf("FillRequest error: %#+v", req) } } - -func Test_Hook(t *testing.T) { - opts := server.Options{} - - var fn server.HandlerFunc = func(fctx context.Context, req server.Request, rsp interface{}) (err error) { - fmt.Println("1") - return nil - } - - var fn2 server.HandlerWrapper = func(server.HandlerFunc) server.HandlerFunc { - return func(ctx context.Context, req server.Request, rsp interface{}) error { - fmt.Println("2") - return nil - } - } - var fn3 server.HandlerWrapper = func(server.HandlerFunc) server.HandlerFunc { - return func(ctx context.Context, req server.Request, rsp interface{}) error { - fmt.Println("3") - return nil - } - } - var fn4 server.HandlerWrapper = func(server.HandlerFunc) server.HandlerFunc { - return func(ctx context.Context, req server.Request, rsp interface{}) error { - fmt.Println("4") - return nil - } - } - - opts.Hooks = append(opts.Hooks, fn2, fn3, fn4) - - opts.Hooks.EachNext(func(hook options.Hook) { - if h, ok := hook.(server.HandlerWrapper); ok { - fn = h(fn) - } - }) - - fn(nil, nil, nil) -} -- 2.45.2