mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-04 19:19:44 +00:00
rpc/server: use httptest.Server for testing
Which allows to reuse it for websockets.
This commit is contained in:
parent
3d2a2af575
commit
3eb6d266ca
2 changed files with 20 additions and 18 deletions
|
@ -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{}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue