mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 11:26:44 +01:00
Merge pull request #674 from hermeznetwork/feature/apiWithoutError
added checker for version in no route api
This commit is contained in:
@@ -79,6 +79,8 @@ func NewAPI(
|
|||||||
}
|
}
|
||||||
server.Use(middleware)
|
server.Use(middleware)
|
||||||
|
|
||||||
|
server.NoRoute(a.noRoute)
|
||||||
|
|
||||||
v1 := server.Group("/v1")
|
v1 := server.Group("/v1")
|
||||||
|
|
||||||
// Add coordinator endpoints
|
// Add coordinator endpoints
|
||||||
|
|||||||
@@ -40,8 +40,11 @@ type Pendinger interface {
|
|||||||
New() Pendinger
|
New() Pendinger
|
||||||
}
|
}
|
||||||
|
|
||||||
const apiPort = "4010"
|
const (
|
||||||
const apiURL = "http://localhost:" + apiPort + "/v1/"
|
apiPort = "4010"
|
||||||
|
apiIP = "http://localhost:"
|
||||||
|
apiURL = apiIP + apiPort + "/v1/"
|
||||||
|
)
|
||||||
|
|
||||||
var SetBlockchain = `
|
var SetBlockchain = `
|
||||||
Type: Blockchain
|
Type: Blockchain
|
||||||
@@ -847,6 +850,25 @@ func doBadReq(method, path string, reqBody io.Reader, expectedResponseCode int)
|
|||||||
return swagger.ValidateResponse(ctx, responseValidationInput)
|
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
|
// test helpers
|
||||||
|
|
||||||
func getTimestamp(blockNum int64, blocks []common.Block) time.Time {
|
func getTimestamp(blockNum int64, blocks []common.Block) time.Time {
|
||||||
|
|||||||
21
api/noroute.go
Normal file
21
api/noroute.go
Normal file
@@ -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",
|
||||||
|
})
|
||||||
|
}
|
||||||
29
api/noroute_test.go
Normal file
29
api/noroute_test.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
@@ -616,12 +616,6 @@ type NodeAPI struct { //nolint:golint
|
|||||||
addr string
|
addr string
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleNoRoute(c *gin.Context) {
|
|
||||||
c.JSON(http.StatusNotFound, gin.H{
|
|
||||||
"error": "404 page not found",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewNodeAPI creates a new NodeAPI (which internally calls api.NewAPI)
|
// NewNodeAPI creates a new NodeAPI (which internally calls api.NewAPI)
|
||||||
func NewNodeAPI(
|
func NewNodeAPI(
|
||||||
addr string,
|
addr string,
|
||||||
@@ -631,7 +625,6 @@ func NewNodeAPI(
|
|||||||
l2db *l2db.L2DB,
|
l2db *l2db.L2DB,
|
||||||
) (*NodeAPI, error) {
|
) (*NodeAPI, error) {
|
||||||
engine := gin.Default()
|
engine := gin.Default()
|
||||||
engine.NoRoute(handleNoRoute)
|
|
||||||
engine.Use(cors.Default())
|
engine.Use(cors.Default())
|
||||||
_api, err := api.NewAPI(
|
_api, err := api.NewAPI(
|
||||||
coordinatorEndpoints, explorerEndpoints,
|
coordinatorEndpoints, explorerEndpoints,
|
||||||
|
|||||||
Reference in New Issue
Block a user