From 4dc44e70c48d38ba830d5f7bae4d1225eb14f381 Mon Sep 17 00:00:00 2001 From: Mikelle Date: Wed, 24 Mar 2021 23:13:45 +0300 Subject: [PATCH 1/3] added checker for version in no route api --- node/node.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/node/node.go b/node/node.go index 3445933..6f5b20e 100644 --- a/node/node.go +++ b/node/node.go @@ -21,6 +21,7 @@ import ( "fmt" "net" "net/http" + "regexp" "sync" "time" @@ -608,6 +609,13 @@ type NodeAPI struct { //nolint:golint } func handleNoRoute(c *gin.Context) { + matched, _ := regexp.MatchString(`^/v[0-9]+/`, c.Request.URL.Path) + if !matched { + c.JSON(http.StatusNotFound, gin.H{ + "error": "Version not provided, please provide a valid version in the path such as v1", + }) + return + } c.JSON(http.StatusNotFound, gin.H{ "error": "404 page not found", }) From 01ec1ca395eb0d4658254f434ebc158364ab1b06 Mon Sep 17 00:00:00 2001 From: Mikelle Date: Thu, 25 Mar 2021 22:41:46 +0300 Subject: [PATCH 2/3] added test and move logic to api.NewAPI --- api/api.go | 2 ++ api/api_test.go | 22 +++++++++++++++++++++- api/noroute.go | 21 +++++++++++++++++++++ api/noroute_test.go | 29 +++++++++++++++++++++++++++++ node/node.go | 15 --------------- 5 files changed, 73 insertions(+), 16 deletions(-) create mode 100644 api/noroute.go create mode 100644 api/noroute_test.go diff --git a/api/api.go b/api/api.go index 926bc86..ddae57c 100644 --- a/api/api.go +++ b/api/api.go @@ -50,6 +50,8 @@ func NewAPI( hermezAddress: consts.HermezAddress, } + server.NoRoute(a.noRoute) + v1 := server.Group("/v1") // Add coordinator endpoints diff --git a/api/api_test.go b/api/api_test.go index a6ff1a9..f95f547 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -41,7 +41,8 @@ type Pendinger interface { } const apiPort = "4010" -const apiURL = "http://localhost:" + apiPort + "/v1/" +const apiIP = "http://localhost:" +const apiURL = apiIP + apiPort + "/v1/" var SetBlockchain = ` Type: Blockchain @@ -841,6 +842,25 @@ func doBadReq(method, path string, reqBody io.Reader, expectedResponseCode int) return swagger.ValidateResponse(ctx, responseValidationInput) } +func doSimpleReq(method, endpoint string) (string, error) { + client := &http.Client{} + httpReq, err := http.NewRequest(method, endpoint, nil) + if err != nil { + return "", tracerr.Wrap(err) + } + resp, err := client.Do(httpReq) + if err != nil { + return "", tracerr.Wrap(err) + } + //nolint + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "", tracerr.Wrap(err) + } + return string(body), nil +} + // test helpers func getTimestamp(blockNum int64, blocks []common.Block) time.Time { diff --git a/api/noroute.go b/api/noroute.go new file mode 100644 index 0000000..04f7cd9 --- /dev/null +++ b/api/noroute.go @@ -0,0 +1,21 @@ +package api + +import ( + "net/http" + "regexp" + + "github.com/gin-gonic/gin" +) + +func (a *API) noRoute(c *gin.Context) { + matched, _ := regexp.MatchString(`^/v[0-9]+/`, c.Request.URL.Path) + if !matched { + c.JSON(http.StatusNotFound, gin.H{ + "error": "Version not provided, please provide a valid version in the path such as v1", + }) + return + } + c.JSON(http.StatusNotFound, gin.H{ + "error": "404 page not found", + }) +} diff --git a/api/noroute_test.go b/api/noroute_test.go new file mode 100644 index 0000000..08223c1 --- /dev/null +++ b/api/noroute_test.go @@ -0,0 +1,29 @@ +package api + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNoRouteVersionNotProvided(t *testing.T) { + endpoint := apiIP + apiPort + "/" + // not using doGoodReq, bcs internally + // there is a method FindRoute that checks route and returns error + resp, err := doSimpleReq("GET", endpoint) + assert.NoError(t, err) + assert.Equal(t, + "{\"error\":\"Version not provided, please provide a valid version in the path such as v1\"}\n", + resp) +} + +func TestNoRoute(t *testing.T) { + endpoint := apiURL + // not using doGoodReq, bcs internally + // there is a method FindRoute that checks route and returns error + resp, err := doSimpleReq("GET", endpoint) + assert.NoError(t, err) + assert.Equal(t, + "{\"error\":\"404 page not found\"}\n", + resp) +} diff --git a/node/node.go b/node/node.go index 6f5b20e..2ff9d65 100644 --- a/node/node.go +++ b/node/node.go @@ -21,7 +21,6 @@ import ( "fmt" "net" "net/http" - "regexp" "sync" "time" @@ -608,19 +607,6 @@ type NodeAPI struct { //nolint:golint addr string } -func handleNoRoute(c *gin.Context) { - matched, _ := regexp.MatchString(`^/v[0-9]+/`, c.Request.URL.Path) - if !matched { - c.JSON(http.StatusNotFound, gin.H{ - "error": "Version not provided, please provide a valid version in the path such as v1", - }) - return - } - c.JSON(http.StatusNotFound, gin.H{ - "error": "404 page not found", - }) -} - // NewNodeAPI creates a new NodeAPI (which internally calls api.NewAPI) func NewNodeAPI( addr string, @@ -630,7 +616,6 @@ func NewNodeAPI( l2db *l2db.L2DB, ) (*NodeAPI, error) { engine := gin.Default() - engine.NoRoute(handleNoRoute) engine.Use(cors.Default()) _api, err := api.NewAPI( coordinatorEndpoints, explorerEndpoints, From 7f971fb72bf6cceb3a0309294c37f5abce6fbbe9 Mon Sep 17 00:00:00 2001 From: Mikelle Date: Wed, 31 Mar 2021 19:11:07 +0300 Subject: [PATCH 3/3] changed const declaration in api_test.go --- api/api_test.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/api/api_test.go b/api/api_test.go index 1b0f5f0..37d681e 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -40,9 +40,11 @@ type Pendinger interface { New() Pendinger } -const apiPort = "4010" -const apiIP = "http://localhost:" -const apiURL = apiIP + apiPort + "/v1/" +const ( + apiPort = "4010" + apiIP = "http://localhost:" + apiURL = apiIP + apiPort + "/v1/" +) var SetBlockchain = ` Type: Blockchain