// TestCmdMethod tests the CmdMethod function to ensure it retuns the expected // methods and errors. func TestCmdMethod(t *testing.T) { t.Parallel() tests := []struct { name string cmd interface{} method string err error }{ { name: "unregistered type", cmd: (*int)(nil), err: btcjson.Error{ErrorCode: btcjson.ErrUnregisteredMethod}, }, { name: "nil pointer of registered type", cmd: (*btcjson.GetBlockCmd)(nil), method: "getblock", }, { name: "nil instance of registered type", cmd: &btcjson.GetBlockCountCmd{}, method: "getblockcount", }, } t.Logf("Running %d tests", len(tests)) for i, test := range tests { method, err := btcjson.CmdMethod(test.cmd) if reflect.TypeOf(err) != reflect.TypeOf(test.err) { t.Errorf("Test #%d (%s) wrong error - got %T (%[3]v), "+ "want %T", i, test.name, err, test.err) continue } if err != nil { gotErrorCode := err.(btcjson.Error).ErrorCode if gotErrorCode != test.err.(btcjson.Error).ErrorCode { t.Errorf("Test #%d (%s) mismatched error code "+ "- got %v (%v), want %v", i, test.name, gotErrorCode, err, test.err.(btcjson.Error).ErrorCode) continue } continue } // Ensure method matches the expected value. if method != test.method { t.Errorf("Test #%d (%s) mismatched method - got %v, "+ "want %v", i, test.name, method, test.method) continue } } }
// sendCmd sends the passed command to the associated server and returns a // response channel on which the reply will be delivered at some point in the // future. It handles both websocket and HTTP POST mode depending on the // configuration of the client. func (c *Client) sendCmd(cmd interface{}) chan *response { // Get the method associated with the command. method, err := btcjson.CmdMethod(cmd) if err != nil { return newFutureError(err) } // Marshal the command. id := c.NextID() marshalledJSON, err := btcjson.MarshalCmd(id, cmd) if err != nil { return newFutureError(err) } // Generate the request and send it along with a channel to respond on. responseChan := make(chan *response, 1) jReq := &jsonRequest{ id: id, method: method, cmd: cmd, marshalledJSON: marshalledJSON, responseChan: responseChan, } c.sendRequest(jReq) return responseChan }