Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added UTs to "rpc" package #48

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 1 addition & 21 deletions rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,11 @@ func JSONRPCCall(url, method string, params ...interface{}) (Response, error) {
// the provided method and parameters, which is compatible with the Ethereum
// JSON RPC Server.
func JSONRPCCallWithContext(ctx context.Context, url, method string, parameters ...interface{}) (Response, error) {
params, err := json.Marshal(parameters)
httpReq, err := BuildJsonHTTPRequest(ctx, url, method, parameters...)
if err != nil {
return Response{}, err
}

req := Request{
JSONRPC: "2.0",
ID: float64(1),
Method: method,
Params: params,
}

reqBody, err := json.Marshal(req)
if err != nil {
return Response{}, err
}

reqBodyReader := bytes.NewReader(reqBody)
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, reqBodyReader)
if err != nil {
return Response{}, err
}

httpReq.Header.Add("Content-type", "application/json")

httpRes, err := http.DefaultClient.Do(httpReq)
if err != nil {
return Response{}, err
Expand Down
66 changes: 66 additions & 0 deletions rpc/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package rpc

import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"
)

func Test_JSONRPCCallWithContext(t *testing.T) {
tests := []struct {
name string
result string
resp Response
statusCode int
err error
}{
{
name: "successfully executed request",
result: `{"result":"test"}`,
resp: Response{
Result: json.RawMessage(`"test"`),
},
},
{
name: "unsuccessful status code returned by server",
statusCode: http.StatusUnauthorized,
err: errors.New("invalid status code, expected: 200, found: 401"),
},
}
for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var res Request
require.NoError(t, json.NewDecoder(r.Body).Decode(&res))
require.Equal(t, "test", res.Method)

if tt.statusCode > 0 {
w.WriteHeader(tt.statusCode)
}

_, err := fmt.Fprint(w, tt.result)
require.NoError(t, err)
}))
defer svr.Close()

got, err := JSONRPCCallWithContext(context.Background(), svr.URL, "test")
if tt.err != nil {
require.Error(t, err)
require.EqualError(t, tt.err, err.Error())
} else {
require.NoError(t, err)
require.Equal(t, tt.resp, got)
}
})
}
}
1 change: 1 addition & 0 deletions rpc/dbtxmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type DBTxScopedFn func(ctx context.Context, dbTx db.Tx) (interface{}, Error)
// NewDbTxScope function to initiate DB scopped txs
func (f *DBTxManager) NewDbTxScope(db db.DB, scopedFn DBTxScopedFn) (interface{}, Error) {
ctx := context.Background()

dbTx, err := db.BeginStateTransaction(ctx)
if err != nil {
return RPCErrorResponse(DefaultErrorCode, "failed to connect to the state", err)
Expand Down