-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathplugin_test.go
114 lines (99 loc) · 3.36 KB
/
plugin_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
package accesslog_test
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/valyala/fasthttp"
"gopkg.in/yaml.v3"
"trpc.group/trpc-go/trpc-gateway/common/gwmsg"
"trpc.group/trpc-go/trpc-gateway/common/http"
"trpc.group/trpc-go/trpc-gateway/plugin"
"trpc.group/trpc-go/trpc-gateway/plugin/accesslog"
"trpc.group/trpc-go/trpc-go/client"
"trpc.group/trpc-go/trpc-go/codec"
tplugin "trpc.group/trpc-go/trpc-go/plugin"
)
func TestPlugin_CheckConfig(t *testing.T) {
p := &accesslog.Plugin{}
tDecoder := &tplugin.YamlNodeDecoder{
Node: &yaml.Node{},
}
err := p.Setup("", tDecoder)
assert.Nil(t, err)
opts := &accesslog.Options{
FieldList: []map[string]string{
{"suid": "suid"},
},
}
// check config success
decoder := &plugin.PropsDecoder{Props: opts}
err = p.CheckConfig("", decoder)
assert.Nil(t, err)
// execute filter success
_, err = accesslog.ServerFilter(context.Background(), nil, func(ctx context.Context,
req interface{}) (rsp interface{}, err error) {
return nil, nil
})
assert.Nil(t, err)
fctx := &fasthttp.RequestCtx{}
ctx := http.WithRequestContext(context.Background(), fctx)
ctx, msg := gwmsg.WithNewGWMessage(ctx)
msg.WithTargetService(&client.BackendConfig{})
msg.WithPluginConfig("accesslog", nil)
_, err = accesslog.ServerFilter(ctx, nil, func(ctx context.Context, req interface{}) (rsp interface{}, err error) {
return nil, errors.New("err")
})
assert.NotNil(t, err)
msg.WithTargetService(nil)
msg.WithPluginConfig("accesslog", nil)
_, err = accesslog.ServerFilter(ctx, nil, func(ctx context.Context, req interface{}) (rsp interface{}, err error) {
return nil, nil
})
assert.Nil(t, err)
msg.WithPluginConfig("accesslog", "nil")
_, err = accesslog.ServerFilter(ctx, nil, func(ctx context.Context, req interface{}) (rsp interface{}, err error) {
return nil, nil
})
assert.Nil(t, err)
msg.WithPluginConfig("accesslog", decoder.DecodedProps)
_, err = accesslog.ServerFilter(ctx, nil, func(ctx context.Context, req interface{}) (rsp interface{}, err error) {
return nil, nil
})
assert.Nil(t, err)
msg.WithPluginConfig("accesslog", decoder.DecodedProps)
_, err = accesslog.ServerFilter(ctx, nil, func(ctx context.Context, req interface{}) (rsp interface{}, err error) {
return nil, nil
})
assert.Nil(t, err)
msg.WithPluginConfig("accesslog", opts)
_, err = accesslog.ServerFilter(ctx, nil, func(ctx context.Context, req interface{}) (rsp interface{}, err error) {
return nil, nil
})
assert.Nil(t, err)
msg.WithPluginConfig("accesslog", opts)
_, err = accesslog.ServerFilter(ctx, nil, func(ctx context.Context, req interface{}) (rsp interface{}, err error) {
return nil, nil
})
assert.Nil(t, err)
fctx.Request.URI().SetPath("/")
ctx, tmsg := codec.WithNewMessage(ctx)
tmsg.WithCallerMethod("/")
msg.WithPluginConfig("accesslog", opts)
_, err = accesslog.ServerFilter(ctx, nil, func(ctx context.Context, req interface{}) (rsp interface{}, err error) {
return nil, nil
})
assert.Nil(t, err)
}