Skip to content

Commit

Permalink
Fix postflight hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
anbsky committed Sep 25, 2020
1 parent c097654 commit d1f79c2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/query/caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (c *Caller) AddPreflightHook(method string, hf Hook, name string) {
// allowing to amend the response before it gets sent back to the client
// or to modify log entry fields.
func (c *Caller) AddPostflightHook(method string, hf Hook, name string) {
c.postflightHooks = append(c.preflightHooks, hookEntry{method, hf, name})
c.postflightHooks = append(c.postflightHooks, hookEntry{method, hf, name})
logger.Log().Debugf("added a postflight hook for method %v", method)
}

Expand Down
31 changes: 31 additions & 0 deletions app/query/caller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,37 @@ func TestCaller_AddPostflightHook_LogField(t *testing.T) {
assert.Equal(t, "8.8.8.8", logHook.LastEntry().Data["remote_ip"])
}

func TestCaller_CloneWithoutHook(t *testing.T) {
timesCalled := 0
call := func() {
timesCalled++
}

reqChan := test.ReqChan()
srv := test.MockHTTPServer(reqChan)
defer srv.Close()

c := NewCaller(srv.URL, 0)
srv.QueueResponses(resolveResponseWithoutPurchase, resolveResponseWithoutPurchase)

c.AddPostflightHook(MethodResolve, func(c *Caller, hctx *HookContext) (*jsonrpc.RPCResponse, error) {
call()
return nil, nil
}, "")

c.AddPostflightHook(MethodResolve, func(c *Caller, hctx *HookContext) (*jsonrpc.RPCResponse, error) {
// This will be cloned without the current hook but the previous one should increment `timesCalled` once again
cc := c.CloneWithoutHook(c.Endpoint(), MethodResolve, "lbrynext_resolve")
_, err := cc.SendQuery(hctx.Query)
assert.NoError(t, err)
return nil, nil
}, "lbrynext_resolve")

_, err := c.Call(jsonrpc.NewRequest(MethodResolve, map[string]interface{}{"urls": "what:19b9c243bea0c45175e6a6027911abbad53e983e"}))
require.NoError(t, err)
assert.Equal(t, timesCalled, 2)
}

func TestCaller_CallSDKError(t *testing.T) {
srv := test.MockHTTPServer(nil)
defer srv.Close()
Expand Down
11 changes: 6 additions & 5 deletions internal/lbrynext/lbrynext.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,12 @@ func stripFieldsFromMap(m map[string]interface{}) map[string]interface{} {
return m
}

func stripFieldsFromResponse(rsp jsonrpc.RPCResponse) *jsonrpc.RPCResponse {
func stripFieldsFromResponse(rsp *jsonrpc.RPCResponse) *jsonrpc.RPCResponse {
rspMod := rsp
if resultMap, ok := rsp.Result.(map[string]interface{}); ok {
rsp.Result = stripFieldsFromMap(resultMap)
rspMod.Result = stripFieldsFromMap(resultMap)
}
return &rsp
return rspMod
}

func rspToByte(rsp *jsonrpc.RPCResponse) []byte {
Expand All @@ -172,8 +173,8 @@ func rspToByte(rsp *jsonrpc.RPCResponse) []byte {
}

func compareResponses(r, xr *jsonrpc.RPCResponse) (string, string, string) {
rStripped := stripFieldsFromResponse(*r)
xrStripped := stripFieldsFromResponse(*xr)
rStripped := stripFieldsFromResponse(r)
xrStripped := stripFieldsFromResponse(xr)
_, diffLog := test.GetJSONDiffLog(rspToByte(rStripped), rspToByte(xrStripped))
return string(rspToByte(r)), string(rspToByte(xr)), diffLog
}

0 comments on commit d1f79c2

Please sign in to comment.