Codebase list golang-github-go-kit-kit / 6860cdd
Move the ClientAfter calls to before the decode (#1008) * Move the ClientAfter calls to before the decode ClientAfter is documented as running _prior_ to being decoded. But, this was only partly true. They were run prior to decode, but after the jsonrpc unmarshalling, As the callback uses the bare response object, and at least for me, is used to debug what's seen on the wire, this seems incorrect. * tests seph authored 3 years ago GitHub committed 3 years ago
2 changed file(s) with 116 addition(s) and 21 deletion(s). Raw diff Collapse all Expand all
195195 defer resp.Body.Close()
196196 }
197197
198 for _, f := range c.after {
199 ctx = f(ctx, resp)
200 }
201
198202 // Decode the body into an object
199203 var rpcRes Response
200204 err = json.NewDecoder(resp.Body).Decode(&rpcRes)
201205 if err != nil {
202206 return nil, err
203 }
204
205 for _, f := range c.after {
206 ctx = f(ctx, resp)
207207 }
208208
209209 return c.dec(ctx, rpcRes)
1717 String string
1818 }
1919
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
37121 }
38122
39123 type staticIDGenerator int
41125 func (g staticIDGenerator) Generate() interface{} { return g }
42126
43127 func TestClientHappyPath(t *testing.T) {
128 t.Parallel()
129
44130 var (
45131 afterCalledKey = "AC"
46132 beforeHeaderKey = "BF"
91177 w.WriteHeader(http.StatusOK)
92178 w.Write([]byte(testbody))
93179 }))
180 defer server.Close()
94181
95182 sut := jsonrpc.NewClient(
96183 mustParse(server.URL),
152239 }
153240
154241 func TestCanUseDefaults(t *testing.T) {
242 t.Parallel()
243
155244 var (
156245 testbody = `{"jsonrpc":"2.0", "result":"boogaloo"}`
157246 requestBody []byte
167256 w.WriteHeader(http.StatusOK)
168257 w.Write([]byte(testbody))
169258 }))
259 defer server.Close()
170260
171261 sut := jsonrpc.NewClient(
172262 mustParse(server.URL),
209299 }
210300
211301 func TestClientCanHandleJSONRPCError(t *testing.T) {
302 t.Parallel()
303
212304 var testbody = `{
213305 "jsonrpc": "2.0",
214306 "error": {
220312 w.WriteHeader(http.StatusOK)
221313 w.Write([]byte(testbody))
222314 }))
315 defer server.Close()
223316
224317 sut := jsonrpc.NewClient(mustParse(server.URL), "add")
225318
254347 }
255348
256349 func TestDefaultAutoIncrementer(t *testing.T) {
350 t.Parallel()
351
257352 sut := jsonrpc.NewAutoIncrementID(0)
258353 var want uint64
259354 for ; want < 100; want++ {