Skip to content

Commit

Permalink
tests(handlers): rename servers_test.go -> handlers_test.go + some sm…
Browse files Browse the repository at this point in the history
…all refactors
  • Loading branch information
samlaf committed Nov 1, 2024
1 parent c479458 commit 28bb62c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"go.testFlags": [
"-test.parallel",
"4",
// Comment the following 2 lines to run unit tests.
"-deploy-config",
"../.devnet/devnetL1.json"
]
Expand Down
37 changes: 21 additions & 16 deletions server/server_test.go → server/handlers_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package server

// The tests in this file test not only the handlers but also the middlewares,
// because server.registerRoutes(r) registers the handlers wrapped with middlewares.

import (
"bytes"
"fmt"
Expand Down Expand Up @@ -27,11 +30,7 @@ const (
func TestHandleOPCommitments(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockRouter := mocks.NewMockIManager(ctrl)

m := metrics.NewMetrics("default")
server := NewServer("localhost", 8080, mockRouter, log.New(), m)
mockStorageMgr := mocks.NewMockIManager(ctrl)

tests := []struct {
name string
Expand All @@ -44,7 +43,7 @@ func TestHandleOPCommitments(t *testing.T) {
name: "Failure - OP Keccak256 Internal Server Error",
url: fmt.Sprintf("/get/0x00%s", testCommitStr),
mockBehavior: func() {
mockRouter.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("internal error"))
mockStorageMgr.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("internal error"))
},
expectedCode: http.StatusInternalServerError,
expectedBody: "",
Expand All @@ -53,7 +52,7 @@ func TestHandleOPCommitments(t *testing.T) {
name: "Success - OP Keccak256",
url: fmt.Sprintf("/get/0x00%s", testCommitStr),
mockBehavior: func() {
mockRouter.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return([]byte(testCommitStr), nil)
mockStorageMgr.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return([]byte(testCommitStr), nil)
},
expectedCode: http.StatusOK,
expectedBody: testCommitStr,
Expand All @@ -62,7 +61,7 @@ func TestHandleOPCommitments(t *testing.T) {
name: "Failure - OP Alt-DA Internal Server Error",
url: fmt.Sprintf("/get/0x010000%s", testCommitStr),
mockBehavior: func() {
mockRouter.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("internal error"))
mockStorageMgr.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("internal error"))
},
expectedCode: http.StatusInternalServerError,
expectedBody: "",
Expand All @@ -71,7 +70,7 @@ func TestHandleOPCommitments(t *testing.T) {
name: "Success - OP Alt-DA",
url: fmt.Sprintf("/get/0x010000%s", testCommitStr),
mockBehavior: func() {
mockRouter.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return([]byte(testCommitStr), nil)
mockStorageMgr.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return([]byte(testCommitStr), nil)
},
expectedCode: http.StatusOK,
expectedBody: testCommitStr,
Expand All @@ -88,6 +87,10 @@ func TestHandleOPCommitments(t *testing.T) {
// To add the vars to the context,
// we need to create a router through which we can pass the request.
r := mux.NewRouter()
// enable this logger to help debug tests
// logger := log.NewLogger(log.NewTerminalHandler(os.Stderr, true)).With("test_name", t.Name())
noopLogger := log.NewLogger(log.DiscardHandler())
server := NewServer("localhost", 0, mockStorageMgr, noopLogger, metrics.NoopMetrics)
server.registerRoutes(r)
r.ServeHTTP(rec, req)

Expand All @@ -103,9 +106,7 @@ func TestHandleOPCommitments(t *testing.T) {
func TestHandlerPut(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockRouter := mocks.NewMockIManager(ctrl)
server := NewServer("localhost", 8080, mockRouter, log.New(), metrics.NoopMetrics)
mockStorageMgr := mocks.NewMockIManager(ctrl)

tests := []struct {
name string
Expand All @@ -121,7 +122,7 @@ func TestHandlerPut(t *testing.T) {
url: "/put",
body: []byte("some data that will trigger an internal error"),
mockBehavior: func() {
mockRouter.EXPECT().Put(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("internal error"))
mockStorageMgr.EXPECT().Put(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("internal error"))
},
expectedCode: http.StatusInternalServerError,
expectedBody: "",
Expand All @@ -132,7 +133,7 @@ func TestHandlerPut(t *testing.T) {
url: "/put",
body: []byte("some data that will successfully be written to EigenDA"),
mockBehavior: func() {
mockRouter.EXPECT().Put(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return([]byte(testCommitStr), nil)
mockStorageMgr.EXPECT().Put(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return([]byte(testCommitStr), nil)
},
expectedCode: http.StatusOK,
expectedBody: opGenericPrefixStr + testCommitStr,
Expand All @@ -143,7 +144,7 @@ func TestHandlerPut(t *testing.T) {
url: fmt.Sprintf("/put/0x00%s", testCommitStr),
body: []byte("some data that will successfully be written to EigenDA"),
mockBehavior: func() {
mockRouter.EXPECT().Put(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return([]byte(testCommitStr), nil)
mockStorageMgr.EXPECT().Put(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return([]byte(testCommitStr), nil)
},
expectedCode: http.StatusOK,
expectedBody: "",
Expand All @@ -154,7 +155,7 @@ func TestHandlerPut(t *testing.T) {
url: "/put?commitment_mode=simple",
body: []byte("some data that will successfully be written to EigenDA"),
mockBehavior: func() {
mockRouter.EXPECT().Put(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return([]byte(testCommitStr), nil)
mockStorageMgr.EXPECT().Put(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return([]byte(testCommitStr), nil)
},
expectedCode: http.StatusOK,
expectedBody: simpleCommitmentPrefix + testCommitStr,
Expand All @@ -172,6 +173,10 @@ func TestHandlerPut(t *testing.T) {
// To add the vars to the context,
// we need to create a router through which we can pass the request.
r := mux.NewRouter()
// enable this logger to help debug tests
// logger := log.NewLogger(log.NewTerminalHandler(os.Stderr, true)).With("test_name", t.Name())
noopLogger := log.NewLogger(log.DiscardHandler())
server := NewServer("localhost", 0, mockStorageMgr, noopLogger, metrics.NoopMetrics)
server.registerRoutes(r)
r.ServeHTTP(rec, req)

Expand Down

0 comments on commit 28bb62c

Please sign in to comment.