Skip to content
This repository has been archived by the owner on Mar 16, 2019. It is now read-only.

Commit

Permalink
gorilla#80 return request in Get methods in order to maintain context
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnps authored and Qifan Xi committed Aug 14, 2017
1 parent 6aef9df commit fc8e4e7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
10 changes: 5 additions & 5 deletions sessions_test.go
Expand Up @@ -48,7 +48,7 @@ func TestFlashes(t *testing.T) {
req, _ = http.NewRequest("GET", "http://localhost:8080/", nil)
rsp = NewRecorder()
// Get a session.
if session, err = store.Get(req, "session-key"); err != nil {
if session, req, err = store.Get(req, "session-key"); err != nil {
t.Fatalf("Error getting session: %v", err)
}
// Get a flash.
Expand All @@ -71,7 +71,7 @@ func TestFlashes(t *testing.T) {
t.Fatal("No cookies. Header:", hdr)
}

if _, err = store.Get(req, "session:key"); err.Error() != "sessions: invalid character in cookie name: session:key" {
if _, req, err = store.Get(req, "session:key"); err.Error() != "sessions: invalid character in cookie name: session:key" {
t.Fatalf("Expected error due to invalid cookie name")
}

Expand All @@ -81,7 +81,7 @@ func TestFlashes(t *testing.T) {
req.Header.Add("Cookie", cookies[0])
rsp = NewRecorder()
// Get a session.
if session, err = store.Get(req, "session-key"); err != nil {
if session, req, err = store.Get(req, "session-key"); err != nil {
t.Fatalf("Error getting session: %v", err)
}
// Check all saved values.
Expand Down Expand Up @@ -114,7 +114,7 @@ func TestFlashes(t *testing.T) {
req, _ = http.NewRequest("GET", "http://localhost:8080/", nil)
rsp = NewRecorder()
// Get a session.
if session, err = store.Get(req, "session-key"); err != nil {
if session, req, err = store.Get(req, "session-key"); err != nil {
t.Fatalf("Error getting session: %v", err)
}
// Get a flash.
Expand All @@ -141,7 +141,7 @@ func TestFlashes(t *testing.T) {
req.Header.Add("Cookie", cookies[0])
rsp = NewRecorder()
// Get a session.
if session, err = store.Get(req, "session-key"); err != nil {
if session, req, err = store.Get(req, "session-key"); err != nil {
t.Fatalf("Error getting session: %v", err)
}
// Check all saved values.
Expand Down
14 changes: 9 additions & 5 deletions store.go
Expand Up @@ -21,7 +21,7 @@ import (
// See CookieStore and FilesystemStore for examples.
type Store interface {
// Get should return a cached session.
Get(r *http.Request, name string) (*Session, error)
Get(r *http.Request, name string) (*Session, *http.Request, error)

// New should create and return a new session.
//
Expand Down Expand Up @@ -76,10 +76,12 @@ type CookieStore struct {
//
// It returns a new session and an error if the session exists but could
// not be decoded.
func (s *CookieStore) Get(r *http.Request, name string) (*Session, error) {
func (s *CookieStore) Get(r *http.Request, name string) (*Session, *http.Request, error) {
var reg *Registry
reg, r = GetRegistry(r)
return reg.Get(s, name)
ses, err := reg.Get(s, name)

return ses, r, err
}

// New returns a session for the given name without adding it to the registry.
Expand Down Expand Up @@ -181,10 +183,12 @@ func (s *FilesystemStore) MaxLength(l int) {
// Get returns a session for the given name after adding it to the registry.
//
// See CookieStore.Get().
func (s *FilesystemStore) Get(r *http.Request, name string) (*Session, error) {
func (s *FilesystemStore) Get(r *http.Request, name string) (*Session, *http.Request, error) {
var reg *Registry
reg, r = GetRegistry(r)
return reg.Get(s, name)
ses, err := reg.Get(s, name)

return ses, r, err
}

// New returns a session for the given name without adding it to the registry.
Expand Down

0 comments on commit fc8e4e7

Please sign in to comment.