add test hooks

This commit is contained in:
Денис Евстигнеев 2024-07-12 11:52:44 +03:00
parent 0838d2ab9b
commit 31c151ce06
2 changed files with 42 additions and 0 deletions

View File

@ -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

View File

@ -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)
}