17 | 17 |
String string
|
18 | 18 |
}
|
19 | 19 |
|
20 | |
func TestCanCallBeforeFunc(t *testing.T) {
|
21 | |
called := false
|
22 | |
u, _ := url.Parse("http://senseye.io/jsonrpc")
|
23 | |
sut := jsonrpc.NewClient(
|
24 | |
u,
|
25 | |
"add",
|
26 | |
jsonrpc.ClientBefore(func(ctx context.Context, req *http.Request) context.Context {
|
27 | |
called = true
|
28 | |
return ctx
|
29 | |
}),
|
30 | |
)
|
31 | |
|
32 | |
sut.Endpoint()(context.TODO(), "foo")
|
33 | |
|
34 | |
if !called {
|
35 | |
t.Fatal("Expected client before func to be called. Wasn't.")
|
36 | |
}
|
|
20 |
type testServerResponseOptions struct {
|
|
21 |
Body string
|
|
22 |
Status int
|
|
23 |
}
|
|
24 |
|
|
25 |
func httptestServer(t *testing.T) *httptest.Server {
|
|
26 |
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
27 |
defer r.Body.Close()
|
|
28 |
|
|
29 |
var testReq jsonrpc.Request
|
|
30 |
if err := json.NewDecoder(r.Body).Decode(&testReq); err != nil {
|
|
31 |
t.Fatal(err)
|
|
32 |
}
|
|
33 |
|
|
34 |
var options testServerResponseOptions
|
|
35 |
if err := json.Unmarshal(testReq.Params, &options); err != nil {
|
|
36 |
t.Fatal(err)
|
|
37 |
}
|
|
38 |
|
|
39 |
if options.Status == 0 {
|
|
40 |
options.Status = http.StatusOK
|
|
41 |
}
|
|
42 |
|
|
43 |
w.WriteHeader(options.Status)
|
|
44 |
w.Write([]byte(options.Body))
|
|
45 |
}))
|
|
46 |
}
|
|
47 |
|
|
48 |
func TestBeforeAfterFuncs(t *testing.T) {
|
|
49 |
t.Parallel()
|
|
50 |
|
|
51 |
var tests = []struct {
|
|
52 |
name string
|
|
53 |
status int
|
|
54 |
body string
|
|
55 |
}{
|
|
56 |
{
|
|
57 |
name: "empty body",
|
|
58 |
body: "",
|
|
59 |
},
|
|
60 |
{
|
|
61 |
name: "empty body 500",
|
|
62 |
body: "",
|
|
63 |
status: 500,
|
|
64 |
},
|
|
65 |
|
|
66 |
{
|
|
67 |
name: "empty json body",
|
|
68 |
body: "{}",
|
|
69 |
},
|
|
70 |
{
|
|
71 |
name: "error",
|
|
72 |
body: `{"jsonrpc":"2.0","error":{"code":32603,"message":"Bad thing happened."}}`,
|
|
73 |
},
|
|
74 |
}
|
|
75 |
|
|
76 |
server := httptestServer(t)
|
|
77 |
defer server.Close()
|
|
78 |
|
|
79 |
testUrl, err := url.Parse(server.URL)
|
|
80 |
if err != nil {
|
|
81 |
t.Fatal(err)
|
|
82 |
}
|
|
83 |
|
|
84 |
for _, tt := range tests {
|
|
85 |
t.Run(tt.name, func(t *testing.T) {
|
|
86 |
beforeCalled := false
|
|
87 |
afterCalled := false
|
|
88 |
finalizerCalled := false
|
|
89 |
|
|
90 |
sut := jsonrpc.NewClient(
|
|
91 |
testUrl,
|
|
92 |
"dummy",
|
|
93 |
jsonrpc.ClientBefore(func(ctx context.Context, req *http.Request) context.Context {
|
|
94 |
beforeCalled = true
|
|
95 |
return ctx
|
|
96 |
}),
|
|
97 |
jsonrpc.ClientAfter(func(ctx context.Context, resp *http.Response) context.Context {
|
|
98 |
afterCalled = true
|
|
99 |
return ctx
|
|
100 |
}),
|
|
101 |
jsonrpc.ClientFinalizer(func(ctx context.Context, err error) {
|
|
102 |
finalizerCalled = true
|
|
103 |
}),
|
|
104 |
)
|
|
105 |
|
|
106 |
sut.Endpoint()(context.TODO(), testServerResponseOptions{Body: tt.body, Status: tt.status})
|
|
107 |
if !beforeCalled {
|
|
108 |
t.Fatal("Expected client before func to be called. Wasn't.")
|
|
109 |
}
|
|
110 |
if !afterCalled {
|
|
111 |
t.Fatal("Expected client after func to be called. Wasn't.")
|
|
112 |
}
|
|
113 |
if !finalizerCalled {
|
|
114 |
t.Fatal("Expected client finalizer func to be called. Wasn't.")
|
|
115 |
}
|
|
116 |
|
|
117 |
})
|
|
118 |
|
|
119 |
}
|
|
120 |
|
37 | 121 |
}
|
38 | 122 |
|
39 | 123 |
type staticIDGenerator int
|
|
41 | 125 |
func (g staticIDGenerator) Generate() interface{} { return g }
|
42 | 126 |
|
43 | 127 |
func TestClientHappyPath(t *testing.T) {
|
|
128 |
t.Parallel()
|
|
129 |
|
44 | 130 |
var (
|
45 | 131 |
afterCalledKey = "AC"
|
46 | 132 |
beforeHeaderKey = "BF"
|
|
91 | 177 |
w.WriteHeader(http.StatusOK)
|
92 | 178 |
w.Write([]byte(testbody))
|
93 | 179 |
}))
|
|
180 |
defer server.Close()
|
94 | 181 |
|
95 | 182 |
sut := jsonrpc.NewClient(
|
96 | 183 |
mustParse(server.URL),
|
|
152 | 239 |
}
|
153 | 240 |
|
154 | 241 |
func TestCanUseDefaults(t *testing.T) {
|
|
242 |
t.Parallel()
|
|
243 |
|
155 | 244 |
var (
|
156 | 245 |
testbody = `{"jsonrpc":"2.0", "result":"boogaloo"}`
|
157 | 246 |
requestBody []byte
|
|
167 | 256 |
w.WriteHeader(http.StatusOK)
|
168 | 257 |
w.Write([]byte(testbody))
|
169 | 258 |
}))
|
|
259 |
defer server.Close()
|
170 | 260 |
|
171 | 261 |
sut := jsonrpc.NewClient(
|
172 | 262 |
mustParse(server.URL),
|
|
209 | 299 |
}
|
210 | 300 |
|
211 | 301 |
func TestClientCanHandleJSONRPCError(t *testing.T) {
|
|
302 |
t.Parallel()
|
|
303 |
|
212 | 304 |
var testbody = `{
|
213 | 305 |
"jsonrpc": "2.0",
|
214 | 306 |
"error": {
|
|
220 | 312 |
w.WriteHeader(http.StatusOK)
|
221 | 313 |
w.Write([]byte(testbody))
|
222 | 314 |
}))
|
|
315 |
defer server.Close()
|
223 | 316 |
|
224 | 317 |
sut := jsonrpc.NewClient(mustParse(server.URL), "add")
|
225 | 318 |
|
|
254 | 347 |
}
|
255 | 348 |
|
256 | 349 |
func TestDefaultAutoIncrementer(t *testing.T) {
|
|
350 |
t.Parallel()
|
|
351 |
|
257 | 352 |
sut := jsonrpc.NewAutoIncrementID(0)
|
258 | 353 |
var want uint64
|
259 | 354 |
for ; want < 100; want++ {
|