-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmiddleware_test.go
161 lines (126 loc) · 4.01 KB
/
middleware_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package middleware_test
import (
"bytes"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"net/http/httputil"
"os"
"testing"
"github.com/go-pg/pg/v10"
"github.com/labstack/echo/v4"
"github.com/vmkteam/zenrpc-middleware"
"github.com/vmkteam/zenrpc/v2"
"github.com/vmkteam/zenrpc/v2/testdata"
)
func newArithServer(isDevel bool, dbc *pg.DB, appName string) zenrpc.Server {
elog := log.New(os.Stderr, "E", log.LstdFlags|log.Lshortfile)
dlog := log.New(os.Stdout, "D", log.LstdFlags|log.Lshortfile)
allowDebugFn := func(param string) middleware.AllowDebugFunc {
return func(req *http.Request) bool {
return req.FormValue(param) == "true"
}
}
rpc := zenrpc.NewServer(zenrpc.Options{
ExposeSMD: true,
AllowCORS: true,
})
rpc.Use(
middleware.WithDevel(isDevel),
middleware.WithHeaders(),
middleware.WithTiming(isDevel, allowDebugFn("d")),
middleware.WithAPILogger(dlog.Printf, appName),
middleware.WithSentry(appName),
middleware.WithNoCancelContext(),
middleware.WithMetrics(appName),
middleware.WithErrorLogger(elog.Printf, appName),
)
if dbc != nil {
rpc.Use(middleware.WithSQLLogger(dbc, isDevel, allowDebugFn("d"), allowDebugFn("s")))
}
arith := testdata.ArithService{}
rpc.Register("arith", arith)
return rpc
}
func TestMiddlewareDevel(t *testing.T) {
rpc := newArithServer(true, nil, middleware.DefaultServerName)
ts := httptest.NewServer(http.HandlerFunc(rpc.ServeHTTP))
defer ts.Close()
in := `{"jsonrpc": "2.0", "method": "arith.divide", "params": { "a": 1, "b": 24 }, "id": 1 }`
out := `{"jsonrpc":"2.0","id":1,"result":{"Quo":0,"rem":1},"extensions":{"DurationLocal":0}}`
res, err := http.Post(ts.URL, "application/json", bytes.NewBufferString(in))
if err != nil {
log.Fatal(err)
}
resp, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
if string(resp) != out {
t.Errorf("Input: %s\n got %s expected %s", in, resp, out)
}
}
func TestMiddlewareNoDevel(t *testing.T) {
rpc := newArithServer(false, nil, "nodevel")
ts := httptest.NewServer(http.HandlerFunc(rpc.ServeHTTP))
defer ts.Close()
in := `{"jsonrpc": "2.0", "method": "arith.divide", "params": { "a": 1, "b": 24 }, "id": 1 }`
out := `{"jsonrpc":"2.0","id":1,"result":{"Quo":0,"rem":1}}`
res, err := http.Post(ts.URL, "application/json", bytes.NewBufferString(in))
if err != nil {
log.Fatal(err)
}
resp, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
if string(resp) != out {
t.Errorf("Input: %s\n got %s expected %s", in, resp, out)
}
}
func TestMiddlewareErrorLogger(t *testing.T) {
rpc := newArithServer(false, nil, "errordevel")
ts := httptest.NewServer(http.HandlerFunc(rpc.ServeHTTP))
defer ts.Close()
in := `{"jsonrpc": "2.0", "method": "arith.checkzenrpcerror", "id": 0, "params": [ true ] }`
out := `{"jsonrpc":"2.0","id":0,"error":{"code":500,"message":"Internal error"}}`
req, err := http.NewRequest(http.MethodPost, ts.URL, bytes.NewBufferString(in))
if err != nil {
t.Errorf("create request failed: %v", err)
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Platform", "Test1")
req.Header.Add("Version", "v1.0.0 alpha1")
c := http.Client{}
res, err := c.Do(req)
if err != nil {
log.Fatal(err)
}
resp, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
if string(resp) != out {
t.Errorf("Input: %s\n got %s expected %s", in, resp, out)
}
}
func TestMiddlewareXRequestID(t *testing.T) {
rpc := newArithServer(true, nil, middleware.DefaultServerName)
ts := httptest.NewServer(middleware.XRequestID(http.HandlerFunc(rpc.ServeHTTP)))
defer ts.Close()
in := `{"jsonrpc": "2.0", "method": "arith.divide", "params": { "a": 1, "b": 24 }, "id": 1 }`
res, err := http.Post(ts.URL, "application/json", bytes.NewBufferString(in))
if err != nil {
log.Fatal(err)
}
xRequestId := res.Header.Get(echo.HeaderXRequestID)
if xRequestId == "" {
t.Error("Empty X-Request-ID header response")
}
bb, _ := httputil.DumpResponse(res, true)
log.Println(string(bb))
}