-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
239 lines (208 loc) · 7.59 KB
/
client_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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package jrpc2client_test
import (
"io/ioutil"
"os"
"testing"
"time"
"github.com/riftbit/jrpc2client"
"github.com/riftbit/jrpc2errors"
"github.com/riftbit/jrpc2server"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/valyala/fasthttp"
)
// DemoAPI area
type DemoAPI struct{}
type TestArgs struct {
ID string
}
type TestReply struct {
LogID string `json:"log_id"`
UserAgent string `json:"user_agent"`
}
// Test Method to test
func (h *DemoAPI) Test(ctx *fasthttp.RequestCtx, args *TestArgs, reply *TestReply) error {
if args.ID == "" {
return &jrpc2errors.Error{Code: jrpc2errors.InvalidParamsError, Message: "ID should not be empty"}
}
reply.LogID = args.ID
reply.UserAgent = string(ctx.Request.Header.UserAgent())
return nil
}
// TestUserAgent Method to test user agent value
func (h *DemoAPI) TestUserAgent(ctx *fasthttp.RequestCtx, args *TestArgs, reply *TestReply) error {
reply.UserAgent = string(ctx.Request.Header.UserAgent())
return nil
}
// TestUserAgent Method to test user agent value
func (h *DemoAPI) TestClientTimeout(ctx *fasthttp.RequestCtx, args *TestArgs, reply *TestReply) error {
time.Sleep(100 * time.Millisecond)
reply.UserAgent = string(ctx.Request.Header.UserAgent())
return nil
}
func init() {
api := jrpc2server.NewServer()
err := api.RegisterService(new(DemoAPI), "demo")
if err != nil {
panic(err)
}
reqHandler := func(ctx *fasthttp.RequestCtx) {
switch string(ctx.Path()) {
case "/api":
api.APIHandler(ctx)
default:
ctx.Error("Unsupported path", fasthttp.StatusNotFound)
}
}
go fasthttp.ListenAndServe(":65001", reqHandler)
}
func TestClient_Call_WrongAddress(t *testing.T) {
client := jrpc2client.NewClient()
client.SetBaseURL("http://127.0.0.1:12345")
dstP := &TestReply{}
err := client.Call("/api", "demo.Test", TestArgs{ID: ""}, dstP)
assert.NotNil(t, err)
}
func TestClient_Call_WrongAnswerFormat(t *testing.T) {
client := jrpc2client.NewClient()
client.SetBaseURL("http://yandex.ru")
dstP := &TestReply{}
err := client.Call("/api", "demo.Test", TestArgs{ID: ""}, dstP)
assert.NotNil(t, err)
}
func TestClient_Call_WrongAPIAnswer(t *testing.T) {
client := jrpc2client.NewClient()
client.SetBaseURL("http://127.0.0.1:65001")
dstP := &TestReply{}
err := client.Call("/api", "demo.Test", TestArgs{ID: ""}, dstP)
assert.NotNil(t, err)
}
func TestClient_CallForMap(t *testing.T) {
client := jrpc2client.NewClient()
client.SetBaseURL("http://127.0.0.1:65001")
client.SetUserAgent("JsonRPC Test Client")
client.SetBasicAuthHeader("user", "password")
dstT, err := client.CallForMap("/api", "demo.Test", TestArgs{ID: "TESTER_ID_TestMapBasicClient"})
assert.Nil(t, err)
val, ok := dstT["log_id"]
if assert.NotEqual(t, ok, false) {
assert.Equal(t, "TESTER_ID_TestMapBasicClient", val)
}
}
func TestClient_CallForMap_WrongAddress(t *testing.T) {
client := jrpc2client.NewClient()
client.SetBaseURL("http://127.0.0.1:12345")
client.SetUserAgent("JsonRPC Test Client")
client.SetBasicAuthHeader("user", "password")
_, err := client.CallForMap("/api", "demo.Test", TestArgs{ID: "TESTER_ID_TestMapBasicClient"})
assert.NotNil(t, err)
}
func TestClient_SetUserAgent_default(t *testing.T) {
client := jrpc2client.NewClient()
client.SetBaseURL("http://127.0.0.1:65001")
dstP := &TestReply{}
err := client.Call("/api", "demo.TestUserAgent", TestArgs{ID: "TESTER_ID_TestDefaultUserAgentClient"}, dstP)
assert.Nil(t, err)
assert.Equal(t, "RIFTBIT-JRPC2-CLIENT", dstP.UserAgent)
}
func TestClient_SetUserAgent_custom(t *testing.T) {
client := jrpc2client.NewClient()
client.SetBaseURL("http://127.0.0.1:65001")
client.SetUserAgent("JsonRPC Test Client")
dstP := &TestReply{}
err := client.Call("/api", "demo.TestUserAgent", TestArgs{ID: "TESTER_ID_TestCustomUserAgentClient"}, dstP)
assert.Nil(t, err)
assert.Equal(t, "JsonRPC Test Client", dstP.UserAgent)
}
func TestClient_SetBasicAuthHeader(t *testing.T) {
client := jrpc2client.NewClient()
client.SetBaseURL("http://127.0.0.1:65001")
client.SetBasicAuthHeader("user", "password")
dstP := &TestReply{}
err := client.Call("/api", "demo.Test", TestArgs{ID: "TESTER_ID_TestBasicAuthClient"}, dstP)
assert.Nil(t, err)
assert.Equal(t, "TESTER_ID_TestBasicAuthClient", dstP.LogID)
}
func TestNewClientWithLogger_devNull(t *testing.T) {
logger := &logrus.Logger{
Out: ioutil.Discard,
Formatter: &logrus.JSONFormatter{DisableTimestamp: false},
Level: logrus.DebugLevel,
}
client := jrpc2client.NewClientWithLogger(logger)
client.SetBaseURL("http://127.0.0.1:65001")
dstP := &TestReply{}
err := client.Call("/api", "demo.Test", TestArgs{ID: "TESTER_ID_TestLoggingDevNullClient"}, dstP)
assert.Nil(t, err)
assert.Equal(t, "TESTER_ID_TestLoggingDevNullClient", dstP.LogID)
}
func TestNewClientWithLogger(t *testing.T) {
logger := &logrus.Logger{
Out: os.Stdout,
Formatter: &logrus.JSONFormatter{DisableTimestamp: false},
Level: logrus.DebugLevel,
}
client := jrpc2client.NewClientWithLogger(logger)
client.SetBaseURL("http://127.0.0.1:65001")
dstP := &TestReply{}
err := client.Call("/api", "demo.Test", TestArgs{ID: "TESTER_ID_TestLoggingClient"}, dstP)
assert.Nil(t, err)
assert.Equal(t, "TESTER_ID_TestLoggingClient", dstP.LogID)
}
func TestClient_Call_double(t *testing.T) {
client := jrpc2client.NewClient()
client.SetBaseURL("http://127.0.0.1:65001")
dstP := &TestReply{}
err := client.Call("/api", "demo.Test", TestArgs{ID: "TESTER_ID_TestDoubleCallBasicClient_1"}, dstP)
assert.Nil(t, err)
assert.Equal(t, "TESTER_ID_TestDoubleCallBasicClient_1", dstP.LogID)
err = client.Call("/api", "demo.Test", TestArgs{ID: "TESTER_ID_TestDoubleCallBasicClient_2"}, dstP)
assert.Nil(t, err)
assert.Equal(t, "TESTER_ID_TestDoubleCallBasicClient_2", dstP.LogID)
}
func TestClient_Call_tripple(t *testing.T) {
client := jrpc2client.NewClient()
client.SetBaseURL("http://127.0.0.1:65001")
dstP := &TestReply{}
err := client.Call("/api", "demo.Test", TestArgs{ID: "TESTER_ID_TestTrippleCallBasicClient_1"}, dstP)
assert.Nil(t, err)
assert.Equal(t, "TESTER_ID_TestTrippleCallBasicClient_1", dstP.LogID)
err = client.Call("/api", "demo.Test", TestArgs{ID: "TESTER_ID_TestTrippleCallBasicClient_2"}, dstP)
assert.Nil(t, err)
assert.Equal(t, "TESTER_ID_TestTrippleCallBasicClient_2", dstP.LogID)
client.SetUserAgent("JsonRPC Test Client")
err = client.Call("/api", "demo.TestUserAgent", TestArgs{ID: "TESTER_ID_TestTrippleCallBasicClient_3"}, dstP)
assert.Nil(t, err)
assert.Equal(t, "JsonRPC Test Client", dstP.UserAgent)
}
func TestClient_SetClientTimeout_expectedErr(t *testing.T) {
client := jrpc2client.NewClient()
client.SetBaseURL("http://127.0.0.1:65001")
dstP := &TestReply{}
client.SetClientTimeout(10 * time.Millisecond)
err := client.Call("/api", "demo.TestClientTimeout", TestArgs{ID: ""}, dstP)
assert.NotNil(t, err)
}
func TestClient_SetClientTimeout_success(t *testing.T) {
client := jrpc2client.NewClient()
client.SetBaseURL("http://127.0.0.1:65001")
dstP := &TestReply{}
client.SetClientTimeout(1 * time.Second)
err := client.Call("/api", "demo.TestClientTimeout", TestArgs{ID: ""}, dstP)
assert.Nil(t, err)
}
func TestClient_DeleteCustomHeaderClient(t *testing.T) {
client := jrpc2client.NewClient()
client.SetBaseURL("http://127.0.0.1:65001")
dstP := &TestReply{}
client.DeleteCustomHeader("User-Agent")
err := client.Call("/api", "demo.TestClientTimeout", TestArgs{ID: ""}, dstP)
assert.Nil(t, err)
}
func TestError_Error(t *testing.T) {
client := jrpc2client.NewClient()
client.SetBaseURL("http://127.0.0.1:65001")
dstP := &TestReply{}
err := client.Call("/api", "demo.Test", TestArgs{ID: ""}, dstP)
assert.Equal(t, "ID should not be empty", err.Error())
}