Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

feat(prover): use httptest.Server to simplify the prover web server test code #389

Merged
merged 12 commits into from
Sep 28, 2023
Merged
55 changes: 29 additions & 26 deletions prover/server/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,51 @@ package server
import (
"crypto/rand"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"time"

"github.com/cyberhorsey/webutils/testutils"
"github.com/ethereum/go-ethereum/common"
"github.com/labstack/echo/v4"
"github.com/taikoxyz/taiko-client/bindings/encoding"
)

func (s *ProverServerTestSuite) TestGetStatusSuccess() {
rec := s.sendReq("/status")
s.Equal(http.StatusOK, rec.Code)
resp := s.sendReq("/status")
s.Equal(http.StatusOK, resp.StatusCode)

status := new(Status)
s.Nil(json.Unmarshal(rec.Body.Bytes(), &status))

s.Equal(s.srv.minProofFee.Uint64(), status.MinProofFee)
s.Equal(uint64(s.srv.maxExpiry.Seconds()), status.MaxExpiry)
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
s.Nil(err)
s.Nil(json.Unmarshal(b, &status))

s.Equal(s.ps.minProofFee.Uint64(), status.MinProofFee)
s.Equal(uint64(s.ps.maxExpiry.Seconds()), status.MaxExpiry)
s.Greater(status.CurrentCapacity, uint64(0))
}

func (s *ProverServerTestSuite) TestProposeBlockSuccess() {
rec := httptest.NewRecorder()

s.srv.ServeHTTP(rec, testutils.NewUnauthenticatedRequest(
echo.POST,
"/assignment",
&encoding.ProposeBlockData{
Fee: common.Big256,
Expiry: uint64(time.Now().Add(time.Minute).Unix()),
Input: encoding.TaikoL1BlockMetadataInput{
Proposer: common.BytesToAddress(randomHash().Bytes()),
TxListHash: randomHash(),
TxListByteStart: common.Big0,
TxListByteEnd: common.Big0,
CacheTxListInfo: false,
},
data, err := json.Marshal(encoding.ProposeBlockData{
Fee: common.Big256,
Expiry: uint64(time.Now().Add(time.Minute).Unix()),
Input: encoding.TaikoL1BlockMetadataInput{
Proposer: common.BytesToAddress(randomHash().Bytes()),
TxListHash: randomHash(),
TxListByteStart: common.Big0,
TxListByteEnd: common.Big0,
CacheTxListInfo: false,
},
))

testutils.AssertStatusAndBody(s.T(), rec, http.StatusOK, []string{"signedPayload"})
})
s.Nil(err)
resp, err := http.Post(s.ws.URL+"/assignment", "application/json", strings.NewReader(string(data)))
s.Nil(err)
s.Equal(http.StatusOK, resp.StatusCode)
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
s.Nil(err)
s.Contains(string(b), "signedPayload")
}

// randomHash generates a random blob of data and returns it as a hash.
Expand Down
5 changes: 0 additions & 5 deletions prover/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@ func (srv *ProverServer) Shutdown(ctx context.Context) error {
return srv.echo.Shutdown(ctx)
}

// ServeHTTP implements the `http.Handler` interface which serves HTTP requests.
func (srv *ProverServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
davidtaikocha marked this conversation as resolved.
Show resolved Hide resolved
srv.echo.ServeHTTP(w, r)
}

// Health endpoints for probes.
func (srv *ProverServer) Health(c echo.Context) error {
return c.NoContent(http.StatusOK)
Expand Down
35 changes: 16 additions & 19 deletions prover/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,35 @@ import (

type ProverServerTestSuite struct {
suite.Suite
srv *ProverServer
ps *ProverServer
ws *httptest.Server // web server
}

func (s *ProverServerTestSuite) SetupTest() {
l1ProverPrivKey, err := crypto.ToECDSA(common.Hex2Bytes(os.Getenv("L1_PROVER_PRIVATE_KEY")))
s.Nil(err)

srv := &ProverServer{
p := &ProverServer{
echo: echo.New(),
proverPrivateKey: l1ProverPrivKey,
minProofFee: common.Big1,
maxExpiry: 24 * time.Hour,
capacityManager: capacity.New(1024),
}

srv.echo.HideBanner = true
srv.configureMiddleware()
srv.configureRoutes()

s.srv = srv
p.echo.HideBanner = true
p.configureMiddleware()
p.configureRoutes()
s.ps = p
s.ws = httptest.NewServer(p.echo)
}

func (s *ProverServerTestSuite) TestHealth() {
s.Equal(http.StatusOK, s.sendReq("/healthz").Code)
s.Equal(http.StatusOK, s.sendReq("/healthz").StatusCode)
}

func (s *ProverServerTestSuite) TestRoot() {
s.Equal(http.StatusOK, s.sendReq("/").Code)
s.Equal(http.StatusOK, s.sendReq("/").StatusCode)
}

func (s *ProverServerTestSuite) TestStartShutdown() {
Expand All @@ -61,7 +62,7 @@ func (s *ProverServerTestSuite) TestStartShutdown() {
s.Nil(err)

go func() {
if err := s.srv.Start(fmt.Sprintf(":%v", port)); err != nil {
if err := s.ps.Start(fmt.Sprintf(":%v", port)); err != nil {
log.Error("Failed to start prover server", "error", err)
}
}()
Expand All @@ -79,23 +80,19 @@ func (s *ProverServerTestSuite) TestStartShutdown() {
return nil
}, backoff.NewExponentialBackOff()))

s.Nil(s.srv.Shutdown(context.Background()))
s.Nil(s.ps.Shutdown(context.Background()))
}

func (s *ProverServerTestSuite) TearDownTest() {
s.Nil(s.srv.Shutdown(context.Background()))
s.ws.Close()
}

func TestProverServerTestSuite(t *testing.T) {
suite.Run(t, new(ProverServerTestSuite))
}

func (s *ProverServerTestSuite) sendReq(path string) *httptest.ResponseRecorder {
req, err := http.NewRequest(echo.GET, path, nil)
func (s *ProverServerTestSuite) sendReq(path string) *http.Response {
resp, err := http.Get(s.ws.URL + path)
s.Nil(err)
rec := httptest.NewRecorder()

s.srv.ServeHTTP(rec, req)

return rec
return resp
}