-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5531e0b
commit 71f7de8
Showing
13 changed files
with
108 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package middleware | ||
|
||
import ( | ||
"github.com/gorilla/context" | ||
"github.com/gorilla/sessions" | ||
"github.com/labstack/echo/v4" | ||
"github.com/mikestefanello/pagoda/pkg/session" | ||
) | ||
|
||
// Session sets the session storage in the request context | ||
func Session(store sessions.Store) echo.MiddlewareFunc { | ||
return func(next echo.HandlerFunc) echo.HandlerFunc { | ||
return func(ctx echo.Context) error { | ||
defer context.Clear(ctx.Request()) | ||
session.Store(ctx, store) | ||
return next(ctx) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package middleware | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gorilla/sessions" | ||
"github.com/mikestefanello/pagoda/pkg/session" | ||
"github.com/mikestefanello/pagoda/pkg/tests" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSession(t *testing.T) { | ||
ctx, _ := tests.NewContext(c.Web, "/") | ||
_, err := session.Get(ctx, "test") | ||
assert.Equal(t, session.ErrStoreNotFound, err) | ||
|
||
store := sessions.NewCookieStore([]byte("secret")) | ||
err = tests.ExecuteMiddleware(ctx, Session(store)) | ||
require.NoError(t, err) | ||
|
||
_, err = session.Get(ctx, "test") | ||
assert.NotEqual(t, session.ErrStoreNotFound, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,6 @@ | |
</div> | ||
</section> | ||
|
||
{{template "footer"}} | ||
{{template "footer" .}} | ||
</body> | ||
</html> |