e.GET("/category/:slug/:id", func(c echo.Context) error {
fmt.Println(c.Get("$1"))
fmt.Println(c.ParamNames())
fmt.Println(c.ParamValues())
// Routes
e.GET("/users", h.ListUsers)
e.POST("/user", h.AddUser)
e.GET("/user/:email", h.GetUser)
e.PUT("/blah", h.ModifyUser)
e.DELETE("/user/:email", h.DeleteUser)
e.Logger.Fatal(e.Start(":8080"))
}
main()
.
PUT
) is extremely simplified just because nothing is working.
func (h *Handler) ModifyUser(c echo.Context) error {
return c.JSON(http.StatusOK, "Got it!")
}
curl -X PUT http://localhost:8080/blah
{"message":"Not Found"}
e := echo.New()
e.Use(middleware.Logger())
r := e.Use(middleware.JWT([]byte("secret")))
e.Use(middleware.JWT(([]byte)("secret"))) used as value
u := new(model.User
if err := c.Bind(u); err != nil {
return err
}
i've a handler;
func Create() func(ctx echo.Context) error {
return func(c echo.Context) error {
var country models.RestaurantCountry
if err := c.Bind(&country); err != nil {
return echo.ErrBadRequest
}
restaurantCountry, err := RestaurantCountryModel.Create(country)
if err != nil {
we, _ := err.(mongo.WriteException)
writeErr := we.WriteErrors[0]
switch writeErr.Code {
case errorDuplicateKey:
return echo.NewHTTPError(409, "Conflict: Restaurant Country already exists")
default:
return echo.ErrInternalServerError
}
}
return c.JSON(http.StatusCreated, map[string]models.RestaurantCountry{
"data": restaurantCountry,
})
}
}
I now want to test this handler;
func TestRestaurantCountryCreate(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(echo.POST, "/v1/admin", strings.NewReader(restaurantCountryJSON))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
c.SetPath("/restaurants/countrys")
h := Create()
// Assertions
if assert.NoError(t, h) {
assert.Equal(t, http.StatusCreated, rec.Code)
}
}
the error I'm getting is;
Cannot use 'h' (type func(ctx echo.Context) error) as type error Type does not implement 'error' as some methods are missing: Error() string
any ideas?