Skip to content

Commit

Permalink
refactored rpctest
Browse files Browse the repository at this point in the history
- package `rpctest` is not internal any more
- improved doc
  • Loading branch information
lmittmann committed Feb 6, 2022
1 parent ae8221e commit 799870d
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 16 deletions.
2 changes: 1 addition & 1 deletion module/eth/chain_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"testing"

"github.com/lmittmann/w3"
"github.com/lmittmann/w3/internal/rpctest"
"github.com/lmittmann/w3/module/eth"
"github.com/lmittmann/w3/rpctest"
)

func TestChainID(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion module/eth/gas_price_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"testing"

"github.com/lmittmann/w3"
"github.com/lmittmann/w3/internal/rpctest"
"github.com/lmittmann/w3/module/eth"
"github.com/lmittmann/w3/rpctest"
)

func TestGasPrice(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion module/eth/get_balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"testing"

"github.com/lmittmann/w3"
"github.com/lmittmann/w3/internal/rpctest"
"github.com/lmittmann/w3/module/eth"
"github.com/lmittmann/w3/rpctest"
)

func TestBalance(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion module/eth/get_block_by_hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/lmittmann/w3"
"github.com/lmittmann/w3/internal/rpctest"
"github.com/lmittmann/w3/module/eth"
"github.com/lmittmann/w3/rpctest"
)

func TestBlockByHash__1(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion module/eth/get_block_by_number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/lmittmann/w3"
"github.com/lmittmann/w3/internal/rpctest"
"github.com/lmittmann/w3/module/eth"
"github.com/lmittmann/w3/rpctest"
)

func TestBlockByNumber__1(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion module/eth/get_code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"testing"

"github.com/lmittmann/w3"
"github.com/lmittmann/w3/internal/rpctest"
"github.com/lmittmann/w3/module/eth"
"github.com/lmittmann/w3/rpctest"
)

func TestCode(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion module/eth/get_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/google/go-cmp/cmp"
"github.com/lmittmann/w3"
"github.com/lmittmann/w3/internal/rpctest"
"github.com/lmittmann/w3/module/eth"
"github.com/lmittmann/w3/rpctest"
)

func TestLogs(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion module/eth/get_storage_at_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/lmittmann/w3"
"github.com/lmittmann/w3/internal/rpctest"
"github.com/lmittmann/w3/module/eth"
"github.com/lmittmann/w3/rpctest"
)

func TestStorageAt(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion module/eth/get_transaction_by_hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/lmittmann/w3"
"github.com/lmittmann/w3/internal/rpctest"
"github.com/lmittmann/w3/module/eth"
"github.com/lmittmann/w3/rpctest"
)

func TestTransactionByHash_Type0(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion module/eth/get_transaction_count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"testing"

"github.com/lmittmann/w3"
"github.com/lmittmann/w3/internal/rpctest"
"github.com/lmittmann/w3/module/eth"
"github.com/lmittmann/w3/rpctest"
)

func TestNonce(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion module/eth/get_transaction_receipt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/lmittmann/w3"
"github.com/lmittmann/w3/internal/rpctest"
"github.com/lmittmann/w3/module/eth"
"github.com/lmittmann/w3/rpctest"
)

func TestTransactionReceipt(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion module/eth/send_raw_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/lmittmann/w3"
"github.com/lmittmann/w3/internal/rpctest"
"github.com/lmittmann/w3/module/eth"
"github.com/lmittmann/w3/rpctest"
)

func TestSendTransaction(t *testing.T) {
Expand Down
25 changes: 21 additions & 4 deletions internal/rpctest/server.go → rpctest/server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package rpctest provides utilities for RPC testing.
// Package rpctest provides utilities for testing RPC methods.
package rpctest

import (
Expand All @@ -13,6 +13,16 @@ import (
"testing"
)

// Server is a fake RPC endpoint that responds only to a single requests that
// is definded in a golden-file.
//
// Request golden-files have the following format to define a single request
// and the corresponding response:s
// // Comments and empty lines will be ignored.
// // Request starts with ">".
// > [{"jsonrpc":"2.0","id":1,"method":"eth_chainId"}]
// // Response starts with "<".
// < [{"jsonrpc":"2.0","id":1,"result":"0x1"}]
type Server struct {
t *testing.T

Expand All @@ -24,6 +34,8 @@ type Server struct {
httptestSrv *httptest.Server
}

// NewServer returns a new instance of Server that serves the golden-file from
// Reader r.
func NewServer(t *testing.T, r io.Reader) *Server {
srv := &Server{t: t, reader: r}
httptestSrv := httptest.NewServer(srv)
Expand All @@ -32,6 +44,8 @@ func NewServer(t *testing.T, r io.Reader) *Server {
return srv
}

// NewFileServer returns a new instance of Server that serves the golden-file
// from the given filename.
func NewFileServer(t *testing.T, filename string) *Server {
f, err := os.Open(filename)
if err != nil {
Expand Down Expand Up @@ -60,18 +74,21 @@ func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write(srv.out)
}

// URL returns the servers RPC endpoint url.
func (srv *Server) URL() string {
return srv.httptestSrv.URL
}

// Close shuts down the server.
func (srv *Server) Close() {
srv.httptestSrv.Close()
if rc, ok := srv.reader.(io.ReadCloser); ok {
rc.Close()
}
}

func (srv *Server) readGolden() {
if rc, ok := srv.reader.(io.ReadCloser); ok {
defer rc.Close()
}

scan := bufio.NewScanner(srv.reader)
for scan.Scan() {
line := scan.Bytes()
Expand Down

0 comments on commit 799870d

Please sign in to comment.