From 84eedaedfe01e736d7c6a8523e68c4fad878e8c4 Mon Sep 17 00:00:00 2001 From: alexshliu <104080237+alexshliu@users.noreply.github.com> Date: Thu, 28 Sep 2023 15:03:26 +0800 Subject: [PATCH] feat(prover): use `httptest.Server` to simplify the prover server tests (#389) Co-authored-by: Roger <50648015+RogerLamTd@users.noreply.github.com> Co-authored-by: RogerLamTd Co-authored-by: David --- prover/server/api_test.go | 55 +++++++++++++++++++----------------- prover/server/server.go | 5 ---- prover/server/server_test.go | 35 +++++++++++------------ 3 files changed, 45 insertions(+), 50 deletions(-) diff --git a/prover/server/api_test.go b/prover/server/api_test.go index 32af6dacc..866c543f7 100644 --- a/prover/server/api_test.go +++ b/prover/server/api_test.go @@ -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. diff --git a/prover/server/server.go b/prover/server/server.go index b6de95392..ac8141190 100644 --- a/prover/server/server.go +++ b/prover/server/server.go @@ -86,11 +86,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) { - srv.echo.ServeHTTP(w, r) -} - // Health endpoints for probes. func (srv *ProverServer) Health(c echo.Context) error { return c.NoContent(http.StatusOK) diff --git a/prover/server/server_test.go b/prover/server/server_test.go index 781ac3416..74a513faa 100644 --- a/prover/server/server_test.go +++ b/prover/server/server_test.go @@ -24,7 +24,8 @@ import ( type ProverServerTestSuite struct { suite.Suite - srv *ProverServer + ps *ProverServer + ws *httptest.Server // web server } func (s *ProverServerTestSuite) SetupTest() { @@ -45,7 +46,7 @@ func (s *ProverServerTestSuite) SetupTest() { }) s.Nil(err) - srv := &ProverServer{ + p := &ProverServer{ echo: echo.New(), proverPrivateKey: l1ProverPrivKey, minProofFee: common.Big1, @@ -57,19 +58,19 @@ func (s *ProverServerTestSuite) SetupTest() { isOracle: false, } - 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() { @@ -80,7 +81,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) } }() @@ -98,23 +99,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 }