Skip to content

Commit

Permalink
Prevent panic in NewSession function (#140)
Browse files Browse the repository at this point in the history
* prevent panic in NewSession function

* TestSessionCookieStore added

* change test function name
  • Loading branch information
ahmdrz authored and kisielk committed Feb 9, 2018
1 parent 41ee504 commit 6ba88b7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
7 changes: 4 additions & 3 deletions sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ type Options struct {
// NewSession is called by session stores to create a new session instance.
func NewSession(store Store, name string) *Session {
return &Session{
Values: make(map[interface{}]interface{}),
store: store,
name: name,
Values: make(map[interface{}]interface{}),
store: store,
name: name,
Options: new(Options),
}
}

Expand Down
25 changes: 25 additions & 0 deletions sessions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,31 @@ func TestFlashes(t *testing.T) {
}
}

func TestCookieStoreMapPanic(t *testing.T) {
defer func() {
err := recover()
if err != nil {
t.Fatal(err)
}
}()

store := NewCookieStore([]byte("aaa0defe5d2839cbc46fc4f080cd7adc"))
req, err := http.NewRequest("GET", "http://www.example.com", nil)
if err != nil {
t.Fatal("failed to create request", err)
}
w := httptest.NewRecorder()

session := NewSession(store, "hello")

session.Values["data"] = "hello-world"

err = session.Save(req, w)
if err != nil {
t.Fatal("failed to save session", err)
}
}

func init() {
gob.Register(FlashMessage{})
}

0 comments on commit 6ba88b7

Please sign in to comment.