Removed echo-contrib dependency.

This commit is contained in:
mikestefanello 2024-06-15 16:56:47 -04:00
parent 8eafb6b666
commit 75aefa8a0a
13 changed files with 108 additions and 18 deletions

27
pkg/session/session.go Normal file
View file

@ -0,0 +1,27 @@
package session
import (
"errors"
"github.com/gorilla/sessions"
"github.com/labstack/echo/v4"
"github.com/mikestefanello/pagoda/pkg/context"
)
// ErrStoreNotFound indicates that the session store was not present in the context
var ErrStoreNotFound = errors.New("session store not found")
// Get returns a session
func Get(ctx echo.Context, name string) (*sessions.Session, error) {
s := ctx.Get(context.SessionKey)
if s == nil {
return nil, ErrStoreNotFound
}
store := s.(sessions.Store)
return store.Get(ctx.Request(), name)
}
// Store sets the session storage in the context
func Store(ctx echo.Context, store sessions.Store) {
ctx.Set(context.SessionKey, store)
}

View file

@ -0,0 +1,23 @@
package session
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/sessions"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)
func TestGetStore(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
ctx := e.NewContext(req, httptest.NewRecorder())
_, err := Get(ctx, "test")
assert.Equal(t, ErrStoreNotFound, err)
Store(ctx, sessions.NewCookieStore([]byte("secret")))
_, err = Get(ctx, "test")
assert.NoError(t, err)
}