-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use gorilla mux for POST routes
- Loading branch information
Showing
5 changed files
with
211 additions
and
272 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/Layr-Labs/eigenda-proxy/metrics" | ||
"github.com/Layr-Labs/eigenda-proxy/mocks" | ||
"github.com/ethereum/go-ethereum/log" | ||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestRouting tests that the routes were properly encoded. | ||
// We should eventually replace this with autogenerated specmatic tests over an openapi spec. | ||
func TestRouting(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
mockRouter := mocks.NewMockIRouter(ctrl) | ||
|
||
m := metrics.NewMetrics("default") | ||
server := NewServer("localhost", 8080, mockRouter, log.New(), m) | ||
err := server.Start() | ||
require.NoError(t, err) | ||
|
||
tests := []struct { | ||
name string | ||
url string | ||
method string | ||
body []byte | ||
expectedCode int | ||
expectedBody string | ||
}{ | ||
{ | ||
name: "Not Found - Must have a commitment key", | ||
url: "/get/0x", | ||
method: http.MethodGet, | ||
body: nil, | ||
// originally we returned 400 for these, but now we return 404 because | ||
// not having a commitment is not a valid route. | ||
expectedCode: http.StatusNotFound, | ||
expectedBody: "404 page not found\n", | ||
}, | ||
{ | ||
name: "Not Found - Op Mode InvalidCommitmentKey", | ||
url: "/get/0x1", | ||
body: nil, | ||
// originally we returned 400 for these, but now we return 404 because | ||
// not having a commitment is not a valid route. | ||
expectedCode: http.StatusNotFound, | ||
expectedBody: "404 page not found\n", | ||
}, | ||
{ | ||
name: "Not Found - Op Mode InvalidCommitmentKey", | ||
url: "/get/0x999", | ||
body: nil, | ||
// originally we returned 400 for these, but now we return 404 because | ||
// not having a commitment is not a valid route. | ||
expectedCode: http.StatusNotFound, | ||
expectedBody: "404 page not found\n", | ||
}, | ||
{ | ||
name: "Not Found OP Keccak256 - TooShortCommitmentKey", | ||
url: "/put/0x", | ||
method: http.MethodPut, | ||
body: []byte("some data"), | ||
// originally we returned 400 for these, but now we return 404 because | ||
// not having a commitment is not a valid route. | ||
expectedCode: http.StatusNotFound, | ||
expectedBody: "404 page not found\n", | ||
}, | ||
{ | ||
name: "Not Found OP Keccak256 - TooShortCommitmentKey", | ||
url: "/put/0x1", | ||
body: []byte("some data"), | ||
// originally we returned 400 for these, but now we return 404 because | ||
// not having a commitment is not a valid route. | ||
expectedCode: http.StatusNotFound, | ||
expectedBody: "404 page not found\n", | ||
}, | ||
{ | ||
name: "Not Found OP Keccak256 - InvalidCommitmentPrefixBytes", | ||
url: fmt.Sprintf("/put/0x999%s", testCommitStr), | ||
body: []byte("some data"), | ||
// originally we returned 400 for these, but now we return 404 because | ||
// not having a commitment is not a valid route. | ||
expectedCode: http.StatusNotFound, | ||
expectedBody: "404 page not found\n", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
req := httptest.NewRequest(tt.method, tt.url, nil) | ||
rec := httptest.NewRecorder() | ||
server.httpServer.Handler.ServeHTTP(rec, req) | ||
|
||
require.Equal(t, tt.expectedCode, rec.Code) | ||
require.Equal(t, tt.expectedBody, rec.Body.String()) | ||
|
||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.