From 3eb6d266cab89efacdb6cf8f05c7ffa8e5bcefcf Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 29 Apr 2020 15:14:56 +0300 Subject: [PATCH] rpc/server: use httptest.Server for testing Which allows to reuse it for websockets. --- pkg/rpc/server/server_helper_test.go | 9 ++++++--- pkg/rpc/server/server_test.go | 29 ++++++++++++++-------------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/pkg/rpc/server/server_helper_test.go b/pkg/rpc/server/server_helper_test.go index 9b99a6505..c6ee3167e 100644 --- a/pkg/rpc/server/server_helper_test.go +++ b/pkg/rpc/server/server_helper_test.go @@ -2,6 +2,7 @@ package server import ( "net/http" + "net/http/httptest" "os" "testing" @@ -17,7 +18,7 @@ import ( "go.uber.org/zap/zaptest" ) -func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, http.HandlerFunc) { +func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, *httptest.Server) { var nBlocks uint32 net := config.ModeUnitTestNet @@ -55,9 +56,11 @@ func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, http.HandlerFu server, err := network.NewServer(serverConfig, chain, logger) require.NoError(t, err) rpcServer := New(chain, cfg.ApplicationConfiguration.RPC, server, logger) - handler := http.HandlerFunc(rpcServer.handleHTTPRequest) - return chain, handler + handler := http.HandlerFunc(rpcServer.handleHTTPRequest) + srv := httptest.NewServer(handler) + + return chain, srv } type FeerStub struct{} diff --git a/pkg/rpc/server/server_test.go b/pkg/rpc/server/server_test.go index 1ffd72ed9..830bf895e 100644 --- a/pkg/rpc/server/server_test.go +++ b/pkg/rpc/server/server_test.go @@ -12,6 +12,7 @@ import ( "strconv" "strings" "testing" + "time" "github.com/nspcc-dev/neo-go/pkg/core" "github.com/nspcc-dev/neo-go/pkg/core/transaction" @@ -28,7 +29,7 @@ import ( type executor struct { chain *core.Blockchain - handler http.HandlerFunc + httpSrv *httptest.Server } const ( @@ -881,18 +882,18 @@ var rpcTestCases = map[string][]rpcTestCase{ } func TestRPC(t *testing.T) { - chain, handler := initServerWithInMemoryChain(t) + chain, httpSrv := initServerWithInMemoryChain(t) defer chain.Close() - e := &executor{chain: chain, handler: handler} + e := &executor{chain: chain, httpSrv: httpSrv} for method, cases := range rpcTestCases { t.Run(method, func(t *testing.T) { rpc := `{"jsonrpc": "2.0", "id": 1, "method": "%s", "params": %s}` for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { - body := doRPCCall(fmt.Sprintf(rpc, method, tc.params), handler, t) + body := doRPCCall(fmt.Sprintf(rpc, method, tc.params), httpSrv.URL, t) result := checkErrGetResult(t, body, tc.fail) if tc.fail { return @@ -916,7 +917,7 @@ func TestRPC(t *testing.T) { block, _ := chain.GetBlock(chain.GetHeaderHash(0)) TXHash := block.Transactions[1].Hash() rpc := fmt.Sprintf(`{"jsonrpc": "2.0", "id": 1, "method": "getrawtransaction", "params": ["%s"]}"`, TXHash.StringLE()) - body := doRPCCall(rpc, handler, t) + body := doRPCCall(rpc, httpSrv.URL, t) result := checkErrGetResult(t, body, false) var res string err := json.Unmarshal(result, &res) @@ -928,7 +929,7 @@ func TestRPC(t *testing.T) { block, _ := chain.GetBlock(chain.GetHeaderHash(0)) TXHash := block.Transactions[1].Hash() rpc := fmt.Sprintf(`{"jsonrpc": "2.0", "id": 1, "method": "getrawtransaction", "params": ["%s", 0]}"`, TXHash.StringLE()) - body := doRPCCall(rpc, handler, t) + body := doRPCCall(rpc, httpSrv.URL, t) result := checkErrGetResult(t, body, false) var res string err := json.Unmarshal(result, &res) @@ -940,7 +941,7 @@ func TestRPC(t *testing.T) { block, _ := chain.GetBlock(chain.GetHeaderHash(0)) TXHash := block.Transactions[1].Hash() rpc := fmt.Sprintf(`{"jsonrpc": "2.0", "id": 1, "method": "getrawtransaction", "params": ["%s", 1]}"`, TXHash.StringLE()) - body := doRPCCall(rpc, handler, t) + body := doRPCCall(rpc, httpSrv.URL, t) txOut := checkErrGetResult(t, body, false) actual := result.TransactionOutputRaw{} err := json.Unmarshal(txOut, &actual) @@ -966,7 +967,7 @@ func TestRPC(t *testing.T) { tx := block.Transactions[3] rpc := fmt.Sprintf(`{"jsonrpc": "2.0", "id": 1, "method": "gettxout", "params": [%s, %d]}"`, `"`+tx.Hash().StringLE()+`"`, 0) - body := doRPCCall(rpc, handler, t) + body := doRPCCall(rpc, httpSrv.URL, t) res := checkErrGetResult(t, body, false) var txOut result.TransactionOutput @@ -997,7 +998,7 @@ func TestRPC(t *testing.T) { } rpc := `{"jsonrpc": "2.0", "id": 1, "method": "getrawmempool", "params": []}` - body := doRPCCall(rpc, handler, t) + body := doRPCCall(rpc, httpSrv.URL, t) res := checkErrGetResult(t, body, false) var actual []util.Uint256 @@ -1027,12 +1028,10 @@ func checkErrGetResult(t *testing.T, body []byte, expectingFail bool) json.RawMe return resp.Result } -func doRPCCall(rpcCall string, handler http.HandlerFunc, t *testing.T) []byte { - req := httptest.NewRequest("POST", "http://0.0.0.0:20333/", strings.NewReader(rpcCall)) - req.Header.Set("Content-Type", "application/json") - w := httptest.NewRecorder() - handler(w, req) - resp := w.Result() +func doRPCCall(rpcCall string, url string, t *testing.T) []byte { + cl := http.Client{Timeout: time.Second} + resp, err := cl.Post(url, "application/json", strings.NewReader(rpcCall)) + require.NoErrorf(t, err, "could not make a POST request") body, err := ioutil.ReadAll(resp.Body) assert.NoErrorf(t, err, "could not read response from the request: %s", rpcCall) return bytes.TrimSpace(body)