2021-10-26 22:36:04 +03:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2024-07-12 11:52:44 +03:00
|
|
|
"fmt"
|
|
|
|
"go.unistack.org/micro/v3/options"
|
|
|
|
"go.unistack.org/micro/v3/server"
|
2021-10-26 22:36:04 +03:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"go.unistack.org/micro/v3/metadata"
|
2024-09-17 12:47:19 +03:00
|
|
|
"go.unistack.org/micro/v3/options"
|
|
|
|
"go.unistack.org/micro/v3/server"
|
2021-10-26 22:36:04 +03:00
|
|
|
)
|
|
|
|
|
2024-09-17 12:47:19 +03:00
|
|
|
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(next server.HandlerFunc) server.HandlerFunc {
|
|
|
|
return func(ctx context.Context, req server.Request, rsp interface{}) error {
|
|
|
|
// fmt.Println("2")
|
|
|
|
return next(ctx, req, rsp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var fn3 server.HandlerWrapper = func(next server.HandlerFunc) server.HandlerFunc {
|
|
|
|
return func(ctx context.Context, req server.Request, rsp interface{}) error {
|
|
|
|
// fmt.Println("3")
|
|
|
|
return next(ctx, req, rsp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var fn4 server.HandlerWrapper = func(next server.HandlerFunc) server.HandlerFunc {
|
|
|
|
return func(ctx context.Context, req server.Request, rsp interface{}) error {
|
|
|
|
// fmt.Println("4")
|
|
|
|
return next(ctx, req, rsp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.Hooks = append(opts.Hooks, fn2, fn3, fn4)
|
|
|
|
|
|
|
|
opts.Hooks.EachNext(func(hook options.Hook) {
|
|
|
|
if h, ok := hook.(server.HandlerWrapper); ok {
|
|
|
|
// fmt.Printf("h %#+v\n", h)
|
|
|
|
fn = h(fn)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
err := fn(nil, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2021-10-26 22:36:04 +03:00
|
|
|
|
|
|
|
func TestFillrequest(t *testing.T) {
|
|
|
|
md := metadata.New(1)
|
|
|
|
md.Set("ClientID", "xxx")
|
|
|
|
type request struct {
|
|
|
|
Token string
|
|
|
|
ClientID string
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
hreq, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/v1", nil)
|
|
|
|
cookie1 := &http.Cookie{Name: "Token", Value: "zzz"}
|
|
|
|
cookie2 := &http.Cookie{Name: "Token", Value: "zzz"}
|
|
|
|
hreq.AddCookie(cookie1)
|
|
|
|
hreq.AddCookie(cookie2)
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
_ = hreq.Write(buf)
|
|
|
|
var cookie string
|
|
|
|
var line string
|
|
|
|
var err error
|
|
|
|
for {
|
|
|
|
line, err = buf.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if strings.Contains(line, "Cookie") {
|
|
|
|
cookie = strings.TrimSpace(strings.Split(line, ":")[1])
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
md.Set("Cookie", cookie)
|
|
|
|
ctx = metadata.NewIncomingContext(ctx, md)
|
|
|
|
req := &request{}
|
|
|
|
|
|
|
|
if err := FillRequest(ctx, req, Cookie("Token", "true"), Header("ClientID", "true")); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if req.ClientID != "xxx" {
|
|
|
|
t.Fatalf("FillRequest error: %#+v", req)
|
|
|
|
}
|
|
|
|
if req.Token != "zzz" {
|
|
|
|
t.Fatalf("FillRequest error: %#+v", req)
|
|
|
|
}
|
|
|
|
}
|
2024-07-12 11:52:44 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|