Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 06:42
    dilaragorum starred labstack/echo
  • Jul 05 22:58
    ibrahimF01 starred labstack/echo
  • Jul 05 21:46
    eliezer-souza starred labstack/echo
  • Jul 05 20:14
    pacokleitz starred labstack/echo
  • Jul 05 19:43
    NicolasRodriguezH starred labstack/echo
  • Jul 05 14:18
    nhuysamen-godaddy closed #2213
  • Jul 05 14:18
    nhuysamen-godaddy commented #2213
  • Jul 05 12:17
    aldas commented #2213
  • Jul 05 12:13
    aldas commented #2213
  • Jul 05 12:07
    aldas commented #2213
  • Jul 05 12:06
    aldas commented #2213
  • Jul 05 11:55
    nhuysamen-godaddy opened #2213
  • Jul 05 11:53
    josepowera starred labstack/echo
  • Jul 05 09:01
    ortyomka commented #2209
  • Jul 05 08:54
    khooj starred labstack/echo
  • Jul 05 08:14
    cdAhmad starred labstack/echo
  • Jul 05 05:18
    ganigeorgiev closed #2212
  • Jul 05 05:18
    ganigeorgiev commented #2212
  • Jul 05 05:17
    ganigeorgiev closed #2211
  • Jul 05 05:17
    ganigeorgiev commented #2211
porty
@tqrj
ok thank
danvenn
@danvenn
Hey all
I'd like to create a test that I can hand a path to and echo will route for me and send it to the appropriate handler
Has anyone here done that before? So far I can only find documentation for manually giving it a specific handler function
This message was deleted
Something like this doesn't seem to return anything
func GETTER(path string, params []string, paramValues []string) string {
    e := echo.New()
    req := httptest.NewRequest(http.MethodGet, path, nil)
    rec := httptest.NewRecorder()

    c := e.NewContext(req, rec)

    c.SetParamNames(params...)

    //res := rec.Result()
    defer rec.Result().Body.Close()
    c.SetParamValues(ClientIdString)

    //controllers.GetFullProfile(c)
    return rec.Body.String()
}
1 reply
kyoukhana
@kyoukhana
is there a good Guide to get started with Echo??
1 reply
Emil Nikolov
@e-nikolov
Is there a way to enumerate all routes of an echo router? Something similar to chi.Walk() - https://github.com/go-chi/chi/blob/d32a83448b5f43e42bc96487c6b0b3667a92a2e4/tree.go#L827
Martti T.
@aldas
e.Routes() gives you all registered routes (with default router). does not include vhost ones
Preetham
@preethamsathyamurthy
Hi, how to pass the echo logging middleware that can be used by other packages, when we write the application ?
Martti T.
@aldas
johndiego
@johndiego
how i can testing my routes?
func login(c echo.Context) error {
    validate := validator.New()
    u := new(Auth)
    if err := c.Bind(u); err != nil {
        return echo.NewHTTPError(http.StatusBadRequest)
    }
    err := validate.Struct(u)

    if err != nil {
        return echo.NewHTTPError(http.StatusBadRequest)
    }
    user , err := domain.GetAuthByUsername(u.Username,u.Password)

    if user.Id == 0  {
          return echo.ErrUnauthorized
    }
    claims := &jwtCustomClaims{ user.Username,*user.Active,*user.Admin,user.Roles,
        jwt.StandardClaims{
            ExpiresAt: time.Now().Add(time.Hour * 72).Unix(),
        },
    }
    token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)

    t, err := token.SignedString([]byte(os.Getenv("ACCESS_SECRET")))
    if err != nil {
        return err
    }

    return c.JSON(http.StatusOK, echo.Map{
        "token": t,"roles":user.Roles,"username": user.Username,
    })
}
func startAuthRouters(app *echo.Echo){
    auth := app.Group("/auth/login")
    auth.POST("", login)
}
MY TESTS
package controllers

import (
    "testing"
    "net/http"
    "net/http/httptest"
    "github.com/labstack/echo/v4"
    "github.com/stretchr/testify/assert"
    "fmt"
)

func TestAuth(t *testing.T) {
    // t.Parallel()
    // Start()
    t.Run("Controllers ECHO", func(t *testing.T) {

        req := httptest.NewRequest(echo.POST, "/auth/login",nil)
        rec := httptest.NewRecorder()

        // // req.Header.Set("Content-Type", "application/json")
        fmt.Println(req)
        fmt.Println(rec)
        // // req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
        // c.SetPath("/auth/login")
        // h := login(c)
        // fmt.Println(h)
        // fmt.Println(rec)
        // assert.Equal(t, http.StatusBadRequest, rec.Code)
        assert.Equal(t, http.StatusBadRequest, rec.Code)

    })
}
image.png
Martti T.
@aldas
func TestHandler(t *testing.T) {
    e := echo.New()
    req := httptest.NewRequest(http.MethodGet, "/", nil)
    rec := httptest.NewRecorder()
    c := e.NewContext(req, rec)

    handler := func(c echo.Context) error {
        return c.String(http.StatusOK, "OK")
    }

    err := handler(c)

    assert.NoError(t, err)
    assert.Equal(t, http.StatusOK, rec.Code)
}
Martti T.
@aldas

lets say your code is

type authService interface {
    GetAuthByUsername(username string, password string) (string, error)
}
type AuthRoutes struct {
    auth authService
}

func (a AuthRoutes) login(c echo.Context) error {
    auth, err := a.auth.GetAuthByUsername("user", "pass")
    if err != nil {
        return err
    }
    return c.JSON(http.StatusCreated, map[string]string{"auth": auth})
}

then you would create something like that for test

type authServiceMock struct {
    mock.Mock
}

func (m *authServiceMock) GetAuthByUsername(username string, password string) (string, error) {
    args := m.Called(username, password)
    return args.String(0), args.Error(1)
}

func TestHandler(t *testing.T) {
    e := echo.New()
    req := httptest.NewRequest(http.MethodGet, "/", nil)
    rec := httptest.NewRecorder()
    c := e.NewContext(req, rec)

    serviceMock := new(authServiceMock)
    serviceMock.On("GetAuthByUsername", "user", "pass").Return("myauth", nil)
    auth := AuthRoutes{auth: serviceMock}

    err := auth.login(c)

    assert.NoError(t, err)
    assert.Equal(t, http.StatusCreated, rec.Code)

    serviceMock.AssertExpectations(t)
}

in that way you can mock that auth service inside your handler

mock.mock is from testify "github.com/stretchr/testify/mock"
Matthias
@matthias:bsr.mn
[m]

hi guys. I am using the proxy module and am proxying some "blocking requests" meaning requests that live for 5 minutes. my clients (that are connecting to the echo proxy) are killing them if they don't need them anymore, but on the echo proxy side, those requests seem to stay open. is that a bug, or expected behaviour?

how can I make sure that those cancelled requests are properly cleaned up?

Matthias
@matthias:bsr.mn
[m]
ah wait, I think there might be something seriously wrong with my code...
ok yeah I was using ModifyResopnse and I didn't close the response.body, which caused the issue
sorry for the noise!
Martti T.
@aldas
It is probably better to ask questions in https://github.com/labstack/echo/discussions I do not think there are many people actively monitoring this chat.
Matthias
@matthias:bsr.mn
[m]
is there any way to use the proxy middleware dynamically? say i have the route /proxy/server1/foo I want to proxy http://server1/foo and if I have /proxy/server2/foo I want to proxy http://server2/foo. server1 and server2 can be anything in this scenario
Matthias
@matthias:bsr.mn
[m]
nevermind, figured it out
José María Martín
@jose-martin-m
Hi! Is there any way to make the 'Timeout' middleware to generate a JSON response? And set custom headers for the response?